Skip to content

Commit 1fb8dc0

Browse files
committed
Fix pagination example
The example of how to set the response HTTP headers was using the old method of parsing the cursor and the wrong name for the query runner class. Fix both issues.
1 parent 53a7dc6 commit 1fb8dc0

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

docs/user-guide/database/pagination.rst

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ The parameter declaration should generally look something like the following:
9595
str | None,
9696
Query(
9797
title="Pagination cursor",
98-
description=(
99-
"Optional cursor used when moving between pages of results"
100-
),
98+
description="Cursor to navigate paginated results",
10199
),
102100
] = None,
103101
limit: Annotated[
@@ -224,26 +222,38 @@ This is the recommended way to return pagination information alongside a result.
224222
Here is a very simplified example of a route handler that sets this header:
225223

226224
.. code-block:: python
225+
:emphasize-lines: 27-36
227226
228227
@router.get("/query", response_class=Model)
229228
async def query(
230229
*,
231230
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+
),
235236
] = 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,
240247
request: Request,
241248
response: Response,
242249
) -> 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)
244254
stmt = build_query(...)
245255
results = await runner.query_object(
246-
session, stmt, cursor=cursor, limit=limit
256+
session, stmt, cursor=parsed_cursor, limit=limit
247257
)
248258
if cursor or limit:
249259
response.headers["Link"] = results.link_header(request.url)

0 commit comments

Comments
 (0)