mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify
https://mercurius.dev/
MIT License
2.34k stars 233 forks source link

Load schema based upon request url parameter #141

Closed RenatoFranze closed 4 years ago

RenatoFranze commented 4 years ago

Hi,

First of all, thanks for this plugin and this is not a bug report or a sugestion, hehe!

Can anyone help me with this scenario: I would like to make an universal route in fastify with an url parameter. Based upon this parameter, my route loads the specific graphql schema and only after that, exposes the graphql/graphqli interface. Is it possible?

Example:

Thanks!

mcollina commented 4 years ago

I don't think this is possible right now. However you could have a preHandler hook to return an early 404 in case it is disabled.

I don't understand the usecase anyway, so I would love to know more.

RenatoFranze commented 4 years ago

Hi @mcollina , thanks for answering.

Basically, I have this:

So, the ideia behind an url parameter is to tell me which database should I connect using Sequelize, and, with graphcraft, creates the right schema for Graphql works properly.

mcollina commented 4 years ago

Unfortunately, I don't think this library would support you well. It assumes the schema does not change during the lifetime of a server.

I would also recommend to rethink some part of your architecture because what you propose would not perform well both for latency and scalability.

asciidisco commented 4 years ago

@RenatoFranze It does sound like an unusual usecase, definitely. You're probably better off with using a dedicated http server (you can use fastify for this) & roll your own GraphQL response/request handler.

You can have a look here, how you could even do that with graphql-jit support, if performance was your concern & the reason you wanted to use fastify-gql. You just need to swap out the schema generation with the one you use & then reply with the one based on your url parameter.

That being said, I think this could be closed //cc @mcollina