Skip to content
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

Generate Getters for Interface Fields #1469

Closed
maaft opened this issue Feb 19, 2021 · 4 comments · Fixed by #2314
Closed

Generate Getters for Interface Fields #1469

maaft opened this issue Feb 19, 2021 · 4 comments · Fixed by #2314

Comments

@maaft
Copy link

maaft commented Feb 19, 2021

Currently, when you have a schema like this:

interface Foo {
  value: String!
}

type FooA {
  bla: String!
}

type FooB {
  blub: String!
}

The generated go file will look like this:

type Foo interface {
   IsFoo()
}

type FooA struct {
   Bla string
   Value string
}

func (f FooA) IsFoo() {}

type FooB struct {
   Blub string
   Value string
}

func (f FooB) IsFoo() {}

Additionally, it would be great to also generate getters for all interface fields. This makes using typeswitches obsolet when you're not interested in the underlying type.

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
}
@elversatile
Copy link

This change makes a ton of sense and would make coding against interfaces a lot simpler.

@iyinoluwaayoola
Copy link

Hit this issue today; this will make the generated interfaces actually useful.

@cpurta
Copy link

cpurta commented Apr 5, 2022

This really should be something that is supported. It is extremely cumbersome to get around this as there is really only 1 way to get the Value in the example. Create helper function for each field that is included in the common interface definition and that has to use type-switching as stated before:

func GetValue(foo Foo) string {
    switch f := foo.(type) {
    case FooA:
        return f.Value
    case FooB:
        return f.Value
    }
    return ""
}

If a new field is added to the interface graphql schema another helper function will need to be added. These have to be implemented in helper packages and adds more complexity than is really needed as opposed to just generating the Getters.

@neptoess
Copy link
Contributor

neptoess commented Aug 5, 2022

If anyone is still wanting this functionality, feel free to review #2314 and let me know if it matches your expectations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants