@@ -267,39 +267,35 @@ A set of GraphQL server packages from Apollo that work with various Node.js HTTP
267
267
To run a hello world server with apollo-server-express:
268
268
269
269
\`\`\`bash
270
- npm install apollo-server-express body-parser express graphql graphql-tools
270
+ npm install apollo-server-express express
271
271
\`\`\`
272
272
273
273
Then run \`node server.js\` with this code in \`server.js\`:
274
274
275
275
\`\`\`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');
280
278
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
+ \`;
289
284
290
- var resolvers = {
285
+ const resolvers = {
291
286
Query: {
292
- hello(root) {
293
- return 'world';
294
- }
295
- }
287
+ hello: () => 'Hello world!',
288
+ },
296
289
};
297
290
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
+ );
303
299
\`\`\`
304
300
305
301
Apollo Server also supports all Node.js HTTP server frameworks: Express, Connect, HAPI and Koa.
0 commit comments