Koa Integration#
Install#
yarn add @koa/router koa graphql graphql-ez @graphql-ez/koa
yarn add -D @types/koa @types/koa__router @types/nodepnpm add @koa/router koa graphql graphql-ez @graphql-ez/koa
pnpm add -D @types/koa @types/koa__router @types/nodenpm install @koa/router koa graphql graphql-ez @graphql-ez/koa
npm install -D @types/koa @types/koa__router @types/nodeUsage#
Server module
import Koa from 'koa';
import KoaRouter from '@koa/router';
import { ezApp } from './ez';
const app = new Koa();
const router = new KoaRouter();
ezApp
  .buildApp({
    app,
    router,
    // ...
  })
  .then(() => {
    app.use(router.routes()).use(router.allowedMethods());
    const port = 8080;
    app.listen(port, () => {
      console.log(`Koa Listening on port ${port}!`);
    });
  });
EZ App module
import { CreateApp, gql } from '@graphql-ez/koa';
export const ezApp = CreateApp({
  // You can use any valid GraphQL Schema
  schema,
  ez: {
    plugins: [
      // EZ Plugins
    ],
  },
  envelop: {
    plugins: [
      // Envelop Plugins
    ],
  },
  // Other Options
});
Build Custom Context#
The Koa specific arguments are optional since, in some specific contexts, those might not available,
but you can always use the HTTP's IncomingMessage req
import { CreateApp, BuildContextArgs, InferContext } from '@graphql-ez/koa';
function buildContext({ req, koa }: BuildContextArgs) {
  // IncomingMessage
  req;
  // Request | undefined
  koa?.request;
  return {
    foo: 'bar',
  };
}
// This snippet allows you to infer the context returned by your 'buildContext' and add it to the EZContext interface
declare module 'graphql-ez' {
  interface EZContext extends InferContext<typeof buildContext> {}
}
export const ezApp = CreateApp({
  // ...
  buildContext,
});
Cross-Origin Resource Sharing (CORS)#
To enable CORS, specify the cors property in your configuration
CreateApp({
  cors: true,
});
Check the @koa/cors for all the available options.
CreateApp({
  // Check https://github.com/koajs/cors#corsoptions
  cors: {
    // ...
  },
});