I have a Flask GraphQL endpoint that looks like:
app.add_url_rule('/graphql', view_func=GraphQLView.as_view(
'graphql',
schema=schema,
graphiql=True,
))
I currently have 3 different schemas. As far as I know, one of the differences between GraphQL and REST is that in GraphQL you just define a single base endpoint. Then the backend processing happens depending on which schema applies on the data received from the frontend. But I'm not sure how this endpoint will end-up choosing the correct schema based on the request body. Any guidance or links to good examples are welcome. I am using Graphene as the library for GraphQL.
PS: This is my first time working with GraphQL so, sorry if my understanding above is incorrect.
CodePudding user response:
Hey,
The way GraphQL works is that the client sends a query/mutation to the endpoint, and the schema will be used to determine what data is requested/mutated. There are a few different ways you can approach this.
You could use a schema directive to denote which schema should be used for a given query or mutation. This would allow you to specify a "schema" argument in your GraphQL query or mutation, and the GraphQL server would use that argument to determine which schema to use.
You could also use a custom GraphQL resolver to determine which schema should be used. This could be done either by looking at the query or mutation itself or by looking at the data in the request body.
Finally, you could also use a custom GraphQL middleware. This would allow you to inspect the request and do some custom logic to determine which schema should be used.
Once you have the ResolverMap set up, you can set it on your GraphQLView like this:
app.add_url_rule('/graphql', view_func=GraphQLView.as_view(
'graphql',
schema=schema,
graphiql=True,
resolver_map=resolver_map
))
This should allow your app to correctly route requests to the appropriate schema.
Good luck, Hope this helps!