In my GoLang GraphQL query, I have the following method (takes user id and returns profile data):
"userProfile": &graphql.Field{
Type: UserProfileType,
Args: graphql.FieldConfigArgument{
"uid": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
uid := p.Args["uid"].(int)
u, err := db.GetUserProfile(p.Context.Value(handler.CtxId("currentUser")).(int), uid)
return u, err
},
},
When user id is invalid db.GetUserProfile
returns nil pointer u
and nil error err
, but GraphQL responds the following:
{
"data": { "userProfile": null },
"errors": [
{
"message": "runtime error: invalid memory address or nil pointer dereference",
"locations": []
}
]
}
Although when I modify GraphQL code like this (explicit nil check and return literal):
...
u, err := db.GetUserProfile(p.Context.Value(handler.CtxId("currentUser")).(int), uid)
if u == nil {
return nil, err
}
return u, err
everything works as expected and GraphQL returns this:
{
"data": {
"userProfile": null
}
}
How do I manage without explicit checking for nil and teach GraphQL to distinguish nil pointer from nil literal?