Bird
0
0

Which of the following is the correct way to add a global dependency in FastAPI?

easy📝 Syntax Q12 of 15
FastAPI - Dependency Injection
Which of the following is the correct way to add a global dependency in FastAPI?
Aapp = FastAPI(dependencies=[Depends(common_dependency)])
Bapp = FastAPI(global_dep=Depends(common_dependency))
Capp = FastAPI(dependency=common_dependency())
Dapp = FastAPI(use_global=common_dependency)
Step-by-Step Solution
Solution:
  1. Step 1: Recall FastAPI global dependency syntax

    Global dependencies are passed as a list to the 'dependencies' parameter when creating the FastAPI app.
  2. Step 2: Match the correct syntax

    app = FastAPI(dependencies=[Depends(common_dependency)]) uses 'dependencies=[Depends(common_dependency)]', which is the correct pattern.
  3. Final Answer:

    app = FastAPI(dependencies=[Depends(common_dependency)]) -> Option A
  4. Quick Check:

    Global dependencies use dependencies=[Depends(...)] [OK]
Quick Trick: Use dependencies=[Depends(...)] in FastAPI() constructor [OK]
Common Mistakes:
MISTAKES
  • Using wrong parameter names like global_dep or use_global
  • Calling the dependency function instead of passing Depends()
  • Passing a single dependency without a list

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes