From 46baebb4a54da6fdd61855edd463bef819ffc3fe Mon Sep 17 00:00:00 2001 From: "dev-docs-github-app-dev[bot]" <178211755+dev-docs-github-app-dev[bot]@users.noreply.github.com> Date: Mon, 5 May 2025 19:42:07 +0000 Subject: [PATCH] Update 1 files --- .../docs/cms/api/graphql/guides/populate.md | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 docusaurus/docs/cms/api/graphql/guides/populate.md diff --git a/docusaurus/docs/cms/api/graphql/guides/populate.md b/docusaurus/docs/cms/api/graphql/guides/populate.md new file mode 100644 index 0000000000..f7d6c0b8f4 --- /dev/null +++ b/docusaurus/docs/cms/api/graphql/guides/populate.md @@ -0,0 +1,128 @@ +# Populate Relations with GraphQL API + +The GraphQL API in Strapi allows you to fetch related data along with your main query. This guide explains how to use the `populate` parameter to include related content in your GraphQL queries. + +## Basic Usage + +When querying a content type, you can use the `populate` parameter to include related fields. The `populate` parameter accepts a string or an array of strings representing the relation fields you want to include. + +```graphql +query { + restaurants { + data { + id + attributes { + name + description + categories { + data { + id + attributes { + name + } + } + } + } + } + } +} +``` + +In this example, the `categories` field is automatically populated because it's included in the query. + +## Deep Population + +For nested relations, you can use dot notation to populate fields deeper in the relation chain. + +```graphql +query { + restaurants { + data { + id + attributes { + name + categories { + data { + id + attributes { + name + subcategories { + data { + id + attributes { + name + } + } + } + } + } + } + } + } + } +} +``` + +This query will populate the `categories` field for restaurants and the `subcategories` field for each category. + +## Populate Media Fields + +Media fields can be populated just like other relations: + +```graphql +query { + restaurants { + data { + id + attributes { + name + cover { + data { + id + attributes { + url + alternativeText + } + } + } + } + } + } +} +``` + +## Populate Dynamic Zones + +For dynamic zones, you need to use fragments to specify which fields to return for each possible component: + +```graphql +query { + articles { + data { + id + attributes { + title + content { + __typename + ... on ComponentTextText { + text + } + ... on ComponentMediaImage { + image { + data { + attributes { + url + } + } + } + } + } + } + } + } +} +``` + +Remember that the GraphQL API automatically includes the fields you request in your query, so explicit use of a `populate` parameter is not necessary. Simply including the relation fields in your query structure will populate them in the response. + +By using these population techniques, you can efficiently retrieve related data in a single GraphQL query, reducing the number of API calls needed and improving your application's performance. \ No newline at end of file