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 on Interfaces #46

Open
maaft opened this issue Feb 18, 2021 · 3 comments
Open

Generate Getters on Interfaces #46

maaft opened this issue Feb 18, 2021 · 3 comments

Comments

@maaft
Copy link

maaft commented Feb 18, 2021

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
Copy link
Contributor

maoueh commented Feb 18, 2021

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
Copy link
Author

maaft commented Feb 19, 2021

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
Copy link
Author

maaft commented Feb 19, 2021

for reference: 99designs/gqlgen#1469

@Yamashou Yamashou reopened this Nov 13, 2024
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

No branches or pull requests

3 participants