Add getter methods to all fields, not just where needed (#126)
## Summary: If your type implements an interface, we add getter methods for the shared fields, so that those may be accessed via the interface. But it turns out occasionally it's useful to have these getter methods when they don't implement a GraphQL interface, so you can use two genqlient-generated types in the same function if they have the same fields. (This comes up most often when you have a GraphQL union that maybe should really be an interface, or if you don't yet support interfaces implementing other interfaces (indeed our parser doesn't either). But one can imagine other use cases.) We can't predict how you want to do that, so we can't generate the interface, but we can generate the methods, so you can define the interface and do a type assertion from there. Since these methods are pretty simple to generate, we just do it always. (As with #120, if binary size becomes an issue we could later add an option to only generate methods that are truly needed but including them seems like the better default.) This also fixes a subtle and rare bug, which would have become much more common (indeed existing tests caught it). Specifically, if you have a query like ```graphql fragment FragmentOne on T { id } fragment FragmentTwo on T { id } query Q { f { # interface type T ...FragmentOne ...FragmentTwo } } ``` since both `FragmentOne` and `FragmentTwo` request some common field, say `id`, we generate a method `GetId` on each one. But since `FragmentOne` and `FragmentTwo` are both on `T`, we also include their interfaces in the interface we generate for the type of `f`, `QFT`. So `QFT` includes a method `GetId`. But on the implementations, the two methods conflict, and neither gets promoted; this causes various code to fail to compile. With this change, this would have happened much more frequently -- even if only one of the two fragments is on `T`, as long as both request the field. Anyway, we now generate explicit methods on each struct for all of its recursively emebedded fields -- using the logic from #120 to compute them -- so that we don't need to rely on method-promotion. ## Test plan: make tesc Author: benjaminjkraft Reviewers: csilvers, dnerdy, aberkan, jvoll, mahtabsabet, MiguelCastillo, StevenACoffman Required Reviewers: Approved By: csilvers, dnerdy Checks: ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Lint, ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Lint Pull Request URL: https://github.com/Khan/genqlient/pull/126
This commit is contained in:
+44
-22
@@ -17,6 +17,12 @@ type ChildVideoFields struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// GetId returns ChildVideoFields.Id, and is useful for accessing the field via an interface.
|
||||
func (v *ChildVideoFields) GetId() testutil.ID { return v.Id }
|
||||
|
||||
// GetName returns ChildVideoFields.Name, and is useful for accessing the field via an interface.
|
||||
func (v *ChildVideoFields) GetName() string { return v.Name }
|
||||
|
||||
// ContentFields includes the GraphQL fields of Content requested by the fragment ContentFields.
|
||||
// The GraphQL type's documentation follows.
|
||||
//
|
||||
@@ -35,28 +41,8 @@ type ContentFields interface {
|
||||
}
|
||||
|
||||
func (v *ContentFieldsArticle) implementsGraphQLInterfaceContentFields() {}
|
||||
|
||||
// GetName is a part of, and documented with, the interface ContentFields.
|
||||
func (v *ContentFieldsArticle) GetName() string { return v.Name }
|
||||
|
||||
// GetUrl is a part of, and documented with, the interface ContentFields.
|
||||
func (v *ContentFieldsArticle) GetUrl() string { return v.Url }
|
||||
|
||||
func (v *ContentFieldsVideo) implementsGraphQLInterfaceContentFields() {}
|
||||
|
||||
// GetName is a part of, and documented with, the interface ContentFields.
|
||||
func (v *ContentFieldsVideo) GetName() string { return v.Name }
|
||||
|
||||
// GetUrl is a part of, and documented with, the interface ContentFields.
|
||||
func (v *ContentFieldsVideo) GetUrl() string { return v.Url }
|
||||
|
||||
func (v *ContentFieldsTopic) implementsGraphQLInterfaceContentFields() {}
|
||||
|
||||
// GetName is a part of, and documented with, the interface ContentFields.
|
||||
func (v *ContentFieldsTopic) GetName() string { return v.Name }
|
||||
|
||||
// GetUrl is a part of, and documented with, the interface ContentFields.
|
||||
func (v *ContentFieldsTopic) GetUrl() string { return v.Url }
|
||||
func (v *ContentFieldsVideo) implementsGraphQLInterfaceContentFields() {}
|
||||
func (v *ContentFieldsTopic) implementsGraphQLInterfaceContentFields() {}
|
||||
|
||||
func __unmarshalContentFields(b []byte, v *ContentFields) error {
|
||||
if string(b) == "null" {
|
||||
@@ -135,6 +121,12 @@ type ContentFieldsArticle struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
// GetName returns ContentFieldsArticle.Name, and is useful for accessing the field via an interface.
|
||||
func (v *ContentFieldsArticle) GetName() string { return v.Name }
|
||||
|
||||
// GetUrl returns ContentFieldsArticle.Url, and is useful for accessing the field via an interface.
|
||||
func (v *ContentFieldsArticle) GetUrl() string { return v.Url }
|
||||
|
||||
// ContentFields includes the GraphQL fields of Topic requested by the fragment ContentFields.
|
||||
// The GraphQL type's documentation follows.
|
||||
//
|
||||
@@ -144,6 +136,12 @@ type ContentFieldsTopic struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
// GetName returns ContentFieldsTopic.Name, and is useful for accessing the field via an interface.
|
||||
func (v *ContentFieldsTopic) GetName() string { return v.Name }
|
||||
|
||||
// GetUrl returns ContentFieldsTopic.Url, and is useful for accessing the field via an interface.
|
||||
func (v *ContentFieldsTopic) GetUrl() string { return v.Url }
|
||||
|
||||
// ContentFields includes the GraphQL fields of Video requested by the fragment ContentFields.
|
||||
// The GraphQL type's documentation follows.
|
||||
//
|
||||
@@ -153,6 +151,12 @@ type ContentFieldsVideo struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
// GetName returns ContentFieldsVideo.Name, and is useful for accessing the field via an interface.
|
||||
func (v *ContentFieldsVideo) GetName() string { return v.Name }
|
||||
|
||||
// GetUrl returns ContentFieldsVideo.Url, and is useful for accessing the field via an interface.
|
||||
func (v *ContentFieldsVideo) GetUrl() string { return v.Url }
|
||||
|
||||
// InnerQueryFragment includes the GraphQL fields of Query requested by the fragment InnerQueryFragment.
|
||||
// The GraphQL type's documentation follows.
|
||||
//
|
||||
@@ -163,6 +167,15 @@ type InnerQueryFragment struct {
|
||||
OtherVideo ContentFieldsVideo `json:"otherVideo"`
|
||||
}
|
||||
|
||||
// GetRandomVideo returns InnerQueryFragment.RandomVideo, and is useful for accessing the field via an interface.
|
||||
func (v *InnerQueryFragment) GetRandomVideo() VideoFields { return v.RandomVideo }
|
||||
|
||||
// GetRandomItem returns InnerQueryFragment.RandomItem, and is useful for accessing the field via an interface.
|
||||
func (v *InnerQueryFragment) GetRandomItem() ContentFields { return v.RandomItem }
|
||||
|
||||
// GetOtherVideo returns InnerQueryFragment.OtherVideo, and is useful for accessing the field via an interface.
|
||||
func (v *InnerQueryFragment) GetOtherVideo() ContentFieldsVideo { return v.OtherVideo }
|
||||
|
||||
func (v *InnerQueryFragment) UnmarshalJSON(b []byte) error {
|
||||
|
||||
if string(b) == "null" {
|
||||
@@ -239,11 +252,20 @@ type VideoFields struct {
|
||||
Parent VideoFieldsParentTopic `json:"parent"`
|
||||
}
|
||||
|
||||
// GetId returns VideoFields.Id, and is useful for accessing the field via an interface.
|
||||
func (v *VideoFields) GetId() testutil.ID { return v.Id }
|
||||
|
||||
// GetParent returns VideoFields.Parent, and is useful for accessing the field via an interface.
|
||||
func (v *VideoFields) GetParent() VideoFieldsParentTopic { return v.Parent }
|
||||
|
||||
// VideoFieldsParentTopic includes the requested fields of the GraphQL type Topic.
|
||||
type VideoFieldsParentTopic struct {
|
||||
VideoChildren []ChildVideoFields `json:"videoChildren"`
|
||||
}
|
||||
|
||||
// GetVideoChildren returns VideoFieldsParentTopic.VideoChildren, and is useful for accessing the field via an interface.
|
||||
func (v *VideoFieldsParentTopic) GetVideoChildren() []ChildVideoFields { return v.VideoChildren }
|
||||
|
||||
func ComplexNamedFragments(
|
||||
client graphql.Client,
|
||||
) (*InnerQueryFragment, error) {
|
||||
|
||||
Reference in New Issue
Block a user