diff --git a/examples/express/src/app.ts b/examples/express/src/app.ts index 3c13994f7a..01f5b36c8b 100644 --- a/examples/express/src/app.ts +++ b/examples/express/src/app.ts @@ -38,7 +38,7 @@ export function buildApp(app: ReturnType) { logging: false, }) - app.use('/graphql', graphQLServer) + app.use(graphQLServer) return app } diff --git a/website/src/pages/docs/integrations/integration-with-express.mdx b/website/src/pages/docs/integrations/integration-with-express.mdx index cdbe32d1e3..a8636f4b32 100644 --- a/website/src/pages/docs/integrations/integration-with-express.mdx +++ b/website/src/pages/docs/integrations/integration-with-express.mdx @@ -2,7 +2,7 @@ description: Express is the most popular web framework for Node.js. It is a minimalist framework that provides a robust set of features to handle HTTP on Node.js applications. --- -import { PackageCmd } from '@theguild/components' +import { PackageCmd, Callout } from '@theguild/components' # Integration with Express @@ -28,8 +28,7 @@ const app = express() const yoga = createYoga() -// Bind GraphQL Yoga to `/graphql` endpoint -app.use('/graphql', yoga) +app.use(yoga) app.listen(4000, () => { console.log('Running a GraphQL API server at http://localhost:4000/graphql') @@ -37,3 +36,37 @@ app.listen(4000, () => { ``` > You can also check a full example on our GitHub repository [here](https://github.com/dotansimha/graphql-yoga/tree/v3/examples/express) + +## Custom endpoint + +By default, GraphQL Yoga will bind to the `/graphql` endpoint. You can change this behavior by passing a `graphqlEndpoint` option to the `createYoga` function. + +```ts +import express from 'express' +import { createYoga } from 'graphql-yoga' + +const app = express() + +const yoga = createYoga({ + graphqlEndpoint: '/custom/endpoint' +}) + +app.use(yoga) + +app.listen(4000, () => { + console.log('Running a GraphQL API server at http://localhost:4000/graphql') +}) +``` + + + Always provide this option even when using express's router. + +```ts +const yoga = createYoga({ + graphqlEndpoint: '/custom/endpoint' +}) + +app.use('/custom/endpoint', yoga) +``` + +