Add support for abstract-typed named fragments (#79)

## Summary:
In previous commits I added support to genqlient for interfaces,
inline fragments, and, most recently, named fragments of concrete
(object) type.  This leaves only named fragments of interface type!
Like other named fragments, these are useful for code-sharing,
especially if you want some code that can handle the same fields of
several different types.

As seems to be inevitable with genqlient, this was mostly pretty
straightforward, although there turned out to be surprisingly many
places we needed to add some handling; almost anywhere that touches
interfaces *or* named fragments needed some updates.  But it's all
hopefully fairly clear code.

As a part of this change I made three semi-related improvements:
1. I refactored the handling of descriptions (i.e. GoDoc), because it
   was getting more and more confusing and duplicative.  I'm still not
   sure how much of it it makes sense to inline vs. separate, but I
   think this is better than it was.  This resulted in some minor
   changes to descriptions, generally in the direction of making things
   more consistent.
2. I bumped the minimum Go version to 1.14 so we can guarantee support
   for duplicate interface methods.  These are useful for
   abstract-in-absstract spreads; we generate an interface for the
   fragment, and (if the fragment-type implements the scope-type) we
   embed it into the interface we generate for its spread-context, and
   if the two have a duplicated field we thus duplicate the method.  It
   wouldn't be impossible to support this on 1.13 (maybe just by
   omitting said embed) but it didn't seem worth it.  This also removes
   a few special-cases in tests.
3. I added a bunch of code to better format syntax errors in the
   generated code (which we see from `gofmt`).  This is mostly just an
   internal improvement; I wrote it because I got annoyed while hunting
   down a few such errors..

Fixes, at last, #8.

Issue: https://github.com/Khan/genqlient/issues/8

## Test plan:
make check


Author: benjaminjkraft

Reviewers: dnerdy, benjaminjkraft, aberkan, MiguelCastillo

Required Reviewers: 

Approved By: dnerdy

Checks:  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint

Pull Request URL: https://github.com/Khan/genqlient/pull/79
This commit is contained in:
Ben Kraft
2021-09-09 09:48:18 -07:00
committed by GitHub
parent f99c10d6fd
commit e88305ecbd
22 changed files with 704 additions and 228 deletions
+99 -46
View File
@@ -41,7 +41,6 @@ func (v *AnimalFields) UnmarshalJSON(b []byte) error {
"Unable to unmarshal AnimalFields.Owner: %w", err)
}
}
return nil
}
@@ -61,10 +60,6 @@ type AnimalFieldsOwnerAnimal struct {
// AnimalFieldsOwnerBeing is implemented by the following types:
// AnimalFieldsOwnerUser
// AnimalFieldsOwnerAnimal
//
// The GraphQL type's documentation follows.
//
//
type AnimalFieldsOwnerBeing interface {
implementsGraphQLInterfaceAnimalFieldsOwnerBeing()
// GetTypename returns the receiver's concrete GraphQL type-name (see interface doc for possible values).
@@ -120,9 +115,10 @@ func __unmarshalAnimalFieldsOwnerBeing(v *AnimalFieldsOwnerBeing, m json.RawMess
// AnimalFieldsOwnerUser includes the requested fields of the GraphQL type User.
type AnimalFieldsOwnerUser struct {
Typename string `json:"__typename"`
Id string `json:"id"`
UserFields `json:"-"`
Typename string `json:"__typename"`
Id string `json:"id"`
UserFields `json:"-"`
LuckyFieldsUser `json:"-"`
}
func (v *AnimalFieldsOwnerUser) UnmarshalJSON(b []byte) error {
@@ -138,7 +134,81 @@ func (v *AnimalFieldsOwnerUser) UnmarshalJSON(b []byte) error {
return err
}
err = json.Unmarshal(b, &v.UserFields)
err = json.Unmarshal(
b, &v.UserFields)
if err != nil {
return err
}
err = json.Unmarshal(
b, &v.LuckyFieldsUser)
if err != nil {
return err
}
return nil
}
// LuckyFields includes the GraphQL fields of Lucky requested by the fragment LuckyFields.
//
// LuckyFields is implemented by the following types:
// LuckyFieldsUser
type LuckyFields interface {
implementsGraphQLInterfaceLuckyFields()
// GetLuckyNumber returns the interface-field "luckyNumber" from its implementation.
GetLuckyNumber() int
}
func (v *LuckyFieldsUser) implementsGraphQLInterfaceLuckyFields() {}
// GetLuckyNumber is a part of, and documented with, the interface LuckyFields.
func (v *LuckyFieldsUser) GetLuckyNumber() int { return v.LuckyNumber }
func __unmarshalLuckyFields(v *LuckyFields, m json.RawMessage) error {
if string(m) == "null" {
return nil
}
var tn struct {
TypeName string `json:"__typename"`
}
err := json.Unmarshal(m, &tn)
if err != nil {
return err
}
switch tn.TypeName {
case "User":
*v = new(LuckyFieldsUser)
return json.Unmarshal(m, *v)
case "":
return fmt.Errorf(
"Response was missing Lucky.__typename")
default:
return fmt.Errorf(
`Unexpected concrete type for LuckyFields: "%v"`, tn.TypeName)
}
}
// LuckyFields includes the GraphQL fields of User requested by the fragment LuckyFields.
type LuckyFieldsUser struct {
MoreUserFields `json:"-"`
LuckyNumber int `json:"luckyNumber"`
}
func (v *LuckyFieldsUser) UnmarshalJSON(b []byte) error {
var firstPass struct {
*LuckyFieldsUser
graphql.NoUnmarshalJSON
}
firstPass.LuckyFieldsUser = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
}
err = json.Unmarshal(
b, &v.MoreUserFields)
if err != nil {
return err
}
@@ -165,9 +235,9 @@ const (
// UserFields includes the GraphQL fields of User requested by the fragment UserFields.
type UserFields struct {
Id string `json:"id"`
LuckyNumber int `json:"luckyNumber"`
MoreUserFields `json:"-"`
Id string `json:"id"`
LuckyFieldsUser `json:"-"`
MoreUserFields `json:"-"`
}
func (v *UserFields) UnmarshalJSON(b []byte) error {
@@ -183,7 +253,13 @@ func (v *UserFields) UnmarshalJSON(b []byte) error {
return err
}
err = json.Unmarshal(b, &v.MoreUserFields)
err = json.Unmarshal(
b, &v.LuckyFieldsUser)
if err != nil {
return err
}
err = json.Unmarshal(
b, &v.MoreUserFields)
if err != nil {
return err
}
@@ -224,7 +300,6 @@ func (v *queryWithFragmentsBeingsAnimal) UnmarshalJSON(b []byte) error {
"Unable to unmarshal queryWithFragmentsBeingsAnimal.Owner: %w", err)
}
}
return nil
}
@@ -245,10 +320,6 @@ type queryWithFragmentsBeingsAnimalOwnerAnimal struct {
// queryWithFragmentsBeingsAnimalOwnerBeing is implemented by the following types:
// queryWithFragmentsBeingsAnimalOwnerUser
// queryWithFragmentsBeingsAnimalOwnerAnimal
//
// The GraphQL type's documentation follows.
//
//
type queryWithFragmentsBeingsAnimalOwnerBeing interface {
implementsGraphQLInterfacequeryWithFragmentsBeingsAnimalOwnerBeing()
// GetTypename returns the receiver's concrete GraphQL type-name (see interface doc for possible values).
@@ -325,10 +396,6 @@ type queryWithFragmentsBeingsAnimalOwnerUser struct {
// queryWithFragmentsBeingsBeing is implemented by the following types:
// queryWithFragmentsBeingsUser
// queryWithFragmentsBeingsAnimal
//
// The GraphQL type's documentation follows.
//
//
type queryWithFragmentsBeingsBeing interface {
implementsGraphQLInterfacequeryWithFragmentsBeingsBeing()
// GetTypename returns the receiver's concrete GraphQL type-name (see interface doc for possible values).
@@ -439,7 +506,6 @@ func (v *queryWithFragmentsResponse) UnmarshalJSON(b []byte) error {
}
}
}
return nil
}
@@ -455,10 +521,6 @@ type queryWithInterfaceListFieldBeingsAnimal struct {
// queryWithInterfaceListFieldBeingsBeing is implemented by the following types:
// queryWithInterfaceListFieldBeingsUser
// queryWithInterfaceListFieldBeingsAnimal
//
// The GraphQL type's documentation follows.
//
//
type queryWithInterfaceListFieldBeingsBeing interface {
implementsGraphQLInterfacequeryWithInterfaceListFieldBeingsBeing()
// GetTypename returns the receiver's concrete GraphQL type-name (see interface doc for possible values).
@@ -564,7 +626,6 @@ func (v *queryWithInterfaceListFieldResponse) UnmarshalJSON(b []byte) error {
}
}
}
return nil
}
@@ -580,10 +641,6 @@ type queryWithInterfaceListPointerFieldBeingsAnimal struct {
// queryWithInterfaceListPointerFieldBeingsBeing is implemented by the following types:
// queryWithInterfaceListPointerFieldBeingsUser
// queryWithInterfaceListPointerFieldBeingsAnimal
//
// The GraphQL type's documentation follows.
//
//
type queryWithInterfaceListPointerFieldBeingsBeing interface {
implementsGraphQLInterfacequeryWithInterfaceListPointerFieldBeingsBeing()
// GetTypename returns the receiver's concrete GraphQL type-name (see interface doc for possible values).
@@ -690,7 +747,6 @@ func (v *queryWithInterfaceListPointerFieldResponse) UnmarshalJSON(b []byte) err
}
}
}
return nil
}
@@ -699,10 +755,6 @@ func (v *queryWithInterfaceListPointerFieldResponse) UnmarshalJSON(b []byte) err
// queryWithInterfaceNoFragmentsBeing is implemented by the following types:
// queryWithInterfaceNoFragmentsBeingUser
// queryWithInterfaceNoFragmentsBeingAnimal
//
// The GraphQL type's documentation follows.
//
//
type queryWithInterfaceNoFragmentsBeing interface {
implementsGraphQLInterfacequeryWithInterfaceNoFragmentsBeing()
// GetTypename returns the receiver's concrete GraphQL type-name (see interface doc for possible values).
@@ -816,7 +868,6 @@ func (v *queryWithInterfaceNoFragmentsResponse) UnmarshalJSON(b []byte) error {
"Unable to unmarshal queryWithInterfaceNoFragmentsResponse.Being: %w", err)
}
}
return nil
}
@@ -840,7 +891,8 @@ func (v *queryWithNamedFragmentsBeingsAnimal) UnmarshalJSON(b []byte) error {
return err
}
err = json.Unmarshal(b, &v.AnimalFields)
err = json.Unmarshal(
b, &v.AnimalFields)
if err != nil {
return err
}
@@ -852,10 +904,6 @@ func (v *queryWithNamedFragmentsBeingsAnimal) UnmarshalJSON(b []byte) error {
// queryWithNamedFragmentsBeingsBeing is implemented by the following types:
// queryWithNamedFragmentsBeingsUser
// queryWithNamedFragmentsBeingsAnimal
//
// The GraphQL type's documentation follows.
//
//
type queryWithNamedFragmentsBeingsBeing interface {
implementsGraphQLInterfacequeryWithNamedFragmentsBeingsBeing()
// GetTypename returns the receiver's concrete GraphQL type-name (see interface doc for possible values).
@@ -931,7 +979,8 @@ func (v *queryWithNamedFragmentsBeingsUser) UnmarshalJSON(b []byte) error {
return err
}
err = json.Unmarshal(b, &v.UserFields)
err = json.Unmarshal(
b, &v.UserFields)
if err != nil {
return err
}
@@ -973,7 +1022,6 @@ func (v *queryWithNamedFragmentsResponse) UnmarshalJSON(b []byte) error {
}
}
}
return nil
}
@@ -1241,13 +1289,18 @@ fragment AnimalFields on Animal {
__typename
id
... UserFields
... LuckyFields
}
}
fragment UserFields on User {
id
luckyNumber
... LuckyFields
... MoreUserFields
}
fragment LuckyFields on Lucky {
... MoreUserFields
luckyNumber
}
fragment MoreUserFields on User {
id
hair {
+19 -3
View File
@@ -293,16 +293,22 @@ func TestNamedFragments(t *testing.T) {
fragment AnimalFields on Animal {
id
hair { hasHair }
owner { id ...UserFields }
owner { id ...UserFields ...LuckyFields }
}
fragment MoreUserFields on User {
id
hair { color }
}
fragment LuckyFields on Lucky {
...MoreUserFields
luckyNumber
}
fragment UserFields on User {
id luckyNumber
id
...LuckyFields
...MoreUserFields
}
@@ -340,9 +346,12 @@ func TestNamedFragments(t *testing.T) {
assert.Equal(t, "1", user.Id)
assert.Equal(t, "1", user.UserFields.Id)
assert.Equal(t, "1", user.UserFields.MoreUserFields.Id)
assert.Equal(t, "1", user.UserFields.LuckyFieldsUser.MoreUserFields.Id)
// on UserFields, but we should be able to access directly via embedding:
assert.Equal(t, 17, user.LuckyNumber)
assert.Equal(t, "Black", user.Hair.Color)
assert.Equal(t, "Black", user.UserFields.MoreUserFields.Hair.Color)
assert.Equal(t, "Black", user.UserFields.LuckyFieldsUser.MoreUserFields.Hair.Color)
// Animal has, in total, the fields:
// __typename
@@ -369,9 +378,16 @@ func TestNamedFragments(t *testing.T) {
assert.Equal(t, "1", owner.Id)
assert.Equal(t, "1", owner.UserFields.Id)
assert.Equal(t, "1", owner.UserFields.MoreUserFields.Id)
assert.Equal(t, "1", owner.UserFields.LuckyFieldsUser.MoreUserFields.Id)
// on UserFields:
assert.Equal(t, 17, owner.LuckyNumber)
assert.Equal(t, "Black", owner.Hair.Color)
assert.Equal(t, "Black", owner.UserFields.MoreUserFields.Hair.Color)
assert.Equal(t, "Black", owner.UserFields.LuckyFieldsUser.MoreUserFields.Hair.Color)
// Lucky-based fields we can also get by casting to the fragment-interface.
luckyOwner, ok := animal.Owner.(LuckyFields)
require.Truef(t, ok, "got %T, not Lucky", animal.Owner)
assert.Equal(t, 17, luckyOwner.GetLuckyNumber())
assert.Nil(t, resp.Beings[2])
}