1 | [![npm version](https://badge.fury.io/js/apollo-server-express.svg)](https://badge.fury.io/js/apollo-server-express) [![Build Status](https://circleci.com/gh/apollographql/apollo-server.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-server) [![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/apollo)
|
2 |
|
3 |
|
4 | This is the Express and Connect integration of GraphQL Server. Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks. [Read the docs](https://www.apollographql.com/docs/apollo-server/). [Read the CHANGELOG.](https://github.com/apollographql/apollo-server/blob/master/CHANGELOG.md)
|
5 |
|
6 | ```shell
|
7 | npm install apollo-server-express
|
8 | ```
|
9 |
|
10 | ## Express
|
11 |
|
12 | ```js
|
13 | const express = require('express');
|
14 | const { ApolloServer, gql } = require('apollo-server-express');
|
15 |
|
16 | // Construct a schema, using GraphQL schema language
|
17 | const typeDefs = gql`
|
18 | type Query {
|
19 | hello: String
|
20 | }
|
21 | `;
|
22 |
|
23 | // Provide resolver functions for your schema fields
|
24 | const resolvers = {
|
25 | Query: {
|
26 | hello: () => 'Hello world!',
|
27 | },
|
28 | };
|
29 |
|
30 | const server = new ApolloServer({ typeDefs, resolvers });
|
31 |
|
32 | const app = express();
|
33 | server.applyMiddleware({ app });
|
34 |
|
35 | app.listen({ port: 4000 }, () =>
|
36 | console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
|
37 | );
|
38 | ```
|
39 |
|
40 | ## Connect
|
41 |
|
42 | > We recommend using `express` rather than `connect`. However, if you wish to
|
43 | > use `connect`, please install [`connect`](https://www.npmjs.com/package/connect)
|
44 | > and [`qs-middleware`](https://www.npmjs.com/package/qs-middleware), in addition
|
45 | > to `apollo-server-express`.
|
46 |
|
47 | ```shell
|
48 | npm install --save connect qs-middleware apollo-server-express
|
49 | ```
|
50 |
|
51 | ```js
|
52 | const connect = require('connect');
|
53 | const { ApolloServer, gql } = require('apollo-server-express');
|
54 | const query = require('qs-middleware');
|
55 |
|
56 | // Construct a schema, using GraphQL schema language
|
57 | const typeDefs = gql`
|
58 | type Query {
|
59 | hello: String
|
60 | }
|
61 | `;
|
62 |
|
63 | // Provide resolver functions for your schema fields
|
64 | const resolvers = {
|
65 | Query: {
|
66 | hello: () => 'Hello world!',
|
67 | },
|
68 | };
|
69 |
|
70 | const server = new ApolloServer({ typeDefs, resolvers });
|
71 |
|
72 | const app = connect();
|
73 | const path = '/graphql';
|
74 |
|
75 | app.use(query());
|
76 | server.applyMiddleware({ app, path });
|
77 |
|
78 | app.listen({ port: 4000 }, () =>
|
79 | console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
|
80 | );
|
81 | ```
|
82 |
|
83 | > Note: `qs-middleware` is only required if running outside of Meteor
|
84 |
|
85 | ## Principles
|
86 |
|
87 | GraphQL Server is built with the following principles in mind:
|
88 |
|
89 | * **By the community, for the community**: GraphQL Server's development is driven by the needs of developers
|
90 | * **Simplicity**: by keeping things simple, GraphQL Server is easier to use, easier to contribute to, and more secure
|
91 | * **Performance**: GraphQL Server is well-tested and production-ready - no modifications needed
|
92 |
|
93 | Anyone is welcome to contribute to GraphQL Server, just read [CONTRIBUTING.md](https://github.com/apollographql/apollo-server/blob/master/CONTRIBUTING.md), take a look at the [roadmap](https://github.com/apollographql/apollo-server/blob/master/ROADMAP.md) and make your first PR!
|