-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Defined type from a basic type should not need a mandatory scalar + MarshalFunc and UnmarshalFunc #2485
Comments
I am currently trying to fix linting on my PR. |
All linting are fixed, but I now have a security check that failed, and I don't know how to fix it.. some missing file? |
I just noticed a TODO under isBanned: Banned # TODO: This can be a Boolean again once multiple backing types are allowed This was having this error message when validation failed: packages.Load: /.../resolvers.go:21:9: cannot use &(userResolver literal) (value of type *userResolver) as UserResolver value in return statement: *userResolver does not implement UserResolver (missing method IsBanned) It seems that my PR is fixing this TODO (no need of |
I think your original problem is because per the Go spec: https://golang.org/ref/spec#Type_declarations
you need to do:
Then all method calls on NewString will be method calls on
But maybe that is not what you are looking for, if that's the case, there may still be other ways around the problem, like type embedding and such. Often people will deliberately introduce a scalar and define a subtype because they explicitly want more control of how a field is marshalled/unmarshalled. For instance, datetimes, dates without times, times without dates. If you are proposing that during marshalling/unmarshalling when subtypes lack those methods, we fallback to the basic type, I would want to be careful not to break existing behavior that people rely on for custom marshalling/unmarshalling. |
Here is an example of code in my actual project: // Manufacturer represent the car manufacturer
type Manufacturer string
// Defines value of Manufacturer
const (
ManufacturerTesla Manufacturer = "TESLA"
ManufacturerHonda Manufacturer = "HONDA"
ManufacturerToyota Manufacturer = "TOYOTA"
...
) I want Manufacturer type be returned as string without creating a |
Try this: // Manufacturer represent the car manufacturer
type Manufacturer = string
// Defines value of Manufacturer
const (
ManufacturerTesla Manufacturer = "TESLA"
ManufacturerHonda Manufacturer = "HONDA"
ManufacturerToyota Manufacturer = "TOYOTA"
...
) |
Yes it works I know, but I would need to update all the existing projects with = everywhere this pattern is used in my employer's code.. not sure they would allow me :( |
Feel free to refuse my PR. I will keep my change in my fork, no worries. |
In my integration tests, I did create a scalar with custom marshalling/unmarshalling to make sure that my change is not affecting existing ones. (at first, my change did affect them but I fixed it by checking on the $type.Definition.BuiltIn in the go template). Also note that my PR works both ways, with and without the '=' (tests updated accordingly). |
As long as it doesn't break existing functionality, I'm still open to your PR, but I would like to review it more carefully, and I'm off for a holiday vacation. One thing is that in your PR description you use some terms that are unfamiliar to me like |
You are right, my title reflects where I modified the code instead of using Go specs. I updated the title and removed the word 'remote' because I noticed that I can reproduce my issue with the |
Please see #2587 which reverts the PRs and re-opens this issue until it can be addressed without causing regressions. |
What happened?
In an existing Golang project where we are trying to add
gqlgen
, we have defined some types from basic types (string, int, boolean, float) that are being used in other structs. Currently, we need to write ascalar
in the GraphQL schema and also do aMarshalFunc
andUnmarshalFunc
in order to avoid an error message saying<type> is incompatible with <named_type>
.What did you expect?
No error message appear and no need to create a
scalar
with relatedMarshalFunc
andUnmarshalFunc
whengo generate
.Minimal graphql.schema and models to reproduce
In a remote package, create some named basic types:
In another package, create a struct using these named basic types (might need to add this package to autobind):
In the GraphQL schema, create a type for the struct:
Please note that a PR is coming to fix this.
versions
go run github.com/99designs/gqlgen version
? v0.17.22-devgo version
? go1.19.3 darwin/arm64The text was updated successfully, but these errors were encountered: