graphql apollo express server code example

Example 1: setup apollo server express

npm install apollo-server-express

# yarn
yarn add apollo-server-express


# server.js
import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress } from 'apollo-server-express';

const myGraphQLSchema = // ... define or import your schema here!
const PORT = 3000;

const app = express();

// bodyParser is needed just for POST.
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema }));

app.listen(PORT);

Example 2: install apollo server

npm install apollo-server graphql

# yarn 
yarn add apollo-server graphql