Using interfaces to access generated types
- Dominant language
- Go
- Stars
- 1.3k
- Forks
- 143
- Avg merge
- 4h 50m
- Merged PRs (30d)
- 1
Description
Hi there,
I'm trying to use interfaces to work around duplicate Go types being generated for the same GQL types when they're used in different queries. Something like this:
```go
// genqlient code
package gql
type Query_Foo struct {
Bar *string
Baz *GetFoo_Foo_Baz
}
type GetFoo_Foo_Baz struct {
Quux *int
}
func (v *Query_Foo) GetBar() *string {
return v.Bar
}
func (v *Query_Foo) GetBaz() *Query_Foo_Baz {
return v.Baz
}
func (v *GetFoo_Foo_Baz) GetQuux() *int {
return v.Quux
}
```
```go
// My code
package example
type gqlFoo interface {
GetBar() *string
}
var a *gql.Query_Foo
x := func(gqlFoo) {}
x(a)
```
This works fine for accessing non-struct types, but when I try to do the same for `Query_Foo.Baz`, it breaks:
```go
type gqlFoo interface {
GetBar() *string
GetBaz() *gqlBaz
}
type gqlBaz interface {
GetQuux() *int
}
var a *gql.Query_Foo
x := func(gqlFoo) {}
x(a)
```
```
redacted/foo.go: cannot use a (variable of type *gql.Query_Foo) as example.gqlFoo value in argument to x:
*gql.Query_Foo does not implement gqlFoo (wrong type for method GetBaz)
have GetBaz() *Query_Foo_Baz
want GetBaz() *gqlBaz
```
I feel like I'm missing something obvious... can I work around this?
Thanks in advance!
Contributor guide
Research direction
Review the generated type and method signatures shown in the issue, along with the generator behavior for nested GraphQL types. Determine whether a supported interface-compatible approach is possible without violating Go method-signature rules. Done means documenting a workaround or defining a clear generator change with tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, graphql
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100