React + GraphQL Tutorial - The Server - Apollo Blog
React + GraphQL Tutorial - The Server - Apollo Blog
BACKEND HOW-TO
This is the second part of our full-stack tutorial series that will walk you
“
step by step through building an instant messaging app with React and
GraphQL.
Last week, in the first part of this series, I walked you through building a
simple frontend with React and GraphQL, and showed you some of the
reasons why GraphQL is becoming a pretty popular choice among
frontend developers.
This week’s section is all about the server, and it will explain everything
“
you need to know to set up a simple Node GraphQL server with easy-to-
use open source libraries.
1. Setting up
We’re going to use Express in this tutorial, because that’s what most
people are currently using, but you should be able to follow along even if
you’re using hapi or Koa, because the GraphQL part of this tutorial is
largely identical.
For starters, let’s clone the tutorial GitHub repo, which has a few
resources we’ll need later. If you’ve already done that in Part 1, you can
skip this step.
Since this tutorial has lots of sections, we have to check out the right
branch to make sure the repo is in the state we want it to be for the start
of this one:
git fetch
git checkout t2-start
To get up and running, install the dependencies and start the server:
cd server
npm install
npm start
With the server up and running, we’re ready to dive into the GraphQL
part of this tutorial!
To write our schema, let’s create a directory called src and add a file
called schema.js in it:
mkdir src
cd src
touch schema.js
// src/schema.js
import {
makeExecutableSchema,
addMockFunctionsToSchema, # we'll use this later
} from 'graphql-tools';const typeDefs = `type Channel {
id: ID! # "!" denotes a required fiel
name: String
}# This type specifies the entry points into our API. I
# there is only one - "channels" - which returns a list
type Query {
channels: [Channel] # "[]" means this is a list o
}
`;const schema = makeExecutableSchema({ typeDefs });
export { schema };
We’ll add more types to our messaging app in future tutorials, but for
now the Channel type and one entry point is all we need.
The last line takes our type definition string and turns it into an
executable schema for our server to use. makeExecutableSchema is a
function from graphql-tools , which we now need to install
Next, we’ll import the functions we’re going to use at the top of
our server.js file:
import {
graphqlExpress,
graphiqlExpress,
} from 'graphql-server-express';
import bodyParser from 'body-parser';
We have to do this because the type definitions only describe what types
exist in our schema, and they don’t tell the server how to execute queries
against it. makeExecutableSchema turns our type definitions into an
executable schema, to which we can add custom resolve functions later.
Resolve functions are the core piece of GraphQL that tell the server how
to resolve each part of a query. But more on that later, let’s finish wiring
up our server first!
4. Setting up GraphiQL
Setting up GraphiQL is super easy. All you have to do is add
the graphiqlExpress middleware from graphql-server and point
it to the GraphQL endpoint we just set up:
server.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql'
}));
If you now go to http://localhost:4000/graphiql?query=
{__schema{types{name}}} , you should see the following a er pressing
the play button and expanding the docs:
{
channels {
id
name
}
}
If you run the same query again, you should now get some data with
random IDs and “Hello World”. Curious about how it works or how to
customize it? You can read about it in one of my posts from last year.
Mocking can be very useful if you want start building your frontend
before the backend is done, but since we want to learn how to build a
real server, let’s delete the addMockFunctionsToSchema line and
write some actual resolve functions!
// src/resolvers.jsconst channels = [{
id: 1,
name: 'soccer',
}, {
id: 2,
name: 'baseball',
}];export const resolvers = {
Query: {
channels: () => {
return channels;
},
},
};
To make our server use the resolve function we just defined, all we have
to do is import it in schema.js and pass it
to makeExecutableSchema like so:
// schema.js
// ...
import { resolvers } from './resolvers';// ...
const schema = makeExecutableSchema({ typeDefs, resolve
export { schema };
Make sure to remove the call to addMockFunctionsToSchema ,
otherwise your resolve function won’t get applied.
If everything is working, here’s what you should see when you run the
query again:
Success!
Part 8: Pagination
If you liked this tutorial and want to keep learning about Apollo and
GraphQL, make sure to click the “Follow” button below, and follow us on
Twitter at @apollographql and @helferjs.
WRITTEN BY
Follow
Jonas Helfer
Read more by Jonas Helfer
SIMILAR POSTS
MARCH 9, 2021
MARCH 4, 2021
MARCH 2, 2021
About Us
Platform
Enterprise
Pricing
Customers
Careers
Search Apollo Blog
Team
Community
Blog
Docs
GraphQL Summit
Help
Get Support
Terms of Service
Privacy Policy