apollo-server/packages/apollo-server-express/README.md

85 lines
3 KiB
Markdown
Raw Normal View History

---
title: Express / Connect
description: Setting up Apollo Server with Express.js or Connect
---
2016-10-23 01:17:37 -07:00
[![npm version](https://badge.fury.io/js/apollo-server-core.svg)](https://badge.fury.io/js/apollo-server-core) [![Build Status](https://circleci.com/gh/apollographql/apollo-cache-control-js.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-cache-control-js) [![Coverage Status](https://coveralls.io/repos/github/apollographql/apollo-server/badge.svg?branch=master)](https://coveralls.io/github/apollographql/apollo-server?branch=master) [![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](https://www.apollographql.com/#slack)
2017-06-28 23:50:20 -07:00
This is the Express and Connect integration of GraphQL Server. Apollo Server is a community-maintained open-source GraphQL server that works with all Node.js HTTP server frameworks: Express, Connect, Hapi, Koa and Restify. [Read the docs](https://www.apollographql.com/docs/apollo-server/). [Read the CHANGELOG.](https://github.com/apollographql/apollo-server/blob/master/CHANGELOG.md)
2017-06-28 23:50:20 -07:00
```sh
npm install apollo-server@beta apollo-server-express@beta
```
2017-06-28 23:50:20 -07:00
## Express
2017-06-28 23:50:20 -07:00
```js
const express = require('express');
const { registerServer } = require('apollo-server-express');
const { ApolloServer, gql } = require('apollo-server');
// 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 });
2017-06-28 23:50:20 -07:00
const app = express();
registerServer({ server, app });
2017-06-28 23:50:20 -07:00
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
2017-06-28 23:50:20 -07:00
```
## Connect
2017-06-28 23:50:20 -07:00
```js
import connect from 'connect';
const { registerServer } = require('apollo-server-express');
const { ApolloServer, gql } = require('apollo-server');
2017-06-28 23:50:20 -07:00
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;
2017-06-28 23:50:20 -07:00
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
2017-06-28 23:50:20 -07:00
const server = new ApolloServer({ typeDefs, resolvers });
const app = connect();
registerServer({ server, app });
2017-06-28 23:50:20 -07:00
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
2017-06-28 23:50:20 -07:00
```
## 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/master/CONTRIBUTING.md), take a look at the [roadmap](https://github.com/apollographql/apollo-server/blob/master/ROADMAP.md) and make your first PR!