orm Full-stack React + Graph. Tutorial —Apallo Graph,
; Jonas Helfer
Full-stack React + GraphQL Tutorial
Part 1—the frontend: Declarative data fetching and
mocking with Apollo
eo eT)
coer) eer)
arenes SOC Raae
GraphQL is a new API-definition and query language that has the
potential to become the new REST. It makes it easy for UI components
to declaratively fetch data without having to worry about backend
implementation details. Because it’s such a powerful abstraction,
GraphQL can speed up app development and make code much easier
to maintain,
However, despite the great advantages of using GraphQL, the first
step can be a bit daunting, That's why I've started writing a series of
tutorials that take you step by step through building a full-stack React
app with GraphQL and Apollo Client. The series will guide you
through the entire process of building an instant messaging app which
uses GraphQL throughout:
+ Part (this part): Setting up a simple client
+ Part 2: Setting up a simple server
nipidev-tlogapalodala comiful-stack-react-grapha-eril-S82acB24e waorm
Full-stack React + Graph. Tutorial —Apallo Graph,
+ Part g: Writing mutations and keeping the client in sync
+ Part 4: Optimistic UL and client side store updates
+ Parts: Input Types and Custom Cache Resolvers
+ Part 6: Subscriptions on the server
+ Part z: GraphQL. Subscriptions on the Client
This tutorial—the first in the series—is about getting started with
GraphQL on the frontend. It only takes about 20-30 minutes, and by
the end of it you'll have a very simple React UI that loads its data with
GraphQI. and looks something like this:
SO hocathost:
Welcome to Apollo
Asirpler
Let’s get started!
1. Getting set up
Note: To do this tutorial you will need to have node, npm and git
installed on your machine, and know a little bit about React,
We're going to use create-react-app in this tutorial, so go ahead
and install that:
> npm install -g create-react-app
We'll also clone the tutorial repository from GitHub, which has some
CSS and images in it that we'll use later.
nipidev-tlogapalodala comful-stack-reac-grapha-eril-S82act24e anaorm Full-stack React + Graph. Tutorial —Apallo Graph,
> git clone https://github.con/apollographal/graphal-
rial.git
> ed graphgi-tutorial
Next, we create our react app with
eact-app
> cd client
To make sure it’s working, let's start our server:
> npm sta
If it all worked, you should now see the following in your browser:
S [© becathost 3
at) HO
Welcome to React
To get storied, edit axe/app. 32 and save to reload.
2. Writing the first component
Since we're building an app with Apollo here, let's change the logo
and CSS by copying over 2oso.svg and app.ces
from ../zesources
> ed ere
> ep ../.-/resources/*
nipidev-tlogapalodala comiful-stack-react-grapha-eril-S82acB24e anaorm Full-stack React + Graph. Tutorial —Apallo Graph,
To keep this initial tutorial short, we'll only build a simple list view
today. Let's change a few things in app.js :
1, Change “Welcome to React” to “Welcome to Apollo”. Apollo is
the name of the GraphQL client we're going to use throughout,
this tutorial series.
2, Remove the “To get started .” paragraph and replace it with a
pure React component that renders an unordered list
with
two list items <1i> , “Channel 1” and “Channel 2” (yes, you
guessed it, we're going to build a messaging app!). Let's name
our list component channetenist
Now your app.je should look like this:
import React, { Component } from ‘react';
import logo from './logo-svg'
import './app.css"?
const Channelsist = () >
(eun>
);
class App extends Component {
render() (
return (
M
"app- log
export default Apps
create-react-app sets up hot reloading for you, so as soon as you
save the file, the browser window with your app should update to
reflect the changes:
nipidev-tlogapalodala comful-stack-reac-grapha-eril-S82act24e aneorm Full-stack React + Graph. Tutorial —Apallo Graph,
© tecalnost-3000 ar|B O04 °
emu S esto
Cae)
ory
IFitlooks lke this,
3, Writing your GraphQL schema
‘Now that we have a simple app running, it’s time to write GraphQL
type definitions for it. The schema will specify what object types exist
in our app, and what fields they have. In addition, it also specifies the
allowed entry points into our API. We'll do that in a file called
schema.3e
export const typebers =
type Channel (
id: 1D! +
name: string
denotes a required field
# This type specifies the entry points into our API. In
this case
# there is only one ~ “channels” - which returns a List
of channels.
type Query {
channels: [Channel] # “(J" means this is a list of
channels
)
With this schema we'll be able to write a simple query to fetch the
data for our channeibist component in the next section. This is
what our query will look like:
nipidev-tlogapalodala comful-stack-reac-grapha-eril-S82act24e siorm Full-stack React + Graph. Tutorial —Apallo Graph,
query Channelstistquery {
channels {
id
4. Wiring your component together with the
GraphQL query
Alright, now that we have our schema and query, we just need to
hook up our component with Apollo Client! Lets install Apollo Client
and some helper packages that we'll need to get GraphQL. into our
app:
> npm i -8 react-apolle
react-apollo is a neat integration of Apollo Client with React that
lets you decorate your components with a higher order component
called grapnei to get your GraphQL data into the component with
zero effort. React Apollo also comes with Apoieciient , which is the
core of Apollo that handles all the data fetching, caching and
optimistic updates (well get to those in another tutorial)
Now with that, let's add a few imports at the top of our app.je and
create an instance of Apollo Client:
import {
Apolleclient,
gal,
graphat,
apolloProvider,
) from ‘react-apollo';
const client = new ApolleClient ()
Next, we decorate the original channeisnist with a GraphQl
higher-order component that takes the query and passes the data to
our component:
nipidev-togapalodala comiful-stack-reac-grapha-aril-St2acB24e wiorm Full-stack React + Graph. Tutorial —Apallo Graph,
const channeleListouery = gal
query ChannelsListouery (
channels {
id
const ChannelszistwithData = graphql (channelsListguery)
(ChanneisList) ;
When wrapped with the grapha1 HOG, our channelstist
component will receive a prop called cata , which will contain
channels when it’s available, or exzor when there is an error. In
addition daca also contains a icading property, which is exue
when Apollo Client is still waiting for data to be fetched.
We'll modify our channeistést component to make sure the user
knows if the component is loading, or if there has been an error:
jst ChannelsList = ({ data: (loading, error, channels
dbp s> 4
if Qeading) (
return loading ...
s
)
if (error) {
return {error.message}
)
return
( channels.map( ch => <1i key={ch.id}>{ch.name}