koa-graphql-batch
This is a port of react-relay-network-layer own express
middleware.
Install
npm i koa-graphql-batch --save
Plus some peer dependencies you probably already have:
npm i graphql koa@^2.0.0 koa-graphql --save
And koa-convert
to use koa-graphql
with Koa@2
npm i koa-convert --save
Usage
This works exactly as the default express
middleware.
import koa from 'koa';
import graphqlHTTP from 'koa-graphql';
import graphqlBatchHTTPWrapper from 'koa-graphql-batch';
import bodyParser from 'koa-bodyparser';
import convert from 'koa-convert';
import Router from 'koa-router';
import myGraphqlSchema from './graphqlSchema';
const port = 3000;
const server = new Koa();
consr router = new Router();
const graphqlServer = convert(graphqlHTTP({
schema: myGraphqlSchema,
formatError: (error) => ({
message: error.message,
stack: process.env.NODE_ENV === 'development' ? error.stack.split('\n') : null,
}),
}));
router.all('/graphql/batch',
bodyParser(),
graphqlBatchHTTPWrapper(graphqlServer)
);
router.all('/graphql',
graphqlServer
);
server.use(router.routes()).use(router.allowedMethods());
server.listen(port);
License
MIT