@@ -95,9 +95,7 @@ The parameter declaration should generally look something like the following:
95
95
str | None ,
96
96
Query(
97
97
title = " Pagination cursor" ,
98
- description = (
99
- " Optional cursor used when moving between pages of results"
100
- ),
98
+ description = " Cursor to navigate paginated results" ,
101
99
),
102
100
] = None ,
103
101
limit : Annotated[
@@ -224,26 +222,38 @@ This is the recommended way to return pagination information alongside a result.
224
222
Here is a very simplified example of a route handler that sets this header:
225
223
226
224
.. code-block :: python
225
+ :emphasize- lines: 27 - 36
227
226
228
227
@router.get (" /query" , response_class = Model)
229
228
async def query (
230
229
* ,
231
230
cursor : Annotated[
232
- ModelCursor | None ,
233
- Query(),
234
- BeforeValidator(lambda c : ModelCursor.from_str(c) if c else None ),
231
+ str | None ,
232
+ Query(
233
+ title = " Pagination cursor" ,
234
+ description = " Cursor to navigate paginated results" ,
235
+ ),
235
236
] = None ,
236
- limit : Annotated[int | None , Query()] = None ,
237
- session : Annotated[
238
- async_scoped_session, Depends(db_session_dependency)
239
- ],
237
+ limit : Annotated[
238
+ int ,
239
+ Query(
240
+ title = " Row limit" ,
241
+ description = " Maximum number of entries to return" ,
242
+ examples = [100 ],
243
+ ge = 1 ,
244
+ le = 100 ,
245
+ ),
246
+ ] = 100 ,
240
247
request : Request,
241
248
response : Response,
242
249
) -> list[Model]:
243
- runner = PydanticQueryRunner(Model, ModelCursor)
250
+ parsed_cursor = None
251
+ if cursor:
252
+ parsed_cursor = ModelCursor.from_str(cursor)
253
+ runner = PaginatedQueryRunner(Model, ModelCursor)
244
254
stmt = build_query(... )
245
255
results = await runner.query_object(
246
- session, stmt, cursor = cursor , limit = limit
256
+ session, stmt, cursor = parsed_cursor , limit = limit
247
257
)
248
258
if cursor or limit:
249
259
response.headers[" Link" ] = results.link_header(request.url)
0 commit comments