[QUESTION] Is there a way to pass FastAPI dependencies to a GraphQL App?
See original GitHub issueFirst check
- I used the GitHub search to find a similar issue and didn’t find it.
- [x ] I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
Description
Is it possible to use FastAPI dependencies in a GraphQLApp route?
For example, using the docs:
import graphene
from fastapi import FastAPI
from starlette.graphql import GraphQLApp
class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="stranger"))
def resolve_hello(self, info, name):
return "Hello " + name
app = FastAPI()
app.add_route("/", GraphQLApp(schema=graphene.Schema(query=Query)))
add_route
doesn’t really take dependencies, so would the solution be to use middleware to put things into request.state?
Essentially, my goal would be something like
import graphene
from fastapi import FastAPI
from starlette.graphql import GraphQLApp
class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="stranger"))
def resolve_hello(self, info, name):
return "Hello " + name
app = FastAPI()
@router.get("/gql")
def read_items(request: Request, some_dep = fastapi.Depends()):
request.state.deps = [some_dep]
return GraphQLApp(schema=graphene.Schema(query=Query))(request)
Additional context
Add any other context or screenshots about the feature request here.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Dependencies - First Steps - FastAPI - tiangolo
With the Dependency Injection system, you can also tell FastAPI that your path operation function also "depends" on something else that should be...
Read more >Advanced Dependencies - FastAPI
In Python there's a way to make an instance of a class a "callable". ... sub-dependencies, and this is what will be called...
Read more >Testing Dependencies with Overrides - FastAPI
In this case, you can override the dependency that calls that provider, and use a custom dependency that returns a mock user, only...
Read more >Sub-dependencies - FastAPI
It declares an optional query parameter q as a str , and then it just returns it. This is quite simple (not very...
Read more >Classes as Dependencies - FastAPI
If you pass a "callable" as a dependency in FastAPI, it will analyze the parameters for that "callable", and process them in the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Solved - it appears that the issue was that
add_route
was a Starlette export rather than FastAPI, thus having no concept of dependencies. The regular decorator syntax works if you use the GraphCommenting in a hope that it might help if someone encountered issue while using ariadne as a graphql stack