GraphQL "Field X must have a selection of subfields" at runtime in CI
GraphQL requires every field that returns an object, interface, or union type to specify which sub-fields to return. The validator rejects a bare selection of such a field with "must have a selection of subfields".
What this error means
A request fails validation with "Field \"x\" of type \"Y\" must have a selection of subfields. Did you mean \"x { ... }\"?" and a 400 GRAPHQL_VALIDATION_FAILED response.
{
"errors": [{
"message": "Field \"author\" of type \"User\" must have a selection of subfields. Did you mean \"author { ... }\"?",
"extensions": { "code": "GRAPHQL_VALIDATION_FAILED" }
}]
}Common causes
An object field selected without braces
The query lists a field that returns a composite type but does not open a { ... } block to choose its scalar sub-fields.
A scalar turned into an object type
A field that used to be a scalar is now an object in the new schema, so existing queries that selected it directly become invalid.
How to fix it
Add the sub-selection
- Find the field named in the error in your operation.
- Open a
{ ... }block and select the scalar sub-fields you need. - Re-run the request against the server.
query {
post(id: 1) {
author { id name } # author returns User, so it needs subfields
}
}Catch it before runtime
Validate operations against the SDL in CI so a missing sub-selection fails the lint step, not a live request.
npx graphql-validate --schema schema.graphql 'src/**/*.graphql'How to prevent it
- Validate every committed operation against the schema in CI.
- When a scalar becomes an object, update all selections in the same change.
- Generate typed operations so the build flags bare object selections.