Express integration#
Install#
yarn add express graphql graphql-ez @graphql-ez/express
yarn add -D @types/express @types/nodepnpm add express graphql graphql-ez @graphql-ez/express
pnpm add -D @types/express @types/nodenpm install express graphql graphql-ez @graphql-ez/express
npm install -D @types/express @types/nodeUsage#
Server module
import express from 'express';
import { ezApp } from './ez';
const app = express();
ezApp
  .buildApp({
    app,
    // ...
  })
  .then(EZApp => {
    app.use(EZApp.router);
    const port = 8080;
    app.listen(port, () => {
      console.log(`Listening on port ${port}!`);
    });
  });
EZ App module
import { CreateApp, gql } from '@graphql-ez/express';
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 Express 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/express';
function buildContext({ req, express }: BuildContextArgs) {
  // IncomingMessage
  req;
  // Request | undefined
  express?.req;
  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 cors package for all the available options.
CreateApp({
  // Check https://github.com/expressjs/cors#configuration-options
  cors: {
    // ...
  },
});