Fastify Integration#
Install#
yarn add fastify graphql graphql-ez @graphql-ez/fastify
yarn add -D @types/nodepnpm add fastify graphql graphql-ez @graphql-ez/fastify
pnpm add -D @types/nodenpm install fastify graphql graphql-ez @graphql-ez/fastify
npm install -D @types/nodeUsage#
Server module
import Fastify from 'fastify';
import { ezApp } from './ez';
const app = Fastify({
  logger: true,
});
const { fastifyPlugin } = ezApp.buildApp({
  // ...
});
app.register(fastifyPlugin);
app.listen({ port: 8080 });
EZ App module
import { CreateApp, gql } from '@graphql-ez/fastify';
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 Fastify 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/fastify';
function buildContext({ req, fastify }: BuildContextArgs) {
  // IncomingMessage
  req;
  // FastifyRequest | undefined
  fastify?.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 fastify-cors for all the available options.
CreateApp({
  // Check https://github.com/fastify/fastify-cors#options
  cors: {
    // ...
  },
});