Skip to content
Latchkey

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.

Apollo Server
{
  "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

  1. Find the field named in the error in your operation.
  2. Open a { ... } block and select the scalar sub-fields you need.
  3. Re-run the request against the server.
GraphQL
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.

Terminal
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.

Frequently asked questions

What causes ""must have a selection of subfields""?
The query lists a field that returns a composite type but does not open a { ... } block to choose its scalar sub-fields.
How do I fix "must have a selection of subfields"?
Add the sub-selection

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card