Skip to content

Can we swap schema after yoga server is created? #1884

Answered by n1ru4l
sep2 asked this question in Q&A
Discussion options

You must be logged in to vote

In GraphQL Yoga v3 you can provide a factory function to the schema which can return a schema based on the incoming requests information.

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')
})

See https://the-guild.dev/graphql/yoga-server/v3/features/schema#conditional-schema for more details.


In GraphQL Yoga v2 you c…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by n1ru4l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants