REST API: ポピュレートとフィールド選択
Page summary:
populateパラメータで、リレーション、メディア、コンポーネント、ダイナミックゾーンを REST API のレスポンスに含めます。fieldsパラメータで、返すフィールドだけに絞ります。
REST API は既定ではリレーション、メディア、コンポーネント、ダイナミックゾーンをポピュレートしません。含めたいフィールドには populate パラメータ を使います。クエリ結果に特定のフィールドだけを返すには fields パラメータ を使います。
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.
フィールド選択
クエリでは fields で返すフィールドを限定できます。既定では REST API は次の フィールド型 のみ返します。
- 文字列系:
string、text、richtext、enumeration、email、password、uid - 日付・時刻系:
date、time、datetime、timestamp - 数値系:
integer、biginteger、float、decimal - 汎用:
boolean、array、JSON
| 用途 | パラメータの例 |
|---|---|
| 1 フィールドだけ | fields=name |
| 複数フィールド | fields[0]=name&fields[1]=description |
フィールド選択は、リレーション、メディア、コンポーネント、ダイナミックゾーンには使えません。これらを含めるには populate パラメータ を使います。
GET /api/restaurants?fields[0]=name&fields[1]=description
JavaScript のクエリ(qs ライブラリで組み立て)
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(
{
fields: ['name', 'description'],
},
{
encodeValuesOnly: true, // prettify URL
}
);
await request(`/api/users?${query}`);
{
"data": [
{
"id": 4,
"Name": "Pizzeria Arrivederci",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "Specialized in pizza, we invite you to rediscover our classics, such as 4 Formaggi or Calzone, and our original creations such as Do Luigi or Nduja."
}
]
}
],
"documentId": "lr5wju2og49bf820kj9kz8c3"
},
// …
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 25,
"pageCount": 1,
"total": 4
}
}
}
ポピュレート
REST API は既定ではこれらの型をポピュレートしないため、populate を渡さない限りリレーション、メディア、コンポーネント、ダイナミックゾーンは含まれません。ポピュレートしたリレーションは常にオブジェクト全体が返り、REST API では ID の配列だけを返すことはできません。
ポピュレートするコンテンツタイプには find 権限が必要です。ロールにアクセスがないコンテンツタイプはポピュレートされません(find の付与方法は Users & Permissions を参照)。
populate は単独 で使うほか、複数のオペレータと組み合わせて細かく制御できます。
クエリ文字列に並ぶ長い populate リスト(populate[0]、populate[1]、…)は、クエリパーサの arrayLimit(既定: 100)で上限があります。より長くするには strapi::query ミドルウェア の arrayLimit を上げます。値を大きくするとリクエストあたりのパース負荷が増えます。
次の表は代表的な populate の書式です。各行の詳細は Understanding populate ガイドへリンクしています。
| 用途 | パラメータの例 | 詳しい説明 |
|---|---|---|
| メディア・リレーション・コンポーネント・ダイナミックゾーンを含め、すべて 1 階層だけ | populate=* | すべてのリレーションとフィールドを 1 階層だけポピュレート |
| 1 つのリレーションを 1 階層だけ | populate=a-relation-name | 特定のリレーションを 1 階層だけ |
| 複数のリレーションを 1 階層だけ | populate[0]=relation-name&populate[1]=another-relation-name&populate[2]=yet-another-relation-name | 特定のリレーションを 1 階層だけ |
| 一部のリレーションを複数階層 | populate[root-relation-name][populate][0]=nested-relation-name | 特定のリレーションを複数階層 |
| コンポーネントをポピュレート | populate[0]=component-name | コンポーネントのポピュレート |
| コンポーネントと、そのネストしたコンポーネント | populate[0]=component-name&populate[1]=component-name.nested-component-name | コンポーネントのポピュレート |
| ダイナミックゾーン(第 1 階層の要素のみ) | populate[0]=dynamic-zone-name | ダイナミックゾーンのポピュレート |
ダイナミックゾーンとネスト・リレーションを、on で明示した戦略でポピュレート | populate[dynamic-zone-name][on][component-category.component-name][populate][relation-name][populate][0]=field-name | ダイナミックゾーンのポピュレート |
階層の深い populate は インタラクティブなクエリビルダー で組み立てるとよいです。例と説明は REST API ガイド も参照してください。
ポピュレートと他オペレータの併用
populate は、フィールド選択、フィルター、ソート などと組み合わせられます。
ポピュレートとページネーションのオペレータは同時に使えません。
フィールド選択と併用
fields と populate は一緒に使えます。
GET /api/articles?fields[0]=title&fields[1]=slug&populate[headerImage][fields][0]=name&populate[headerImage][fields][1]=url
JavaScript のクエリ(qs ライブラリで組み立て)
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(
{
fields: ['title', 'slug'],
populate: {
headerImage: {
fields: ['name', 'url'],
},
},
},
{
encodeValuesOnly: true, // prettify URL
}
);
await request(`/api/articles?${query}`);