REST API: 並び替えとページネーション
REST APIへのクエリによって返されるエントリは、並び替えやページネーションが可能です。
Strapi takes advantage of the ability of the qs library to parse nested objects to create more complex queries.
Use qs directly to generate complex queries instead of creating them manually. Examples in this documentation showcase how you can use qs.
You can also use the interactive query builder if you prefer playing with our online tool instead of generating queries with qs on your machine.
並び替え
クエリは、以下の構文で1つまたは複数のフィールドを並び替えることを許可するsortパラメータを受け入れることができます:
GET /api/:pluralApiId?sort=valueで1つのフィールドを並び替えるGET /api/:pluralApiId?sort[0]=value1&sort[1]=value2で複数のフィールドを並び替える(例:2つのフィールド)
並び替えの順序は以下のように定義できます:
:ascで昇順(デフォルトの順序、省略可能)- または
:descで降順。
例:2つのフィールドを使用して並び替える
sort配列にフィールドを渡すことで、複数のフィールドで並び替えることができます。
GET /api/restaurants?sort[0]=Description&sort[1]=Name
{
"data": [
{
"id": 9,
"documentId": "hgv1vny5cebq2l3czil1rpb3",
"Name": "BMK Paris Bamako",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
{
"id": 8,
"documentId": "flzc8qrarj19ee0luix8knxn",
"Name": "Restaurant D",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
// …
],
"meta": {
// …
}
}
JavaScript query (built with the qs library):
The query URL above was built using the qs library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
sort: ['Description', 'Name'],
}, {
encodeValuesOnly: true, // prettify URL
});
await request(`/api/restaurants?${query}`);
例:2つのフィールドを使用して並び替え、順序を設定する
sortパラメータを使用し、並び替えたフィールドに:ascまたは:descを定義することで、特定の順序で結果を並び替えることができます。
GET /api/restaurants?sort[0]=Description:asc&sort[1]=Name:desc
{
"data": [
{
"id": 8,
"documentId": "flzc8qrarj19ee0luix8knxn",
"Name": "レストランD",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "ここに非常に短い説明が入ります。"
}
]
}
],
// …
},
{
"id": 9,
"documentId": "hgv1vny5cebq2l3czil1rpb3",
"Name": "BMK Paris Bamako",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "ここに非常に短い説明が入ります。"
}
]
}
],
// …
},
// …
],
"meta": {
// …
}
}
JavaScript query (built with the qs library):
The query URL above was built using the qs library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
sort: ['Description:asc', 'Name:desc'],
}, {
encodeValuesOnly: true, // URLをきれいにする
});
await request(`/api/restaurants?${query}`);
ページネーション
クエリはpaginationパラメータを受け入れることができます。結果はページネーションできます:
ページネーション方法は混在できません。常にpageとpageSize、または startとlimitを使用してください。
ページごとのページネーション
ページごとに結果をページネーションするには、以下のパラメータを使用します:
| パラメータ | タイプ | 説明 | デフォルト |
|---|---|---|---|
pagination[page] | 整数 | ページ番号 | 1 |
pagination[pageSize] | 整数 | ページサイズ | 25 |
pagination[withCount] | ブール | 応答にエントリの総数とページ数を追加します | True |
GET /api/articles?pagination[page]=1&pagination[pageSize]=10
{
"data": [
// ...
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 10,
"pageCount": 5,
"total": 48
}
}
}
JavaScript query (built with the qs library):
The query URL above was built using the qs library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
pagination: {
page: 1,
pageSize: 10,
},
}, {
encodeValuesOnly: true, // URLをきれいにする
});
await request(`/api/articles?${query}`);
オフセットによるページネーション
オフセットによる結果のページネーションには、以下のパラメーターを使用します:
| パラメーター | タイプ | 説明 | デフォルト |
|---|---|---|---|
pagination[start] | 整数 | 開始値(つまり、最初に返すエントリー) | 0 |
pagination[limit] | 整数 | 返すエントリーの数 | 25 |
pagination[withCount] | ブール値 | レスポンスにエントリーの総数を表示するかどうかを切り替えます | true |
pagination[limit]のデフォルト値と最大値は、./config/api.jsファイルのapi.rest.defaultLimitとapi.rest.maxLimitキーで設定できます。
GET /api/articles?pagination[start]=0&pagination[limit]=10
{
"data": [
// ...
],
"meta": {
"pagination": {
"start": 0,
"limit": 10,
"total": 42
}
}
}
JavaScript query (built with the qs library):
The query URL above was built using the qs library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
pagination: {
start: 0,
limit: 10,
},
}, {
encodeValuesOnly: true, // URLを綺麗にする
});
await request(`/api/articles?${query}`);