UNPKG

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