Skip to content

Commit

Permalink
document express customization of fastify endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
EmrysMyrddin committed Apr 28, 2023
1 parent d816a49 commit 7da4465
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions website/src/pages/docs/integrations/integration-with-fastify.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

0 comments on commit 7da4465

Please sign in to comment.