End-to-end example

Copy and paste this code to have a GraphQL server running in 30 seconds.

Here’s a complete example that sets up a GraphQL server with apollo-server-express and graphql-tools. First, make sure to install the necessary modules:

1
npm install --save apollo-server-express graphql-tools graphql express body-parser

Then, run this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const express = require('express');
const bodyParser = require('body-parser');
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');

// Some fake data
const books = [
{
title: "Harry Potter and the Sorcerer's stone",
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];

// The GraphQL schema in string form
const typeDefs = `
type Query { books: [Book] }
type Book { title: String, author: String }
`;

// The resolvers
const resolvers = {
Query: { books: () => books },
};

// Put together a schema
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});

// Initialize the app
const app = express();

// The GraphQL endpoint
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));

// GraphiQL, a visual editor for queries
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));

// Start the server
app.listen(3000, () => {
console.log('Go to http://localhost:3000/graphiql to run queries!');
});

To understand the example, read the docs about Apollo Server here, and also learn how to make a GraphQL schema in the graphql-tools docs.

Edit on GitHub