Skip to content

Using interfaces to access generated types #382

@opeik

Description

@opeik

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:

// 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
}
// 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:

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions