You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
importkoafrom'koa';importgraphqlHTTPfrom'koa-graphql';importgraphqlBatchHTTPWrapperfrom'koa-graphql-batch';/* some middleware to convert request.body to a JS object */importbodyParserfrom'koa-bodyparser';/* koa-graphql is a Koa@1 middleware, so needs to be converted */importconvertfrom'koa-convert';/* you'll need some routing middleware also */importRouterfrom'koa-router';importmyGraphqlSchemafrom'./graphqlSchema';constport=3000;constserver=newKoa();consrrouter=newRouter();/* setup standard `graphqlHTTP` Koa middleware */constgraphqlServer=convert(graphqlHTTP({schema: myGraphqlSchema,formatError: (error)=>({// better errors for development. `stack` used in `gqErrors` middlewaremessage: error.message,stack: process.env.NODE_ENV==='development' ? error.stack.split('\n') : null,}),}));/* declare route for batch query */router.all('/graphql/batch',bodyParser(),graphqlBatchHTTPWrapper(graphqlServer));/* declare standard graphql route */router.all('/graphql',graphqlServer);/* mount routes */server.use(router.routes()).use(router.allowedMethods());server.listen(port);
License
MIT
About
Koa middleware to support query batching for react-relay-network-layer