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