Skip to content

Commit 68df965

Browse files
authored
Merge pull request graphql#770 from Ajmdag/patch-1
Upd docs: Graphql with JavaScript Express server
2 parents c25d065 + 3278113 commit 68df965

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

site/code/index.html.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -267,39 +267,35 @@ A set of GraphQL server packages from Apollo that work with various Node.js HTTP
267267
To run a hello world server with apollo-server-express:
268268
269269
\`\`\`bash
270-
npm install apollo-server-express body-parser express graphql graphql-tools
270+
npm install apollo-server-express express
271271
\`\`\`
272272
273273
Then run \`node server.js\` with this code in \`server.js\`:
274274
275275
\`\`\`js
276-
var express = require('express');
277-
var bodyParser = require('body-parser');
278-
var { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
279-
var { makeExecutableSchema } = require('graphql-tools');
276+
const express = require('express');
277+
const { ApolloServer, gql } = require('apollo-server-express');
280278
281-
var typeDefs = [\`
282-
type Query {
283-
hello: String
284-
}
285-
286-
schema {
287-
query: Query
288-
}\`];
279+
const typeDefs = gql\`
280+
type Query {
281+
hello: String
282+
}
283+
\`;
289284
290-
var resolvers = {
285+
const resolvers = {
291286
Query: {
292-
hello(root) {
293-
return 'world';
294-
}
295-
}
287+
hello: () => 'Hello world!',
288+
},
296289
};
297290
298-
var schema = makeExecutableSchema({typeDefs, resolvers});
299-
var app = express();
300-
app.use('/graphql', bodyParser.json(), graphqlExpress({schema}));
301-
app.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}));
302-
app.listen(4000, () => console.log('Now browse to localhost:4000/graphiql'));
291+
const server = new ApolloServer({ typeDefs, resolvers });
292+
293+
const app = express();
294+
server.applyMiddleware({ app });
295+
296+
app.listen({ port: 4000 }, () =>
297+
console.log('Now browse to http://localhost:4000' + server.graphqlPath)
298+
);
303299
\`\`\`
304300
305301
Apollo Server also supports all Node.js HTTP server frameworks: Express, Connect, HAPI and Koa.

0 commit comments

Comments
 (0)