GraphQL Error: Cannot query field "userSummary"
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.
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.