Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Object key cannot be named "ID" #282

Open
scan opened this issue Jul 9, 2019 · 2 comments
Open

Object key cannot be named "ID" #282

scan opened this issue Jul 9, 2019 · 2 comments

Comments

@scan
Copy link

scan commented Jul 9, 2019

When trying to enable pagination, I have to add a Key to my object. The struct looks like this (simplified for brevity):

type Post struct {
    ID string `graphql:"id"`
}

func registerPost(builder *schemabuilder.Schema) {
	obj := builder.Object("Post", place.Post{})

	obj.Key("id")
}

I get the error Error building schema: field doesn't exist on struct.

Next I changed the Key registration to

obj.Key("ID")

This gives me the error: Error building schema: bad method place on type schemabuilder.query: key field doesn't exist on object

So lastly I changed the name of the ID field:

type Post struct {
    Id string `graphql:"id"`
}

func registerPost(builder *schemabuilder.Schema) {
	obj := builder.Object("Post", place.Post{})

	obj.Key("id")
}

This now works without an error. But the linter complains that Id looks ugly in the code, and I'm inclined to agree. But I don't know why I cannot just name it ID.

@atombender
Copy link

Same here. Note that you can use a function as a workaround:

obj.FieldFunc("id", func(ctx context.Context, p *place.Post) string {
	return p.ID
})

@w65536
Copy link

w65536 commented Jan 4, 2022

I do not have a solution. But I believe to know why this is.

reverseGraphqlFieldName blindly converts the graphql name back to the Go field name by simply making the first letter upper case. The graphql tags override the standard forward conversion of Go field names to graphql names. reverseGraphqlFieldName completely ignores this. Thus graphql id becomes Go field name Id, which is why the Go field must be called Id, at which point you could drop the graphql tag again because it is no longer required.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants