dgraph-io/dgraph · error

Operation name must by supplied when query has more than 1 o

Error message

Operation name must by supplied when query has more than 1 operation.

What it means

When the parsed document contains more than one operation, GraphQL requires the client to say which one to execute via the operationName. This error is returned when the request has multiple operations but req.OperationName is empty.

Source

Thrown at graphql/schema/request.go:64

	doc, gqlErr := parser.ParseQuery(&ast.Source{Input: req.Query})
	if gqlErr != nil {
		return nil, gqlErr
	}

	listErr := validator.Validate(s.schema, doc, req.Variables)
	if len(listErr) != 0 {
		return nil, listErr
	}

	if len(doc.Operations) == 1 && doc.Operations[0].Operation == ast.Subscription &&
		s.schema.Subscription == nil {
		return nil, errors.Errorf("Not resolving subscription because schema doesn't have any " +
			"fields defined for subscription operation.")
	}

	if len(doc.Operations) > 1 && req.OperationName == "" {
		return nil, errors.Errorf("Operation name must by supplied when query has more " +
			"than 1 operation.")
	}

	op := doc.Operations.ForName(req.OperationName)
	if op == nil {
		return nil, errors.Errorf("Supplied operation name %s isn't present in the request.",
			req.OperationName)
	}

	vars, gqlErr := validator.VariableValues(s.schema, op, req.Variables)
	if gqlErr != nil {
		return nil, gqlErr
	}

	operation := &operation{op: op,
		vars:                    vars,
		query:                   req.Query,
		header:                  req.Header,

View on GitHub (pinned to 759e242be6)

Solutions

  1. Set req.OperationName (or the JSON operationName field) to the name of the operation to execute.
  2. Split the document into one operation per request.
  3. Give the operation a name and pass that name explicitly.

Example fix

// before
{"query": "query A { me { id } } query B { users { id } }"}
// after
{"query": "query A { me { id } } query B { users { id } }", "operationName": "A"}
Defensive patterns

Strategy: validation

Validate before calling

// client-side: require operationName when the document has >1 operation
if namedOperationCount(query) > 1 && operationName == "" {
    return errors.New("operationName is required for multi-operation documents")
}

Type guard

func needsOperationName(query, opName string) bool {
    return namedOperationCount(query) > 1 && opName == ""
}

Try / catch

op, err := sch.Operation(req)
if err != nil {
    if strings.Contains(err.Error(), "Operation name must by supplied") {
        http.Error(w, `please provide operationName`, http.StatusBadRequest)
        return
    }
    return err
}

Prevention

When it happens

Trigger: Posting a document like `query A {...} query B {...}` (or query+subscription) in one request without setting Request.OperationName.

Common situations: Clients bundling multiple operations in one document; tools like GraphiQL sending the whole editor content; missing operationName in the JSON payload {query, operationName}.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/f15dba0bcdaff2cc. Report an issue: GitHub.