v3
Features
GraphQL Schema
⚠️
This is the documentation for the old GraphQL Yoga version 3. We recommend upgrading to the latest GraphQL Yoga version 5.

Migrate to GraphQL Yoga v5

GraphQL Schema

GraphQL Yoga makes no assumptions about the schema building library you want to use. Whether you’re using a library such as Pothos, GraphQL Nexus, gqtx, or vanilla graphql-js, as long as it provides you with a GraphQLSchema to schema, you can pass it to GraphQL Yoga.

Providing a Schema

Simply pass an schema to the schema option:

Provide a conditional schema
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { schema } from './schema.js'
 
const yoga = createYoga({ schema })
const server = createServer(yoga)
 
// Start the server and you're done!
server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})

Conditional Schema

You can also pass a factory function for your schema that can return a Promise. The factory function is invoked for every GraphQL request.

⚠️

We do not recommend building a GraphQL schema from scratch for every single incoming request. Please, use a caching mechanism or pre-build your GraphQL schemas before starting the server.

Provide a sync conditional schema
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { getSchemaForViewer } from './schema.js'
 
const yoga = createYoga({
  schema: async (request) =>
    getSchemaForViewer(request.headers.get('x-schema') ?? 'default')
})
const server = createServer(yoga)
 
// Start the server and you're done!
server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})