-
I'm building a server that allows plugins to extend a GraphQL schema, plugins can be activated and deactivated at runtime to modify the GraphQL schema, and would like to swap the schema after the plugin has made modifications while the server is running, is there a recommended way? Or is it possible to create a new yoga server and swap it in express middleware? Will this have a memory/performance impact if we create the server too many times? How could we dispose the old servers? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In GraphQL Yoga v3 you can provide a factory function to the 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 can write your own plugin for doing this by hooking into both const schemaByRequest = new WeakMap<Request, GraphQLSchema>();
const plugin = {
onRequest({ request }) {
const schema = await myLogicToGetSchema(request);
schemaByRequest.set(request, schema);
},
onEnveloped({ setSchema, context }) {
const schema = schemaByRequest.get(context.request);
setSchema(schema);
}
} |
Beta Was this translation helpful? Give feedback.
In GraphQL Yoga v3 you can provide a factory function to the
schema
which can return a schema based on the incoming requests information.See https://the-guild.dev/graphql/yoga-server/v3/features/schema#conditional-schema for more details.
In GraphQL Yoga v2 you c…