Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override template ref to be any instead of interface{} #3474

Merged
merged 1 commit into from
Jan 7, 2025

Conversation

StevenACoffman
Copy link
Collaborator

@StevenACoffman StevenACoffman commented Jan 7, 2025

This fixes #3414 so that ref template functions used when generating will consistently use any over interface{}. Linters, like revive, will flag the use of interface{} and the recommendation is to use gofmt -w -r 'interface{} -> any' to change them all to any. This PR coerces the value to always prefer any, which is a dirty hack that will probably bite me. I am not sure why the behavior is inconsistent without this change. I think somehow the template is supplied with $type.GO inconsistently (despite my putting types.Unalias seemingly all over the place in other PRs). If someone has a better idea, I would love to hear about it.

When you bind a type with an Any scalar field to a go struct where that field is an interface{} it will sometimes generate the scalar marshaller (eg marshalNAny2interface) with a parameter/return type of interface{} to match that field.

I'm guessing it's a race condition sort of thing, where you're using threads to traverse the graph in parallel and generate the schema gen code. One thread sees something like an input with no go type to reference and generates the marshaller with default type of any while another thread sees the go struct with type interface{} being mapped to a field on the graph of type Any and uses that (interface{}) type when generating the marshaller.

I've made an example repo to show this happening. Just run the regen.sh script (or run go run github.com/99designs/gqlgen repeatedly) to see it sometimes generate the schema_gen.go file exactly as it's committed and sometime with a diff in the schema_gen.go file.

Example repo: https://github.com/evellior/gqlgen-test

Signed-off-by: Steve Coffman steve@khanacademy.org

Signed-off-by: Steve Coffman <steve@khanacademy.org>
@coveralls
Copy link

Coverage Status

coverage: 73.888% (+0.02%) from 73.868%
when pulling 413b8d2 on override_interface_to_any
into 40ad172 on master.

@StevenACoffman StevenACoffman marked this pull request as ready for review January 7, 2025 23:21
@StevenACoffman StevenACoffman added the help wanted Extra attention is needed label Jan 7, 2025
@StevenACoffman
Copy link
Collaborator Author

StevenACoffman commented Jan 7, 2025

@UnAfraid @evellior @giautm Not sure if any of you have any insight or better ideas. This seems to work, so I'm going to merge it and let people try it out, but I would rather understand the real root of the problem, and fix it at the source.

@StevenACoffman StevenACoffman merged commit b6665ce into master Jan 7, 2025
18 checks passed
@StevenACoffman StevenACoffman deleted the override_interface_to_any branch January 7, 2025 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistencies in generated files
2 participants