-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Cannot map Go interface type to schema interface/union type #42
Comments
in this case they aren't, but if they accepted non pointer receivers, eg:
then it would be valid to switch on
I think this has to be the answer.
You already know the implementing types, the schema maps them out for you. What probably needs to be added is a And the interface implementor struct probably needs a custom type to carry this extra info into the template. If you want to continue your deep dive, a PR it would be appreciated. Otherwise I'll take a look when I get a chance. |
Thanks @vektah, really appreciate the fix! This was still a few items deep in my todo list... However, while the issue seems to have been fixed for the graphql interface type, it seems to still persist for the union type. I have added a union type mapped to a Go interface in my sample repo that reproduces this problem with the latest code: ereyes01/gqlgen-interface-bug@ad9648b Let me know if you will reopen this, or if you need a new issue. Thanks! |
Should be fixed in 15b3af2 |
That did it, thanks again! |
I am hitting an issue where I cannot get the
gqlgen
generated code to build when I map a schema interface type to a Go interface.Below is an example of what I'm talking about. Full code and detailed instructions to reproduce are at https://github.com/ereyes01/gqlgen-interface-bug
Suppose I have this schema:
And suppose I have the following corresponding Go types:
So let's map our types as follows:
NOTE: ^^ This uses the path to my repository given above as if I'd obtained it via
go get
.So from this we can now create our
generated.go
as follows:$ gqlgen -out generated.go -package shapes -typemap types.json schema.graphql
... and that works fine. We now implement the needed resolver:
... And I confirmed that the code generated for
Shape.area()
looks correct- it just calls theArea()
Go method on the Go type.So now let's say that inside
shapes/server
we place a simple Playground server:If I try to build that via
go install
I get a compile error in the generated code:If we go have a look at that part of the generated code, we have:
Of course, the
case Circle
andcase Rectangle
are not valid when type-switching on the Shape type, as those types do not implement the Shape interface (the pointers to those types do).I could work around this by commenting out the
case Circle
andcase Rectangle
sections, and then everything works fine.I investigated why this code was generated, and I found that in the template for the interface type, it just blindly writes out a case for both the Go type and its pointer:
This generated will only ever work if:
interface{}
)... which doesn't sound right if you're going to allow the interface type to be matched to your own type.
I'm not immediately sure what the best way to fix this might be. One way might be to look into whether the Go type system can tell you which are all the implementing types of an interface (I haven't really investigated that rabbit hole).
Another approach might be to allow us to tell you the implementing types in the
types.json
file. Of course, that will further complicate the format of that JSON file, but it seems otherwise robust at first glance.Thanks for taking a look.
The text was updated successfully, but these errors were encountered: