I have strawberry setup to run with fastapi. Basically, like the examples, the fastapi initialization code creates a GraphQLRouter
with a Schema
object and a context_getter
.
Everything works great, I use __get_context
to pass some Depend
encies in.
What I don't know is, how do I conditionally call Depend
encies?
Like I might want a Depends(UserManager)
on some graphql calls and Depends(ClientManager)
on others. Normally in fast api, I could use different routers to assign different dependencies. But most of the strawberry fastapi examples use a single router for a single schema. Or more rarely multiple routers across multiple schemas.
How do I break up a strawberry GraphQL Schema
across multiple GraphQLRouter
s so I can use different Depend
s chains for different requests?
I haven't toyed with the context dependency values yet, but I did just resolve the issue of multiple graphql routers. There is a merge_types helper method.
You'll need to import the queries and mutations from your respective paths and combine them into a single GraphQLRouter decleration. Here's an example:
Now all your queries will be accessible (provided no naming conflicts) in the same api router. Here's the github issue I found that helped me towards the solution: https://github.com/strawberry-graphql/strawberry/pull/1273