APIRouter — Splitting the App into Modules — Concept Check
Questions on APIRouter — Splitting the App into Modules. APIRouter lets each domain (students, drives, auth) live in its own file with its own prefix, tags, and dependencies — include_router assembles them into one app with one /docs.
Questions in this quiz
8 questions on APIRouter — Splitting the App into Modules — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In a FastAPI project, you notice that main.py is growing large and hard to manage. How can APIRouter help?
- AAPIRouter automatically optimizes route performance.
- BAPIRouter allows splitting routes into separate files, keeping main.py small.
- CAPIRouter removes the need for Pydantic models.
- DAPIRouter combines routes into a single function in main.py.
2.A developer claims that using APIRouter means you don't need to define prefixes for each route. What's wrong with this statement?
- AAPIRouter doesn't support prefixes at all.
- BAPIRouter requires a global prefix in main.py.
- CPrefixes must be defined for each individual route.
- DPrefixes are defined once per router, not per route.
3.You have a service with routes for 'students' and 'drives'. How should you organize these using APIRouter?
- AUse a single router with no prefixes for simplicity.
- BCreate separate router files for 'students' and 'drives', each with its own prefix.
- CDefine routes directly in main.py for better performance.
- DCombine 'students' and 'drives' into a single router file.
4.When using APIRouter, what is a common mistake that can lead to untestable business logic?
- ADefining routes without prefixes.
- BUsing Pydantic models in services.
- CPlacing business logic directly in the routers instead of services.
- DFailing to include routers in main.py.
5.A new developer suggests that APIRouter handles versioning automatically. What should you clarify?
- AAPIRouter doesn't support versioning.
- BAPIRouter automatically creates versioned routes.
- CVersioning requires separate FastAPI instances.
- DVersioning is managed by mounting routers with specific prefixes.
6.What is the primary benefit of keeping routers 'thin' and services 'fat' in FastAPI?
- AIt improves the speed of HTTP requests.
- BIt eliminates the need for Pydantic models.
- CIt makes the business logic unit-testable without HTTP context.
- DIt reduces the number of files in the project.
7.You see a project where all routes are defined in main.py. What problem might this cause?
- ARoutes will not work without APIRouter.
- BThe application will run slower.
- CPydantic models cannot be used.
- DThe main.py file becomes difficult to manage and maintain.
8.How does APIRouter contribute to the documentation of a FastAPI application?
- AIt automatically tags routes, organizing them in the /docs.
- BIt generates documentation without any additional setup.
- CIt requires manual tagging of each route for documentation.
- DIt doesn't affect documentation at all.