New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: document schema by http header (#913) #979
Docs: document schema by http header (#913) #979
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice finding, I didn't know this was possible. although this may work, to me it seems a little hack-ish and I think we should try to come up with something better.
the issues I see at the moment are:
- the fastify docs discourage the creation of custom constraints because of the performance impact on the router. maybe by inspiring from the built-in ones we can come up with an implementation without any performance implications?
- the fact that you're registering two different instances of mercurius may still be necessary, but it would be nice it there wasn't so much work involved. maybe there's a way to build something into mercurius which would allow to achieve this without so much code? e.g. if we solved the first issue above and mercurius supported constraints natively, then maybe we can make it work without having to register routes explicitly because mercurius would know that unless the constraint is matched it wouldn't have to run?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fastify is amazing from time to time ;)
I am exploring alternative ways to solve this issue. Here a simple example const app = Fastify()
const schema1 = makeExecutableSchema({
typeDefs: ...
resolvers: ...
})
const schema2 = makeExecutableSchema({
typeDefs: ...
resolvers: ...
})
app.register(mercurius, {
schema: schema1,
})
app.ready(err => {
if (err) throw err
app.graphql.addHook('preExecution', (schema, document, context) => {
if (context.reply.request.headers.schema === "schema2") {
return { schema: schema2}
}
return { schema }
})
}) Not sure which is the impact on loaders and jit. Do you have any clue? |
I think using the constraints is the best way to solve this. Adding support for multiple schemas in Mercurius will make the code harder to read, add a bunch if edge case we don't know about, and increase the maintenance cost. |
@mcollina sounds good, thanks for the feedback. @brainrepo let's go ahead and document this option for the time being, while we think about other options to do this. Based on Matteo's feedback, implementing support for this within mercurius may not be a compelling option, on which case we may consider building a plugin instead, which hides away the implementation details. |
closes #913
This PR explains exposing two different mercurius instances to the same route using the fastify/find-my-way custom constraints.