メインコンテンツまでスキップ

Document Service API: フィールドのポピュレート

既定では、Document Service API はリレーション、メディア、コンポーネント、ダイナミックゾーンをいずれもポピュレートしません。このページでは、populate で特定のフィールドをポピュレートする方法を説明します。

Tip

返すフィールドだけを絞るには fields も使えます(fields パラメータ のドキュメントを参照)。

Caution

Users & Permissions プラグインが有効な場合、ポピュレート先のコンテンツタイプに対して find 権限が必要です。ロールがそのコンテンツタイプにアクセスできない場合、ポピュレートされません。

リレーションとメディア

クエリでは populate で、ポピュレートするフィールドを次のような形で指定します。

すべてのリレーションを 1 階層だけ

すべてのリレーションを 1 階層だけポピュレートするには、populate にワイルドカード * を使います。

リクエスト例
const documents = await strapi.documents("api::article.article").findMany({
populate: "*",
});
レスポンス例
{
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1",
// ...
"headerImage": {
"data": {
"id": 1,
"attributes": {
"name": "17520.jpg",
"alternativeText": "17520.jpg",
"formats": {
// ...
}
// ...
}
}
},
"author": {
// ...
},
"categories": {
// ...
}
}
// ...
]
}

特定のリレーションだけ 1 階層

特定のリレーションだけを 1 階層ポピュレートするには、リレーション名を populate の配列で渡します。

リクエスト例
const documents = await strapi.documents("api::article.article").findMany({
populate: ["headerImage"],
});
レスポンス例
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1",
// ...
"headerImage": {
"id": 2,
"name": "17520.jpg"
// ...
}
}
// ...
]

特定のリレーションを複数階層

特定のリレーションを複数階層ポピュレートするには、オブジェクト形式の populate を使います。

リクエスト例
const documents = await strapi.documents("api::article.article").findMany({
populate: {
categories: {
populate: ["articles"],
},
},
});
レスポンス例
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1",
// ...
"categories": {
"id": 1,
"name": "Test Category",
"slug": "test-category",
"description": "Test 1"
// ...
"articles": [
{
"id": 1,
"title": "Test Article",
"slug": "test-article",
"body": "Test 1",
// ...
}
// ...
]
}
}
// ...
]

コンポーネントとダイナミックゾーン

コンポーネントはリレーションと同じ要領でポピュレートします。

リクエスト例
const documents = await strapi.documents("api::article.article").findMany({
populate: ["testComp"],
});
レスポンス例
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1",
// ...
"testComp": {
"id": 1,
"name": "Test Component"
// ...
}
}
// ...
]

ダイナミックゾーンは構造が可変です。ポピュレートするには on でコンポーネントごとにクエリを定義します。

リクエスト例
const documents = await strapi.documents("api::article.article").findMany({
populate: {
testDZ: {
on: {
"test.test-compo": {
fields: ["testString"],
populate: ["testNestedCompo"],
},
},
},
},
});
レスポンス例
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1",
// ...
"testDZ": [
{
"id": 3,
"__component": "test.test-compo",
"testString": "test1",
"testNestedCompo": {
"id": 3,
"testNestedString": "testNested1"
}
}
]
}
// ...
]

create() 時のポピュレート

作成レスポンスにポピュレート結果を含める例です。

リクエスト例
await strapi.documents("api::article.article").create({
data: {
title: "Test Article",
slug: "test-article",
body: "Test 1",
headerImage: 2,
},
populate: ["headerImage"],
});
レスポンス例
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1",
"headerImage": {
"id": 2,
"name": "17520.jpg"
// ...
}
}

update() 時のポピュレート

更新レスポンスにポピュレート結果を含める例です。

リクエスト例
await strapi.documents("api::article.article").update({
documentId: "cjld2cjxh0000qzrmn831i7rn",
data: {
title: "Test Article Update",
},
populate: ["headerImage"],
});
レスポンス例
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article Update",
"slug": "test-article",
"body": "Test 1",
"headerImage": {
"id": 2,
"name": "17520.jpg"
// ...
}
}

publish() 時のポピュレート

公開時のレスポンスにポピュレート結果を含めます(unpublish()discardDraft() も同様の動きです)。

リクエスト例
await strapi.documents("api::article.article").publish({
documentId: "cjld2cjxh0000qzrmn831i7rn",
populate: ["headerImage"],
});
レスポンス例
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"versions": [
{
"id": "cjld2cjxh0001qzrm1q1i7rn",
"locale": "en",
// ...
"headerImage": {
"id": 2,
"name": "17520.jpg"
// ...
}
}
]
}