cayleygraph/cayley · error
expand all is not supported at top level
Error message
expand all is not supported at top level
What it means
The expand-all shorthand (the AnyKey wildcard field, typically `_`) selects all predicates, but this library forbids using it at the top level of a query. Top-level selections must be explicit fields; expand all is only allowed nested inside them.
Source
Thrown at query/graphql/graphql.go:438
}
doc, err := parser.Parse(parser.ParseParams{Source: string(data)})
if err != nil {
return nil, err
}
if len(doc.Definitions) != 1 {
return nil, fmt.Errorf("unsupported query type")
}
def, ok := doc.Definitions[0].(*ast.OperationDefinition)
if !ok {
return nil, fmt.Errorf("unsupported query type: %T", doc.Definitions[0])
} else if def.Operation != "query" {
return nil, fmt.Errorf("unsupported operation: %s", def.Operation)
}
fields, all, err := setToFields(def.SelectionSet, nil)
if err != nil {
return nil, err
} else if all {
return nil, fmt.Errorf("expand all is not supported at top level")
}
return &Query{fields: fields}, nil
}
func setToFields(set *ast.SelectionSet, labels []quad.Value) (out []field, all bool, _ error) {
if set == nil {
return
}
for _, s := range set.Selections {
switch sel := s.(type) {
case *ast.Field:
fld, err := convField(sel, labels)
if err != nil {
return nil, false, err
}
if fld.Via == quad.IRI(AnyKey) {
if len(set.Selections) != 1 {
return nil, false, fmt.Errorf("expand all cannot be used with other fields")View on GitHub (pinned to 81dcd7d73e)
Solutions
- Name the starting nodes explicitly at the top level and use expand-all (`_`) only in nested selections
- Replace the wildcard with the specific predicates you need
- Enumerate root nodes with a query (e.g. via has/id) before expanding
Example fix
// before
q := "query { _ }"
// after
q := "query { node { _ } }" Defensive patterns
Strategy: validation
Validate before calling
const WILDCARD = '_';
function topLevelHasExpandAll(doc) {
const sel = doc.definitions[0]?.selectionSet?.selections || [];
return sel.some(s => s.name?.value === WILDCARD);
} Type guard
const isExpandAllSelection = (s) => s?.name?.value === '_';
Prevention
- Use `_` only in nested selections
- Always anchor queries with explicit root fields
- Review generated queries for root-level wildcards
When it happens
Trigger: A top-level selection set in the query operation contains the expand-all wildcard field (AnyKey) as its only selection, causing setToFields to return all=true at the root.
Common situations: Queries like `query { _ }` intending to dump all predicates of every node; users familiar with `_` expansion on nested nodes applying it at the root; exploratory queries generated by tooling.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- expand all cannot be used with other fields
- unsupported query type
- unsupported operation: %s
- filters inside expand all are not supported
- Datastore: invalid action
AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06).
Data as JSON: /api/errors/51bd3dab260b8a1e.
Report an issue: GitHub.