cayleygraph/cayley · error

expand all cannot be used with other fields

Error message

expand all cannot be used with other fields

What it means

When a selection expands to all predicates (the AnyKey wildcard field), it must be the only selection in its set and carry no sub-fields. Combining the wildcard with other sibling fields is ambiguous and rejected.

Source

Thrown at query/graphql/graphql.go:456

		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")
				} else if len(fld.Has) != 0 || len(fld.Fields) != 0 {
					return nil, false, fmt.Errorf("filters inside expand all are not supported")
				}
				return nil, true, nil
			}
			out = append(out, fld)
		default:
			return nil, false, fmt.Errorf("unknown selection type: %T", s)
		}
	}
	return
}

func stringToVia(s string) (_ quad.IRI, rev bool) {
	if len(s) > 0 && s[0] == '~' {
		rev = true
		s = s[1:]
	}

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Remove sibling fields so the wildcard is the sole selection, or drop the wildcard and list fields explicitly
  2. Split into two queries: one with explicit fields, one using expand-all
  3. Replace `_` with the full list of predicates you want

Example fix

// before
q := "query { node { _ name } }"
// after
q := "query { node { _ } }" // or list explicit fields only
Defensive patterns

Strategy: validation

Validate before calling

function checkWildcardAlone(selections) {
  const hasWildcard = selections.some(s => s.name?.value === '_');
  if (hasWildcard && selections.length !== 1) throw new Error('expand all (_) must be the only field in its selection set');
}

Type guard

const isBareWildcard = (sel) => sel.length === 1 && sel[0].name?.value === '_';

Prevention

When it happens

Trigger: A selection set containing the expand-all field (`_`) plus at least one other field, e.g. `query { node { _ name } }` — setToFields detects len(set.Selections) != 1 when fld.Via == AnyKey.

Common situations: Adding a wildcard 'everything else' alongside named fields; generated queries that append `_` to normal projections; users assuming `_` means 'plus these fields'.

Understand the failure class

Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/2a2903e4a5bf3cf7. Report an issue: GitHub.