GraphQL query for Modular Content field type

I’m trying to construct a GraphQL query which will return data outputted from a Modular Content field type. The problem is that I don’t know in advance what kind of content might appear there, as content authors can choose from different Blocks to add.

I’ve set up a fragment for each of the Block types, and I understand that if statements can be used in GraphQL, but I’m struggling with the syntax. Could someone point me in the right direction?

1 Like

I think you have to add all the possible blocks into your query. Only used blocks will be returned, and the frontend will have to know how to render each type based on its _modelApiKey or __typename.

You can use a switch statement to deal with the different kinds of possible blocks, like in this example for Structured Text (not the same thing as Modular Content, but the frontend switch logic should be similar):

switch (record.__typename) {
            case 'ImageRecord':
              return <Image data={record.image.responsiveImage} />;
            default:
              return null;
          }
2 Likes