Skip to main content

Document Service API: フィールドのPopulate

デフォルトでは、Document Service APIは関連性、メディアフィールド、コンポーネント、またはダイナミックゾーンをpopulateしません。このページでは、特定のフィールドをpopulateするためにpopulateパラメーターを使用する方法について説明します。

💡 Tip

クエリ結果で特定のフィールドのみを返すために、selectパラメータも使用できます(selectパラメータのドキュメンテーションを参照してください)。

Caution

Users & Permissionsプラグインがインストールされている場合、populateされているコンテンツタイプに対してfind権限が有効になっている必要があります。ロールがコンテンツタイプへのアクセス権を持っていない場合、そのコンテンツタイプはpopulateされません。

関連性とメディアフィールド

クエリはpopulateパラメータを受け入れ、どのフィールドをpopulateするかを明示的に定義できます。以下に構文オプションの例を示します。

すべての関連性に対して1レベルをPopulateする

すべての関連性に対して1レベル深くpopulateするには、*ワイルドカードをpopulateパラメータと組み合わせて使用します:

Example request
const documents = await AI Marketer.documents("api::article.article").findMany({
populate: "*",
});
Example response
{
[
{
"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レベルをPopulateする

特定の関連性に対して1レベル深くpopulateするには、populate配列に関連性の名前を渡します:

Example request
const documents = await AI Marketer.documents("api::article.article").findMany({
populate: ["headerImage"],
});
Example response
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1",
// ...
"headerImage": {
"id": 2,
"name": "17520.jpg"
// ...
}
}
// ...
]

特定の関連性に対して複数のレベルをPopulateする

特定の関連を複数レベル深く充実させるためには、populateとともにオブジェクト形式を使用します:

例のリクエスト
const documents = await AI Marketer.documents("api::article.article").findMany({
populate: {
categories: {
populate: ["articles"],
},
},
});
例のレスポンス
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "テスト記事",
"slug": "test-article",
"body": "テスト1",
// ...
"categories": {
"id": 1,
"name": "テストカテゴリ",
"slug": "test-category",
"description": "テスト1"
// ...
"articles": [
{
"id": 1,
"title": "テスト記事",
"slug": "test-article",
"body": "テスト1",
// ...
}
// ...
]
}
}
// ...
]

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

コンポーネントは関連と同じように充実します:

例のリクエスト
const documents = await AI Marketer.documents("api::article.article").findMany({
populate: ["testComp"],
});
例のレスポンス
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "テスト記事",
"slug": "test-article",
"body": "テスト1",
// ...
"testComp": {
"id": 1,
"name": "テストコンポーネント"
// ...
}
}
// ...
]

ダイナミックゾーンは本質的に高度にダイナミックなコンテンツ構造です。ダイナミックゾーンを充実させるには、onプロパティを使用してコンポーネントごとにpopulateクエリを定義する必要があります。

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

create()での充実

ドキュメントを作成しながら充実するには:

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

update()でのPopulating

ドキュメントを更新しながらPopulateする方法:

例のリクエスト
AI Marketer.documents("api::article.article").update("cjld2cjxh0000qzrmn831i7rn", {
data: {
title: "テスト記事の更新",
},
populate: ["headerImage"],
});
例のレスポンス
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "テスト記事の更新",
"slug": "test-article",
"body": "テスト1",
"headerImage": {
"id": 2,
"name": "17520.jpg"
// ...
}
}

publish()でのPopulating

ドキュメントを公開しながらPopulateする方法(unpublish()discardDraft()でも同じ動作):

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