diff --git a/website/src/pages/docs/integrations/integration-with-fastify.mdx b/website/src/pages/docs/integrations/integration-with-fastify.mdx index bcfc332068..f55f212972 100644 --- a/website/src/pages/docs/integrations/integration-with-fastify.mdx +++ b/website/src/pages/docs/integrations/integration-with-fastify.mdx @@ -79,3 +79,57 @@ app.addContentTypeParser('multipart/form-data', {}, (req, payload, done) => ``` > You can also check a full example on our GitHub repository [here](https://github.com/dotansimha/graphql-yoga/tree/v3/examples/fastify) + +## Customize endpoint + +By default GraphQL Yoga will expose a `/graphql` endpoint. You can customize it by passing a `graphqlEndpoint` option to `createYoga`: + +```ts +import { createYoga } from 'graphql-yoga' +import fastify, { FastifyRequest, FastifyReply } from 'fastify' + +// This is the fastify instance you have created +const app = fastify({ logger: true }) + +const yoga = createYoga<{ + req: FastifyRequest + reply: FastifyReply +}>({ + graphqlEndpoint: '/custom/endpoint', + // Integrate Fastify logger + logging: { + debug: (...args) => args.forEach((arg) => app.log.debug(arg)), + info: (...args) => args.forEach((arg) => app.log.info(arg)), + warn: (...args) => args.forEach((arg) => app.log.warn(arg)), + error: (...args) => args.forEach((arg) => app.log.error(arg)) + } +}) + +/** + * We pass the incoming HTTP request to GraphQL Yoga + * and handle the response using Fastify's `reply` API + * Learn more about `reply` https://www.fastify.io/docs/latest/Reply/ + **/ +app.route({ + url: '/custom/endpoint', + method: ['GET', 'POST', 'OPTIONS'], + handler: async (req, reply) => { + // Second parameter adds Fastify's `req` and `reply` to the GraphQL Context + const response = await yoga.handleNodeRequest(req, { + req, + reply + }) + response.headers.forEach((value, key) => { + reply.header(key, value) + }) + + reply.status(response.status) + + reply.send(response.body) + + return reply + } +}) + +app.listen(4000) +```