diff --git a/docs/reference/api/introduction.md b/docs/reference/api/introduction.md index 90b879b448..0ac0131cbf 100644 --- a/docs/reference/api/introduction.md +++ b/docs/reference/api/introduction.md @@ -78,7 +78,7 @@ nullifying the field: } ``` -#### One-to-Many (/ Many-to-Many / Many-to-Any) +#### One-to-Many (/ Many-to-Many) One-to-Many, and therefore Many-to-Many and Many-to-Any, relationships can be updated in one of two ways: @@ -145,3 +145,70 @@ foreign key. This means that your data will never suddenly disappear, but it als orphaned items. ::: + +#### Many-to-Any (Union Types) + +Many-to-Any fields work very similar to a "regular" many-to-many, with the exception that the related field can pull in +the fields from any of the related collections, for example: + +```json +{ + "sections": [ + { + "collection": "headings", + "item": { + /* headings fields */ + } + }, + { + "collection": "paragraphs", + "item": { + /* paragraphs fields */ + } + } + ] +} +``` + +##### REST API + +To scope the fields that are returned per collection type, you can use the `:` syntax in the fields +parameter as follows: + +``` +GET /items/pages + ?fields[]=sections.item:headings.id + &fields[]=sections.item:headings.title + &fields[]=sections.item:paragraphs.body + &fields[]=sections.item:paragraphs.background_color +``` + +##### GraphQL + +In GraphQL, you can use nested fragments on the Union Type to select the fields: + +```graphql +query { + pages { + sections { + item { + ... on headings { + id + title + } + + ... on paragraphs { + body + background_color + } + } + } + } +} +``` + +::: tip Updating + +Updating records in a many-to-any is identical to the other relationship types. + +::: diff --git a/docs/reference/api/items.md b/docs/reference/api/items.md index 1c890141e1..79ad7920e8 100644 --- a/docs/reference/api/items.md +++ b/docs/reference/api/items.md @@ -74,10 +74,6 @@ will be an empty array. If your collection is a singleton, this endpoint will return the item. -#### Example for M2M relation - -Let's imagine each article can be related to one or multiple `themes`. -
@@ -115,27 +111,6 @@ query { } ``` -##### Example with themes related to an article with a M2M relation - -```graphql -query { - articles { - id - title - author { - first_name - } - themes { - item { - ... on them { - label - } - } - } - } -} -``` -