Retrieve linked document content
Here we will explore how you can retrieve content from a Content Relationship / linked document.
By default, the GraphQL API will let you easily get content from a Content Relationship / linked document. In most cases, you will have multiple document types that might be linked. For example, you might be able to link to either a "blog" type or a "page" type, which would have different schemas (data structures).
To handle this, you will have to use a union type to define which fields you want to retrieve depending on the type of the document linked.
The following example shows how to get the data from a linked document.
In this case we're querying all "blog_post" documents. The blog posts have a "link_field" Content Relationship field where you can link to another "blog_post" or to a "page" document.
If the linked document is a "blog_post", then we will retrieve the "title" and "subtitle" fields. If the linked document is a "page", then we will retrieve the "main_title" and its UID value.
query{
allBlog_posts{
edges{
node{
link_field{
... on Blog_post{
title
subtitle
}
... on Page{
main_title
_meta{
uid
}
}
__typename
}
}
}
}
}
Note that even if the Content Relationship field only links to one type of document, you will need to specify the union type.
Get multiple levels of linked documents
Depending on your query, you will be able to query multiple levels of linked documents. We limited the depth level of the graph for your query to 9 to give plenty of room to query as deep as needed for the content you need.