Structured text fields
If you have Structured Text fields you can use GraphQL fragments to fetch the different blocks.
Suppose a blog_post model has a Structured Text field called content, which in turn accepts links to other blog posts and the following embedded blocks:
Block
cta_block: with alabelandurlfields (both Single-line text)Block
carousel_block: with an Asset Gallery field calledgalleryBlock
mention_block: with a Single-line text field calledusername
This GraphQL query will return all the data needed to render it:
query { allBlogPosts { title content { value blocks { __typename ... on RecordInterface { id } ... on CtaBlockRecord { label url } ... on CarouselBlockRecord { gallery { url } } } inlineBlocks { __typename ... on RecordInterface { id } ... on MentionBlockRecord { username } } links { __typename ... on RecordInterface { id } ... on BlogPostRecord { slug title } } } }}Rendering Structured Text content
You can then use the result of this query with one of the following libraries to render the result as HTML:
datocms-structured-text-to-plain-textto render it as plain text;datocms-structured-text-to-html-stringto render it as an HTML string;datocms-structured-text-to-dom-nodesto transform it in a list of DOM nodes;
We also have ready-made components for the most popular frontend frameworks:
Filtering records by contained blocks
If you need to filter records based on the content within their embedded blocks, please refer to the Deep filtering section of this guide, where this scenario is explained in detail.