2016-12-16 23:41:32 -06:00
# GraphQL Server for Express, Connect, Hapi, Koa, Restify and AWS Lambda
2016-04-24 16:00:42 -07:00
2016-11-14 22:18:33 +02:00
[](https://badge.fury.io/js/graphql-server-core)
2017-02-04 00:08:22 +02:00
[](https://travis-ci.org/apollographql/graphql-server)
[](https://coveralls.io/github/apollographql/graphql-server?branch=master)
[](http://www.apollodata.com/#slack )
2016-04-24 16:00:42 -07:00
2016-10-24 11:04:18 -07:00
GraphQL Server is a community-maintained open-source GraphQL server. It works with all Node.js HTTP server frameworks: Express, Connect, Hapi, Koa and Restify.
2016-04-24 16:00:42 -07:00
2016-08-01 12:54:47 -07:00
## Principles
2016-04-24 16:00:42 -07:00
2016-10-18 09:36:07 +03:00
GraphQL Server is built with the following principles in mind:
2016-06-10 22:22:38 -07:00
2016-10-18 09:36:07 +03:00
* **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
2016-06-10 22:22:38 -07:00
2016-10-18 09:36:07 +03:00
Anyone is welcome to contribute to GraphQL Server, just read [CONTRIBUTING.md ](./CONTRIBUTING.md ), take a look at the [roadmap ](./ROADMAP.md ) and make your first PR!
2016-06-10 22:22:38 -07:00
2016-08-01 12:54:47 -07:00
## Getting started
2016-10-24 05:34:31 +03:00
GraphQL Server is super easy to set up. Just `npm install graphql-server-<variant>` , write a GraphQL schema, and then use one of the following snippets to get started. For more info, read the [GraphQL Server docs ](http://dev.apollodata.com/tools/graphql-server/index.html ).
2016-08-01 12:54:47 -07:00
2016-09-29 17:46:40 +03:00
### Installation
2016-08-02 10:04:15 -07:00
2016-10-18 09:36:07 +03:00
Just run `npm install --save graphql-server-<variant>` and you're good to go!
2016-10-05 02:11:42 +03:00
where variant is one of the following:
- express
- koa
- hapi
2017-01-25 08:20:10 -06:00
- restify
2016-12-16 23:41:32 -06:00
- lambda
2016-08-02 10:40:40 -07:00
2016-08-01 12:54:47 -07:00
### Express
```js
import express from 'express';
2016-12-13 13:38:29 +01:00
import bodyParser from 'body-parser';
2016-10-22 23:52:32 -07:00
import { graphqlExpress } from 'graphql-server-express';
2016-08-01 12:54:47 -07:00
const myGraphQLSchema = // ... define or import your schema here!
const PORT = 3000;
var app = express();
2016-11-14 23:45:45 +02:00
// bodyParser is needed just for POST.
2016-10-22 23:52:32 -07:00
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema }));
2016-08-01 12:54:47 -07:00
app.listen(PORT);
```
### Connect
```js
2016-08-29 12:35:30 +02:00
import connect from 'connect';
2016-10-05 10:57:45 -05:00
import bodyParser from 'body-parser';
2016-10-22 23:52:32 -07:00
import { graphqlConnect } from 'graphql-server-express';
2016-10-05 10:57:45 -05:00
import http from 'http';
2016-08-01 12:54:47 -07:00
const PORT = 3000;
var app = connect();
2016-11-14 23:45:45 +02:00
// bodyParser is needed just for POST.
2016-10-05 10:57:45 -05:00
app.use('/graphql', bodyParser.json());
2016-10-22 23:52:32 -07:00
app.use('/graphql', graphqlConnect({ schema: myGraphQLSchema }));
2016-08-01 12:54:47 -07:00
2016-10-05 10:57:45 -05:00
http.createServer(app).listen(PORT);
2016-08-01 12:54:47 -07:00
```
2016-09-09 20:22:13 -05:00
### Hapi
2016-09-09 07:49:23 -05:00
2016-10-22 23:52:32 -07:00
Now with the Hapi plugins `graphqlHapi` and `graphiqlHapi` you can pass a route object that includes options to be applied to the route. The example below enables CORS on the `/graphql` route.
2016-09-09 07:49:23 -05:00
2016-08-01 12:54:47 -07:00
```js
import hapi from 'hapi';
2016-10-22 23:52:32 -07:00
import { graphqlHapi } from 'graphql-server-hapi';
2016-08-01 12:54:47 -07:00
const server = new hapi.Server();
const HOST = 'localhost';
const PORT = 3000;
server.connection({
host: HOST,
port: PORT,
});
server.register({
2016-10-22 23:52:32 -07:00
register: graphqlHapi,
2016-09-14 20:35:32 -07:00
options: {
path: '/graphql',
2016-10-22 23:52:32 -07:00
graphqlOptions: {
2016-09-14 20:35:32 -07:00
schema: myGraphQLSchema,
},
route: {
cors: true
}
},
2016-08-01 12:54:47 -07:00
});
2016-09-02 01:07:46 -07:00
server.start((err) => {
if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}` );
});
2016-08-01 12:54:47 -07:00
```
2016-09-09 07:49:23 -05:00
2016-08-01 12:54:47 -07:00
### Koa
```js
2016-12-14 07:40:52 -08:00
import koa from 'koa'; // koa@2
import koaRouter from 'koa-router'; // koa-router@next
2016-10-20 22:01:23 +03:00
import koaBody from 'koa-bodyparser'; // koa-bodyparser@next
2016-10-22 23:52:32 -07:00
import { graphqlKoa } from 'graphql-server-koa';
2016-08-01 12:54:47 -07:00
const app = new koa();
const router = new koaRouter();
const PORT = 3000;
2016-11-14 23:45:45 +02:00
// koaBody is needed just for POST.
2016-08-01 12:54:47 -07:00
app.use(koaBody());
2016-10-22 23:52:32 -07:00
router.post('/graphql', graphqlKoa({ schema: myGraphQLSchema }));
2016-10-20 22:01:23 +03:00
router.get('/graphql', graphqlKoa({ schema: myGraphQLSchema }));
2016-11-14 23:45:45 +02:00
2016-08-01 12:54:47 -07:00
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(PORT);
```
2016-10-24 11:04:18 -07:00
### Restify
```js
import restify from 'restify';
import { graphqlRestify, graphiqlRestify } from 'graphql-server-restify';
const PORT = 3000;
const server = restify.createServer({
title: 'GraphQL Server'
});
2017-01-23 09:43:58 -08:00
const graphQLOptions = { schema: myGraphQLSchema };
2016-10-24 11:04:18 -07:00
server.use(restify.bodyParser());
2017-01-23 09:43:58 -08:00
server.use(restify.queryParser());
2016-10-24 11:04:18 -07:00
2017-01-23 09:43:58 -08:00
server.post('/graphql', graphqlRestify(graphQLOptions));
server.get('/graphql', graphqlRestify(graphQLOptions));
2016-10-24 11:04:18 -07:00
2017-01-22 11:56:35 -08:00
server.get('/graphiql', graphiqlRestify({ endpointURL: '/graphql' }));
2016-10-24 11:04:18 -07:00
server.listen(PORT, () => console.log(`Listening on ${PORT}` ));
```
2016-12-16 23:41:32 -06:00
### AWS Lambda
Lambda function should be run with Node.js v4.3. Requires an API Gateway with Lambda Proxy Integration.
```js
var server = require("graphql-server-lambda");
exports.handler = server.graphqlLambda({ schema: myGraphQLSchema });
```
2016-08-01 12:54:47 -07:00
## Options
2016-12-16 23:41:32 -06:00
=======
2016-10-18 09:36:07 +03:00
GraphQL Server can be configured with an options object with the the following fields:
2016-08-01 12:54:47 -07:00
* **schema**: the GraphQLSchema to be used
* **context**: the context value passed to resolvers during GraphQL execution
* **rootValue**: the value passed to the first resolve function
* **formatError**: a function to apply to every error before sending the response to clients
* **validationRules**: additional GraphQL validation rules to be applied to client-specified queries
* **formatParams**: a function applied for each query in a batch to format parameters before execution
* **formatResponse**: a function applied to each response after execution
All options except for `schema` are optional.
2016-08-20 20:33:20 -07:00
### Whitelisting
The `formatParams` function can be used in combination with the `OperationStore` to enable whitelisting.
```js
const store = new OperationStore(Schema);
store.put('query testquery{ testString }');
2016-10-22 23:52:32 -07:00
graphqlOptions = {
2016-08-20 20:33:20 -07:00
schema: Schema,
formatParams(params) {
params['query'] = store.get(params.operationName);
return params;
},
};
```
2016-08-01 12:54:47 -07:00
## Differences to express-graphql
2016-10-18 09:36:07 +03:00
GraphQL Server and express-graphql are more or less the same thing (GraphQL middleware for Node.js), but there are a few key differences:
2016-08-01 12:54:47 -07:00
2016-10-24 11:04:18 -07:00
* express-graphql works with Express and Connect, GraphQL Server supports Express, Connect, Hapi, Koa and Restify.
2016-10-18 09:36:07 +03:00
* express-graphql's main goal is to be a minimal reference implementation, whereas GraphQL Server's goal is to be a complete production-ready GraphQL server.
* Compared to express-graphql, GraphQL Server has a simpler interface and supports exactly one way of passing queries.
* GraphQL Server separates serving GraphiQL (GraphQL UI) from responding to GraphQL requests.
* express-graphql contains code for parsing HTTP request bodies, GraphQL Server leaves that to standard packages like body-parser.
2016-08-20 20:33:20 -07:00
* Includes an `OperationStore` to easily manage whitelisting
2016-08-02 10:04:15 -07:00
* Built with TypeScript
2016-08-01 12:54:47 -07:00
2016-10-18 09:36:07 +03:00
Despite express-graphql being a reference implementation, GraphQL Server is actually easier to understand and more modular than express-graphql.
2016-08-01 13:31:26 -07:00
2016-10-18 09:36:07 +03:00
That said, GraphQL Server is heavily inspired by express-graphql (it's the reference implementation after all). Rather than seeing the two as competing alternatives, we think that they both have separate roles in the GraphQL ecosystem: express-graphql is a reference implementation, and GraphQL Server is a GraphQL server to be used in production and evolve quickly with the needs of the community. Over time, express-graphql can adopt those features of GraphQL Server that have proven their worth and become established more widely.
2016-09-10 18:37:14 -04:00
2017-03-10 10:37:12 +01:00
### application/graphql requests
2017-03-17 19:59:23 +01:00
express-graphql supports the `application/graphql` Content-Type for requests, which is an alternative to `application/json` request with only the query part sent as text. In the same way that we use `bodyParser.json` to parse `application/json` requests for graphql-server, we can use `bodyParser.text` plus one extra step in order to also parse `application/graphql` requests. Here's an example for express:
2017-03-10 10:37:12 +01:00
```js
import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress } from 'graphql-server-express';
const myGraphQLSchema = // ... define or import your schema here!
const helperMiddleware = [
bodyParser.json(),
bodyParser.text({ type: 'application/graphql' }),
(req, res, next) => {
if (req.is('application/graphql')) {
req.body = { query: req.body };
}
next();
}
];
express()
.use('/graphql', ...helperMiddleware, graphqlExpress({ schema: myGraphQLSchema }))
.listen(3000);
```
2016-10-18 09:36:07 +03:00
## GraphQL Server Development
2016-09-10 18:37:14 -04:00
2017-02-04 00:12:58 -08:00
If you want to develop GraphQL Server locally you must follow the following instructions:
2016-09-10 18:37:14 -04:00
* Fork this repository
2016-10-18 09:36:07 +03:00
* Install the GraphQL Server project in your computer
2016-09-10 18:37:14 -04:00
```
2016-10-18 09:36:07 +03:00
git clone https://github.com/[your-user]/graphql-server
cd graphql-server
2016-10-05 02:32:12 +03:00
npm install
2016-10-18 09:36:07 +03:00
cd packages/graphql-server-< variant > /
2016-09-10 18:37:14 -04:00
npm link
```
2016-10-18 09:36:07 +03:00
* Install your local GraphQL Server in other App
2016-09-10 18:37:14 -04:00
```
cd ~/myApp
2016-10-18 09:36:07 +03:00
npm link graphql-server-< variant >
2016-09-10 18:37:14 -04:00
```