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

Config option for generating embedded structs for GraphQL interfaces #2220

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ianling
Copy link
Contributor

@ianling ianling commented May 27, 2022

See discussion #2212 for more context.

Given the following GraphQL schema:

interface Animal {
    species: String!
}

type Human implements Animal {
    species: String!
    name: String!
}

type Dog implements Animal {
    species: String!
    breed: String!
}

gqlgen without this PR generates the following Go code:

type Animal interface {
	IsAnimal()
}

type Human struct {
	Species string `json:"species"`
	Name    string `json:"name"`
}

func (Human) IsAnimal() {}

type Dog struct {
	Species string `json:"species"`
	Breed    string `json:"breed"`
}

func (Dog) IsAnimal() {}

gqlgen with this PR and a config that includes generate_embedded_structs_for_interfaces: true generates the following Go code (existing users that do not specifically opt in to the new behavior by adding this option to their config will continue getting the same behavior as before this PR):

type Animal struct {
	Species string `json:"species"`
}

type Human struct {
	Animal
	Name    string `json:"name"`
}

type Dog struct {
	Animal
	Breed    string `json:"breed"`
}

I have:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs)

@ianling ianling force-pushed the interface_embedded_structs branch from c3ab3f9 to d2294fb Compare May 27, 2022 16:44
@ianling
Copy link
Contributor Author

ianling commented Jun 1, 2022

Note: this commit makes uses of continue/break statements within the modelgen Go template. This was a feature added in Go 1.18.

@StevenACoffman
Copy link
Collaborator

Sorry, but we can't move to Go 1.18 yet. We are stuck on Go 1.16(!) until GCP AppEngine supports a newer version (3-6 months), and I don't feel comfortable maintaining this library when I can't vet it against our production use cases.

@ianling
Copy link
Contributor Author

ianling commented Jun 5, 2022 via email

@neptoess
Copy link
Contributor

neptoess commented Aug 8, 2022

@ianling
My apologies, but I had a couple PRs (#2314 #2315) get merged recently that almost certainly caused this one to have conflicts. Would recommend rebasing on the latest master.

@jgalliers
Copy link

We are encountering this in our software and this seems like a good quality-of-life enhancement. Can this be progressed? 🤞

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 this pull request may close these issues.

4 participants