graphql-go / graphql-go/graphql

Cannot query field "id" on type "Entity"

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

Description

**EDIT: Here is the [pastebin](https://pastebin.com/78EfpjZj) with the full code for further informations**

Hi, I ran into this issue and don't understand where it comes from.

I tried going through graphql issues, and some forums but couldn't find a fix.
I think the error could came from the use of FieldsThunk

Here is my code, hope it's clear
```golang
func root() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: "query",
Fields: graphql.Fields{
"post": &graphql.Field{
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Type: entity(),
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
id, idOk := params.Args["id"]
if idOk {
sID, sOk := id.(string)
if !sOk {
return nil, err.CreateArgumentsError("id", "string")
}

post, gErr := database.GetPostByID(sID)
if gErr != nil {
return nil, err.ErrInDatabaseOccured
}

return post, nil
}
return nil, nil
},
},
"posts": &graphql.Field{
Args: graphql.FieldConfigArgument{
"last": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Type: graphql.NewList(entity()),
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
nPost, uOk := params.Args["last"]
if uOk {
sNPost, sOk := nPost.(string)
if !sOk {
return nil, err.CreateArgumentsError("last", "string")
}

posts, gErr := database.GetLastNPosts(sNPost)
if gErr != nil {
return nil, err.ErrInDatabaseOccured
}

return posts, nil
}

return nil, nil
},
},
},
})
}

func entity() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: "Entity",
Fields: (graphql.FieldsThunk)(func() graphql.Fields {
return graphql.Fields{
"id": &graphql.Field{
Type: graphql.ID,
},
"inside": &graphql.Field{
Type: content(),
},
"comments": &graphql.Field{
Type: graphql.NewList(entity()),
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
return nil, nil
},
},
}
}),
})
}

func content() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: "Entity Content",
Fields: graphql.Fields{
"type": &graphql.Field{
Type: graphql.String,
},
"description": &graphql.Field{
Type: graphql.String,
},
"source": &graphql.Field{
Type: graphql.String,
},
"NSFW": &graphql.Field{
Type: graphql.Boolean,
},
},
})
}
```
Edit:
Just try once again and this time it works. Close the server try one more time, and it didn't work... I can't understand what's the issue. Maybe it's the fact that I'm returning the graphql.Object from functions, because error occurs with my post and user types too now.

Using the debugger didn't help, sometimes, entity type contains all of its fields, sometimes not. But even with all its fields, I still get the issue sometimes.

Here is a [debugger screenshot](https://framapic.org/WApMGan4LfnW/LyEfKWf5OxT8.png), with not all the fields being present inside my objects at debug time.

By the way, the resolve function doesn't get executed when the error occured.

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.