Hapi Integration#
Install#
yarn add @hapi/hapi graphql graphql-ez @graphql-ez/hapi
yarn add -D @types/node @types/hapi__hapipnpm add @hapi/hapi graphql graphql-ez @graphql-ez/hapi
pnpm add -D @types/node @types/hapi__hapinpm install @hapi/hapi graphql graphql-ez @graphql-ez/hapi
npm install -D @types/node @types/hapi__hapiUsage#
Server module
import Hapi from '@hapi/hapi';
import { ezApp } from './ez';
async function init() {
  const port = 8080;
  const server = Hapi.server({
    port,
    host: 'localhost',
  });
  const { hapiPlugin } = await ezApp.buildApp({
    // ...
  });
  await server.register(hapiPlugin);
  await server.start();
  console.log(`Listening on port ${port}!`);
}
init().catch(console.error);
EZ App module
import { CreateApp, gql } from '@graphql-ez/hapi';
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 Hapi 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/hapi';
function buildContext({ req, hapi }: BuildContextArgs) {
  // IncomingMessage
  req;
  // Request | undefined
  hapi?.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 Hapi Cors Options for all the available options.
CreateApp({
  // Check https://hapi.dev/api/?v=20.1.4#-routeoptionscors
  cors: {
    // ...
  },
});