UNPKG

3.16 kBMarkdownView Raw
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
4This 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
7npm install apollo-server-express
8```
9
10## Express
11
12```js
13const express = require('express');
14const { ApolloServer, gql } = require('apollo-server-express');
15
16// Construct a schema, using GraphQL schema language
17const typeDefs = gql`
18 type Query {
19 hello: String
20 }
21`;
22
23// Provide resolver functions for your schema fields
24const resolvers = {
25 Query: {
26 hello: () => 'Hello world!',
27 },
28};
29
30const server = new ApolloServer({ typeDefs, resolvers });
31
32const app = express();
33server.applyMiddleware({ app });
34
35app.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
48npm install --save connect qs-middleware apollo-server-express
49```
50
51```js
52const connect = require('connect');
53const { ApolloServer, gql } = require('apollo-server-express');
54const query = require('qs-middleware');
55
56// Construct a schema, using GraphQL schema language
57const typeDefs = gql`
58 type Query {
59 hello: String
60 }
61`;
62
63// Provide resolver functions for your schema fields
64const resolvers = {
65 Query: {
66 hello: () => 'Hello world!',
67 },
68};
69
70const server = new ApolloServer({ typeDefs, resolvers });
71
72const app = connect();
73const path = '/graphql';
74
75app.use(query());
76server.applyMiddleware({ app, path });
77
78app.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
87GraphQL 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
93Anyone 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!