103 lines
3.6 KiB
Markdown
103 lines
3.6 KiB
Markdown
[](https://badge.fury.io/js/apollo-server-express)
|
|
[](https://circleci.com/gh/apollographql/apollo-server)
|
|
[](https://community.apollographql.com)
|
|
[](https://github.com/apollographql/apollo-server/blob/HEAD/CHANGELOG.md)
|
|
|
|
|
|
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/main/CHANGELOG.md)
|
|
|
|
```shell
|
|
npm install apollo-server-express graphql
|
|
```
|
|
|
|
## Express
|
|
|
|
```js
|
|
const express = require('express');
|
|
const { ApolloServer, gql } = require('apollo-server-express');
|
|
|
|
async function startApolloServer() {
|
|
// Construct a schema, using GraphQL schema language
|
|
const typeDefs = gql`
|
|
type Query {
|
|
hello: String
|
|
}
|
|
`;
|
|
|
|
// Provide resolver functions for your schema fields
|
|
const resolvers = {
|
|
Query: {
|
|
hello: () => 'Hello world!',
|
|
},
|
|
};
|
|
|
|
const server = new ApolloServer({ typeDefs, resolvers });
|
|
await server.start();
|
|
|
|
const app = express();
|
|
server.applyMiddleware({ app });
|
|
|
|
await new Promise(resolve => app.listen({ port: 4000 }, resolve));
|
|
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
|
|
return { server, app };
|
|
}
|
|
```
|
|
|
|
## Connect
|
|
|
|
> We recommend using `express` rather than `connect`. However, if you wish to
|
|
> use `connect`, please install [`connect`](https://www.npmjs.com/package/connect)
|
|
> and [`qs-middleware`](https://www.npmjs.com/package/qs-middleware), in addition
|
|
> to `apollo-server-express`.
|
|
|
|
```shell
|
|
npm install --save connect qs-middleware apollo-server-express graphql
|
|
```
|
|
|
|
```js
|
|
const connect = require('connect');
|
|
const { ApolloServer, gql } = require('apollo-server-express');
|
|
const query = require('qs-middleware');
|
|
|
|
async function startApolloServer() {
|
|
// Construct a schema, using GraphQL schema language
|
|
const typeDefs = gql`
|
|
type Query {
|
|
hello: String
|
|
}
|
|
`;
|
|
|
|
// Provide resolver functions for your schema fields
|
|
const resolvers = {
|
|
Query: {
|
|
hello: () => 'Hello world!',
|
|
},
|
|
};
|
|
|
|
const server = new ApolloServer({ typeDefs, resolvers });
|
|
await server.start();
|
|
|
|
const app = connect();
|
|
const path = '/graphql';
|
|
|
|
app.use(query());
|
|
server.applyMiddleware({ app, path });
|
|
|
|
await new Promise(resolve => app.listen({ port: 4000 }, resolve));
|
|
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
|
|
return { server, app };
|
|
}
|
|
```
|
|
|
|
> Note: `qs-middleware` is only required if running outside of Meteor
|
|
|
|
## Principles
|
|
|
|
GraphQL Server is built with the following principles in mind:
|
|
|
|
* **By the community, for the community**: GraphQL Server's development is driven by the needs of developers
|
|
* **Simplicity**: by keeping things simple, GraphQL Server is easier to use, easier to contribute to, and more secure
|
|
* **Performance**: GraphQL Server is well-tested and production-ready - no modifications needed
|
|
|
|
Anyone 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!
|