data-api-builder
data-api-builder copied to clipboard
[Enhancement]: Pagination metadata
Proposal
Consider enhancing pagination to include page metadata?
Why?
Many APIs provide additional page information. All of this information is available to us.
Example: https://stapi.co/api/v2/rest/spacecraft/search
Example
{
"page": {
"pagingStrategy": "numeric|cursor" // `numeric` means $page, `cursor` means $after,
"pageNumber": 0, // null when pagingStrategy == cursor
"pageSize": 50, // reflects the global setting
"totalPages": 29, // totalElements / pageSize
"totalElements": 1443, // database count(*) with same filter
"firstPage": true, // pageNumber == 0
"lastPage": false // pageNumber == totalPages
}
}
Configuration
{
"runtime": {
"pagination": {
"max-page-size": 1000000,
"default-page-size": 100,
"include-metadata": <boolean; default: false>
}
}
}
Ad hoc request
$page-metadata
Syntax
https://server/api/entity?$page-metadata=true
Rules
$pagepresent |
$afterpresent |
$page-metadatavalue |
global setting |
Is metadata included in the response payload? |
|---|---|---|---|---|
| 🟩 | 🟥 | 🟩 true | n/a | included |
| 🟥 | 🟩 | 🟩 true | n/a | included |
| 🟥 | 🟥 | 🟩 true | n/a | - |
| n/a | n/a | 🟥 false | n/a | - |
| 🟩 | 🟩 | 🟨 missing | 🟩 true |
included |
| 🟩 | 🟩 | 🟨 missing | 🟩 true |
included |
| 🟥 | 🟩 | 🟨 missing | 🟩 true |
included |
| 🟥 | 🟥 | 🟨 missing | 🟩 true |
- |
| 🟩 | 🟩 | 🟨 missing | 🟥 false |
- |
| 🟩 | 🟥 | 🟨 missing | 🟥 false |
- |
| 🟥 | 🟩 | 🟨 missing | 🟥 false |
- |
| 🟥 | 🟥 | 🟨 missing | 🟥 false |
- |
As English
-
When the global setting is
true:- Metadata is not returned if all three (
$page,$after, and$page-metadata) are absent. - Metadata is returned if any of the following are true:
-
$pageis used. -
$afteris used. -
$page-metadata=trueis explicitly used.
-
- Metadata is not returned if all three (
-
When the global setting is
false:- Metadata is not returned if
$page-metadatais not used or is set tofalse. - Metadata is only returned if
$page-metadata=trueis explicitly used.
- Metadata is not returned if
-
Priority:
-
$page-metadatatakes precedence when explicitly set, overriding the absence of$pageor$after.
-
As C#
public static bool ShouldIncludeMetadata(
int? pageValue,
string? afterValue,
bool? includeMetadata,
bool globalSetting = false)
{
return includeMetadata switch
{
true when pageValue.HasValue => true,
true when !string.IsNullOrEmpty(afterValue) => true,
null when pageValue.HasValue => globalSetting,
null when !string.IsNullOrEmpty(afterValue) => globalSetting,
_ => false
};
}
query{
products(filter: color eq yellow && pageMetadata: true)
{
items {
Id
Color
Warehouse(pageMetadata: true) {
items {
Id
Location
}
}
}
}
}
Hi Jerry, This improvement hasn't been made yet, right?