Adding a GraphQL endpoint
GraphQL Server has a slightly different API depending on which server integration you are using, but all of the packages share the same core implementation and options format.
GraphQL Server options
GraphQL Server accepts a
GraphQLOptions object as its single argument. The
GraphQLOptions object has the following properties:
Passing options as a function
Alternatively, GraphQL Server can accept a function which takes the request as input and returns a GraphQLOptions object or a promise that resolves to one:
This is useful if you need to attach objects to your context on a per-request basis, for example to initialize user data, caching tools like
dataloader, or set up some API keys.
Importing ES6 Modules
Currently, the ES6 Module import syntax used in these examples is not implemented in Nodejs 6.x,7.x, and earlier versions. To use these examples, you will need to configure an external tool like Babel that will transpile the import statements into standard require statements. For example,
import express from 'express'; would become
var express = require('express');. If you don’t want to use an external transpiler, you can manually convert the imports to requires using the example format.
Using with Express
The following code snippet shows how to use GraphQL Server with Express:
Using with Connect
Connect is so similar to Express that the integration is in the same package. The following code snippet shows how to use GraphQL Server with Connect:
The arguments passed to
graphqlConnect are the same as those passed to
Using with Hapi
The following code snippet shows how to use GraphQL Server with Hapi:
graphqlOptions can also be a callback or a promise:
Using with Koa 2
The following code snippet shows how to use GraphQL Server with Koa:
graphqlOptions can also be a callback that returns a GraphQLOptions or returns a promise that resolves to GraphQLOptions. This function takes a koa 2
ctx as its input.