Node.js HTTP Integration#
Install#
yarn add graphql graphql-ez @graphql-ez/http
yarn add -D @types/node
pnpm add graphql graphql-ez @graphql-ez/http
pnpm add -D @types/node
npm install graphql graphql-ez @graphql-ez/http
npm install -D @types/node
Usage#
Server module
import { createServer } from 'http';
import { ezApp } from './ez';
const server = createServer((req, res) => {
EZApp.requestHandler(req, res);
});
const EZApp = ezApp.buildApp({
server,
// ...
});
const port = 8080;
server.listen(port, () => {
console.log(`Listening on port ${port}!`);
});
EZ App module
import { CreateApp, gql } from '@graphql-ez/http';
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#
import { CreateApp, BuildContextArgs, InferContext } from '@graphql-ez/http';
function buildContext({ req }: BuildContextArgs) {
// IncomingMessage
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://npm.im/cors#configuration-options
cors: {
// ...
},
});