Yamashou / gqlgenc

This is Go library for building GraphQL client with gqlgen
MIT License
371 stars 65 forks source link

Generate Getters on Interfaces #46

Open maaft opened 3 years ago

maaft commented 3 years ago

When you have this schema:

interface Foo {
  value: String!
}

type FooA {
  bla: String!
}

type FooB {
  blub: String!
}

it would be great if you could generate getter functions for all fields on the interface.

type Foo interface {
   Value()
}

type FooA struct {
   Bla string
   Value string
}

func (f FooA) Value() string {
   return f.Value
}

type FooB struct {
   Blub string
   Value string
}

func (f FooB) Value() string {
   return f.Value
}

This would allow to read all common fields directly from the interface instead of doing a typeswitch.

maoueh commented 3 years ago

The actual model generation which includes the interface/implementation generation is performed by https://github.com/99designs/gqlgen.

Would be good to open an issue there first.

Also, you should re-write your example above because it is hard to understand, here a fixed one:

interface Foo {
  value: String!
}

type FooA implements Foo  {
  bla: String!
  value: String!
}

type FooB {
  blub: String!
  value: String!
}
type Foo interface {
   IsFoo()

   GetValue() string
}

type FooA struct {
   Bla string
   Value string
}

func (f FooA) IsFoo() {}

func (f FooA) GetValue() string {
   return f.Value
}

type FooB struct {
   Blub string
   Value string
}

func (f FooB) IsFoo() {}

func (f FooB) GetValue() string {
   return f.Value
}

The getters will need to have the prefix Get since a function on a struct and a field on struct can't have the same name.

maaft commented 3 years ago

Thanks for pointing out that this is done by 99designs/gqlgen. I'll open an issue there.

Also thanks for pointing out that we'd need a prefix like "Get". You're absolutely right. Regarding your schema, you don't need to redeclare interface fields on impementing types.

maaft commented 3 years ago

for reference: https://github.com/99designs/gqlgen/issues/1469