If you have modular-content fields you can use GraphQL fragments to fetch the different blocks.
Suppose a blog_post
model has a modular content field called content
, which in turn accepts the following blocks:
Block blog_post_text_block
: made of a text
field (multi-paragraph text);
Block blog_post_quote_block
: made of a quote
field (multi-paragraph text) and author
field (single-line string);
Block blog_post_gallery_block
: made of a gallery
field (image gallery);
This GraphQL query will do the work:
query {allBlogPosts {titlecontent {... on BlogPostTextBlockRecord {id_modelApiKeytext}... on BlogPostQuoteBlockRecord {id_modelApiKeyquoteauthor}... on BlogPostGalleryBlockRecord {id_modelApiKeygallery { url }}}}}
Since all records implement the GraphQL interface RecordInterface
, you can dry up the same query like this:
query {allBlogPosts {titlecontent {... on RecordInterface {id_modelApiKey}... on BlogPostTextBlockRecord {text}... on BlogPostQuoteBlockRecord {quoteauthor}... on BlogPostGalleryBlockRecord {gallery { url }}}}}