Querying nested array objects with block content in Sanity GraphQL

5 replies
Last updated: Jan 28, 2026
I'm trying to query some new fields added to my documents. These fields are an array of objects with a nested content block.
My initial approach was something like this:

// templates/project.js

export const query = graphql`
  query ProjectTemplateQuery($id: String!) {
    sampleProject: sanitySampleProject(id: { eq: $id }) {
...
      projectSummary {
        userSummary {
          _rawUserContent
        }
        problemSummary {
          _rawProblemContent
        }
        solutionSummary {
          _rawSolutionContent
        }
        tldr {
          _rawTldrContent
        }
      }
...
    }
  }
`;
But that results in this error:

Cannot query field "userSummary" on type "SanityProblemSummaryOrSolutionSummaryOrTldrOrUserSummary".
With this description
There was an error in your GraphQL query:

Cannot query field "userSummary" on type "SanityProblemSummaryOrSolutionSummaryOrTldrOrUserSummary".

...

It is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned "dummy content". Visit our docs to learn how you can define the schema for "SanityProblemSummaryOrSolutionSummaryOrTldrOrUserSummary":

I never defined a field with that type, but is that an autogenerated type to hold whatever objects get propagated into the array? Does anyone have any tips for how I can properly query the content? I've tried exploring it in the GraphQL playground but don't know how to query those objects with block content fields (I'm also a bit of a graphQL noob so please be nice)
AI Update

This is a common issue when working with Gatsby and Sanity's GraphQL API! The error you're seeing happens because when you have an array of different object types in Sanity, Gatsby creates a union type to represent all possible objects that could be in that array.

That long type name SanityProblemSummaryOrSolutionSummaryOrTldrOrUserSummary is indeed auto-generated by Gatsby - it's a GraphQL union type that represents "this could be a userSummary OR a problemSummary OR a solutionSummary OR a tldr object."

The solution is to use inline fragments to query each type separately:

query ProjectTemplateQuery($id: String!) {
  sampleProject: sanitySampleProject(id: { eq: $id }) {
    projectSummary {
      ... on SanityUserSummary {
        _rawUserContent
      }
      ... on SanityProblemSummary {
        _rawProblemContent
      }
      ... on SanitySolutionSummary {
        _rawSolutionContent
      }
      ... on SanityTldr {
        _rawTldrContent
      }
    }
  }
}

The ... on SanityTypeName syntax tells GraphQL "when the object is of this specific type, query these fields."

To find the exact type names, use the GraphQL playground (usually at http://localhost:8000/___graphql in development) and explore the schema. Look for types that start with Sanity and match your object type names from your Sanity schema.

Alternative approach: If you want to avoid this complexity, you could restructure your Sanity schema to use a single object type with optional fields instead of an array of different object types. However, inline fragments are the standard GraphQL pattern for querying union types, so the approach above is perfectly valid.

The gatsby-source-sanity plugin automatically generates these GraphQL types from your Sanity schema, and the _raw fields you're using are special fields unique to the Gatsby integration that provide the raw Portable Text data from Sanity.

Show original thread
5 replies

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?