graphql-go / graphql-go/graphql
`Cannot return null for non-nullable field` when a non nullable field has a nil parent type
- Dominant language
- Go
- Stars
- 10.1k
- Forks
- 845
- PR merge metrics
- No merged PRs in 30d
Description
The following example uses the schema below
```gql
type Query {
product: Product
}
type Product {
obj: Nested
}
type Nested {
field: String!
}
```
```go
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/graphql-go/graphql"
)
var productType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Product",
Fields: graphql.Fields{
"obj": &graphql.Field{
Type: graphql.NewObject(
graphql.ObjectConfig{
Name: "Nested",
Fields: graphql.Fields{
"field": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
},
},
},
),
},
},
},
)
type Product struct {
Obj map[string]interface{} `json:"obj"`
}
func main() {
// Schema
fields := graphql.Fields{
"product": &graphql.Field{
Type: productType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return Product{Obj: nil}, nil
},
},
}
schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(graphql.ObjectConfig{Name: "Query", Fields: fields})}
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
log.Fatalf("failed to create new schema, error: %v", err)
}
// Query
query := `
query {
product {
obj {
field
}
}
}
`
params := graphql.Params{Schema: schema, RequestString: query}
r := graphql.Do(params)
if len(r.Errors) > 0 {
log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
}
rJSON, _ := json.Marshal(r)
fmt.Printf("%s \n", rJSON) // {“data”:{“hello”:”world”}}
}
```
The query should simply return `{"product": {"obj": null}}`, instead it gives the error
```
Cannot return null for non-nullable field Nested.nested.
```
Contributor guide
Assessment
This issue has not been assessed yet.