Dependencies on API Routers not being called for routes via add_route
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- 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.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import APIRouter, Depends, HTTPException
from strawberry.asgi import GraphQL
from . import graphql_schema
def some_func():
print("CALLED")
raise HTTPException(status_code=400, detail="X-Token header invalid")
print("HERE")
router = APIRouter(dependencies=[Depends(some_func)])
graphql = GraphQL(graphql_schema)
router.add_route("/", graphql)
Description
Accessing a route added with add_route does not invoke the dependencies - it never actually executes some_func (I never see the CALLED print statement in logs)
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.65.2
Python Version
3.9.5
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:15 (5 by maintainers)
Top Results From Across the Web
vue.js - load routes via component from external api and add ...
Another possible solution is to use $router.addRoutes() method. But it is inadequate for your needs. It will not work considering authorization ...
Read more >Bigger Applications - Multiple Files - FastAPI
All the same parameters , responses , dependencies , tags , etc. Tip. In this example, the variable is called router , but...
Read more >Routing in ASP.NET Core - Microsoft Learn
Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints.
Read more >Routing - Laravel - The PHP Framework For Web Artisans
Middleware; Controllers; Subdomain Routing; Route Prefixes; Route Name Prefixes ... the /api URI prefix is automatically applied so you do not need to ......
Read more >Create Maintainable Minimal Web APIs - CODE Magazine
A single public method, AddRoutes() , is needed in order to initialize the routes for router class. This method is called from 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
This works for me, a little hacky but you need to create a wrapper that takes the request and pulls it back to scope, receive and send.
Full example:
@navyamehta
You can use
add_api_route
rather thanadd_route
I don’t know if that will cause other issues for you though. When I usedadd_route
I don’t get any docs for the route but usingadd_api_route
I get docs and the deps are managedadd_route
is just inherited from starlette so wont get the fastapi magic sprinkled in (eg docs and dependencies)Full minimal example: