Bird
0
0

How do you correctly add a global dependency to a FastAPI app instance?

easy📝 Syntax Q3 of 15
FastAPI - Dependency Injection
How do you correctly add a global dependency to a FastAPI app instance?
APass a list of Depends instances to the dependencies parameter in FastAPI()
BAdd Depends instances to the middleware parameter in FastAPI()
CUse the @app.global_dependency decorator on the dependency function
DCall app.add_dependency() with the dependency function
Step-by-Step Solution
Solution:
  1. Step 1: Recall FastAPI app parameters

    FastAPI accepts a dependencies parameter that takes a list of Depends instances to apply globally.
  2. Step 2: Eliminate incorrect options

    There is no middleware parameter for dependencies, no @app.global_dependency decorator, and no add_dependency() method.
  3. Final Answer:

    Pass a list of Depends instances to the dependencies parameter in FastAPI() -> Option A
  4. Quick Check:

    dependencies parameter expects list of Depends [OK]
Quick Trick: Use dependencies=[Depends(...)] in FastAPI() constructor [OK]
Common Mistakes:
MISTAKES
  • Trying to add dependencies via middleware parameter
  • Assuming a global_dependency decorator exists
  • Calling non-existent add_dependency() method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes