graphql-go / graphql-go/graphql

Only returning first error when querying with missing variables

Open
#385 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
10.1k
Forks
845
PR merge metrics
No merged PRs in 30d

Description

Not sure if this is a bug or if the behavior is expected. Either way: If a query expects multiple variables, it will return a list of all missing variables when queried like this `query { myquery }`.
However, it will fail on first variable and return only one error if queried like this `query ($vara: String!, $varb: String! ) { myquery( vara: $vara, varb: $varb) }`.

The issue can be reproduced with the code below:

```
package main

import (
"fmt"

"github.com/graphql-go/graphql"
)

var args = graphql.FieldConfigArgument{
"vara": &graphql.ArgumentConfig{
Description: "variable a",
Type: graphql.NewNonNull(graphql.String),
},
"varb": &graphql.ArgumentConfig{
Description: "variable b",
Type: graphql.NewNonNull(graphql.String),
},
}

var schema, _ = graphql.NewSchema(
graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"myquery": &graphql.Field{
Type: graphql.String,
Resolve: func(params graphql.ResolveParams) (interface{}, error) { return "", nil },
Args: args,
},
},
}),
},
)

func main() {
query := `query { myquery }`
r := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
VariableValues: map[string]interface{}{},
})
fmt.Printf("Query: %s\nNo Errors: %d\nErrors: %s\n\n", query, len(r.Errors), r.Errors)
// Query: query { myquery }
// No Errors: 2
// Errors: [Field "myquery" argument "vara" of type "String!" is required but not provided.
// Field "myquery" argument "varb" of type "String!" is required but not provided.]

query = `query ($vara: String!, $varb: String! ) { myquery( vara: $vara, varb: $varb) }`
r = graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
fmt.Printf("Query: %s\nNo Errors: %d\nErrors: %s\n\n", query, len(r.Errors), r.Errors)
// Query: query ($vara: String!, $varb: String! ) { myquery( vara: $vara, varb: $varb) }
// No Errors: 1
// Errors: [Variable "$vara" of required type "String!" was not provided.]
}
```

Is this a bug or is this an expected behavior? If the behavior is expected, how can I get around it if I specifically want to use the second format?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.