Skip to content

Commit ee7d90a

Browse files
author
Yury
authored
Upd docs: Graphql with JavaScript Express server
Previous version of docs works only with apollo-server-express 1.x.x. Now available 2.9.3 (https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-express)
1 parent af546f2 commit ee7d90a

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

site/code/index.html.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -266,39 +266,37 @@ A set of GraphQL server packages from Apollo that work with various Node.js HTTP
266266
To run a hello world server with apollo-server-express:
267267
268268
\`\`\`bash
269-
npm install apollo-server-express body-parser express graphql graphql-tools
269+
npm install apollo-server-express express graphql
270270
\`\`\`
271271
272272
Then run \`node server.js\` with this code in \`server.js\`:
273273
274274
\`\`\`js
275-
var express = require('express');
276-
var bodyParser = require('body-parser');
277-
var { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
278-
var { makeExecutableSchema } = require('graphql-tools');
275+
const express = require('express');
276+
const { ApolloServer, gql } = require('apollo-server-express');
279277
280-
var typeDefs = [\`
281-
type Query {
282-
hello: String
283-
}
284-
285-
schema {
286-
query: Query
287-
}\`];
278+
// Construct a schema, using GraphQL schema language
279+
const typeDefs = gql\`
280+
type Query {
281+
hello: String
282+
}
283+
\`;
288284
289-
var resolvers = {
285+
// Provide resolver functions for your schema fields
286+
const resolvers = {
290287
Query: {
291-
hello(root) {
292-
return 'world';
293-
}
294-
}
288+
hello: () => 'Hello world!',
289+
},
295290
};
296291
297-
var schema = makeExecutableSchema({typeDefs, resolvers});
298-
var app = express();
299-
app.use('/graphql', bodyParser.json(), graphqlExpress({schema}));
300-
app.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}));
301-
app.listen(4000, () => console.log('Now browse to localhost:4000/graphiql'));
292+
const server = new ApolloServer({ typeDefs, resolvers });
293+
294+
const app = express();
295+
server.applyMiddleware({ app });
296+
297+
app.listen({ port: 4000 }, () =>
298+
console.log(`🚀 Now browse to http://localhost:4000${server.graphqlPath}`)
299+
);
302300
\`\`\`
303301

304302
Apollo Server also supports all Node.js HTTP server frameworks: Express, Connect, HAPI and Koa.

0 commit comments

Comments
 (0)