Fix type-naming in the presence of interfaces, and refactor it a lot (#71)
## Summary: When adding support for interfaces, I did not do the type-names as I intended: they came out to be `MyFieldMyType`, not `MyInterfaceMyFieldMyType`, which is inconsistent, but not strictly wrong. But once supporting fragments, this is also now incorrect. (Exactly why is described in the comments inline.) In this commit, in any case, I fix it. To do that, I finally did the last of the refactors I've been hoping to do but unable to successfully implement, which is to make the type-name and type-name-prefix management clearer. In the past it was kind of spread out, and each caller would have to pass the right name into `convertDefinition`, which go quite unwieldy. Now, the case that really wanted that -- the operation toplevel -- just does it own thing; and the main name-generation code is factored out into a separate file with tests, and with a long comment that goes into all the details of the algorithm that the design-doc didn't cover. (I even had some fun using a linked list to implement the prefix-stack!) This allowed me to fix the above bug fairly easily -- actually the fix was pretty much automatic once I understood how to organize things. There is one change which is that if your query name is unexported, we no longer do the same with the input-type names; it's unclear to me if anyone will actually care about this behavior (Khan always makes the queries exported) but if they did it was very inconsistent (only at the query toplevel, and only for input-objects, not enums), so we can reimplement it properly if that comes up. As a bonus fix, we now better handle the case where your type-names are lowercase, which is legal if nonstandard GraphQL. Issue: https://github.com/Khan/genqlient/issues/8 ## Test plan: make tesc Author: benjaminjkraft Reviewers: dnerdy, benjaminjkraft, aberkan, MiguelCastillo Required Reviewers: Approved by: dnerdy Checks: ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Test (1.13), ✅ Lint, ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Test (1.13), ✅ Lint Pull request URL: https://github.com/Khan/genqlient/pull/71
This commit is contained in:
+109
-104
@@ -19,12 +19,12 @@ const (
|
||||
|
||||
// queryWithFragmentsBeingsAnimal includes the requested fields of the GraphQL type Animal.
|
||||
type queryWithFragmentsBeingsAnimal struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Hair queryWithFragmentsBeingsHair `json:"hair"`
|
||||
Species Species `json:"species"`
|
||||
Owner queryWithFragmentsBeingsOwnerBeing `json:"-"`
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Hair queryWithFragmentsBeingsAnimalHairBeingsHair `json:"hair"`
|
||||
Species Species `json:"species"`
|
||||
Owner queryWithFragmentsBeingsAnimalOwnerBeing `json:"-"`
|
||||
}
|
||||
|
||||
func (v *queryWithFragmentsBeingsAnimal) UnmarshalJSON(b []byte) error {
|
||||
@@ -45,7 +45,7 @@ func (v *queryWithFragmentsBeingsAnimal) UnmarshalJSON(b []byte) error {
|
||||
{
|
||||
target := &v.Owner
|
||||
raw := firstPass.Owner
|
||||
err = __unmarshalqueryWithFragmentsBeingsOwnerBeing(
|
||||
err = __unmarshalqueryWithFragmentsBeingsAnimalOwnerBeing(
|
||||
target, raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf(
|
||||
@@ -55,6 +55,98 @@ func (v *queryWithFragmentsBeingsAnimal) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsAnimalHairBeingsHair includes the requested fields of the GraphQL type BeingsHair.
|
||||
type queryWithFragmentsBeingsAnimalHairBeingsHair struct {
|
||||
HasHair bool `json:"hasHair"`
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsAnimalOwnerAnimal includes the requested fields of the GraphQL type Animal.
|
||||
type queryWithFragmentsBeingsAnimalOwnerAnimal struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsAnimalOwnerBeing includes the requested fields of the GraphQL interface Being.
|
||||
//
|
||||
// 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).
|
||||
GetTypename() string
|
||||
// GetId returns the interface-field "id" from its implementation.
|
||||
GetId() string
|
||||
// GetName returns the interface-field "name" from its implementation.
|
||||
GetName() string
|
||||
}
|
||||
|
||||
func (v *queryWithFragmentsBeingsAnimalOwnerUser) implementsGraphQLInterfacequeryWithFragmentsBeingsAnimalOwnerBeing() {
|
||||
}
|
||||
|
||||
// GetTypename is a part of, and documented with, the interface queryWithFragmentsBeingsAnimalOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsAnimalOwnerUser) GetTypename() string { return v.Typename }
|
||||
|
||||
// GetId is a part of, and documented with, the interface queryWithFragmentsBeingsAnimalOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsAnimalOwnerUser) GetId() string { return v.Id }
|
||||
|
||||
// GetName is a part of, and documented with, the interface queryWithFragmentsBeingsAnimalOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsAnimalOwnerUser) GetName() string { return v.Name }
|
||||
|
||||
func (v *queryWithFragmentsBeingsAnimalOwnerAnimal) implementsGraphQLInterfacequeryWithFragmentsBeingsAnimalOwnerBeing() {
|
||||
}
|
||||
|
||||
// GetTypename is a part of, and documented with, the interface queryWithFragmentsBeingsAnimalOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsAnimalOwnerAnimal) GetTypename() string { return v.Typename }
|
||||
|
||||
// GetId is a part of, and documented with, the interface queryWithFragmentsBeingsAnimalOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsAnimalOwnerAnimal) GetId() string { return v.Id }
|
||||
|
||||
// GetName is a part of, and documented with, the interface queryWithFragmentsBeingsAnimalOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsAnimalOwnerAnimal) GetName() string { return v.Name }
|
||||
|
||||
func __unmarshalqueryWithFragmentsBeingsAnimalOwnerBeing(v *queryWithFragmentsBeingsAnimalOwnerBeing, 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(queryWithFragmentsBeingsAnimalOwnerUser)
|
||||
return json.Unmarshal(m, *v)
|
||||
case "Animal":
|
||||
*v = new(queryWithFragmentsBeingsAnimalOwnerAnimal)
|
||||
return json.Unmarshal(m, *v)
|
||||
case "":
|
||||
return fmt.Errorf(
|
||||
"Response was missing Being.__typename")
|
||||
default:
|
||||
return fmt.Errorf(
|
||||
`Unexpected concrete type for queryWithFragmentsBeingsAnimalOwnerBeing: "%v"`, tn.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsAnimalOwnerUser includes the requested fields of the GraphQL type User.
|
||||
type queryWithFragmentsBeingsAnimalOwnerUser struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
LuckyNumber int `json:"luckyNumber"`
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsBeing includes the requested fields of the GraphQL interface Being.
|
||||
//
|
||||
// queryWithFragmentsBeingsBeing is implemented by the following types:
|
||||
@@ -125,105 +217,18 @@ func __unmarshalqueryWithFragmentsBeingsBeing(v *queryWithFragmentsBeingsBeing,
|
||||
}
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsHair includes the requested fields of the GraphQL type BeingsHair.
|
||||
type queryWithFragmentsBeingsHair struct {
|
||||
HasHair bool `json:"hasHair"`
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsOwnerAnimal includes the requested fields of the GraphQL type Animal.
|
||||
type queryWithFragmentsBeingsOwnerAnimal struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsOwnerBeing includes the requested fields of the GraphQL interface Being.
|
||||
//
|
||||
// queryWithFragmentsBeingsOwnerBeing is implemented by the following types:
|
||||
// queryWithFragmentsBeingsOwnerUser
|
||||
// queryWithFragmentsBeingsOwnerAnimal
|
||||
//
|
||||
// The GraphQL type's documentation follows.
|
||||
//
|
||||
//
|
||||
type queryWithFragmentsBeingsOwnerBeing interface {
|
||||
implementsGraphQLInterfacequeryWithFragmentsBeingsOwnerBeing()
|
||||
// GetTypename returns the receiver's concrete GraphQL type-name (see interface doc for possible values).
|
||||
GetTypename() string
|
||||
// GetId returns the interface-field "id" from its implementation.
|
||||
GetId() string
|
||||
// GetName returns the interface-field "name" from its implementation.
|
||||
GetName() string
|
||||
}
|
||||
|
||||
func (v *queryWithFragmentsBeingsOwnerUser) implementsGraphQLInterfacequeryWithFragmentsBeingsOwnerBeing() {
|
||||
}
|
||||
|
||||
// GetTypename is a part of, and documented with, the interface queryWithFragmentsBeingsOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsOwnerUser) GetTypename() string { return v.Typename }
|
||||
|
||||
// GetId is a part of, and documented with, the interface queryWithFragmentsBeingsOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsOwnerUser) GetId() string { return v.Id }
|
||||
|
||||
// GetName is a part of, and documented with, the interface queryWithFragmentsBeingsOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsOwnerUser) GetName() string { return v.Name }
|
||||
|
||||
func (v *queryWithFragmentsBeingsOwnerAnimal) implementsGraphQLInterfacequeryWithFragmentsBeingsOwnerBeing() {
|
||||
}
|
||||
|
||||
// GetTypename is a part of, and documented with, the interface queryWithFragmentsBeingsOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsOwnerAnimal) GetTypename() string { return v.Typename }
|
||||
|
||||
// GetId is a part of, and documented with, the interface queryWithFragmentsBeingsOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsOwnerAnimal) GetId() string { return v.Id }
|
||||
|
||||
// GetName is a part of, and documented with, the interface queryWithFragmentsBeingsOwnerBeing.
|
||||
func (v *queryWithFragmentsBeingsOwnerAnimal) GetName() string { return v.Name }
|
||||
|
||||
func __unmarshalqueryWithFragmentsBeingsOwnerBeing(v *queryWithFragmentsBeingsOwnerBeing, 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(queryWithFragmentsBeingsOwnerUser)
|
||||
return json.Unmarshal(m, *v)
|
||||
case "Animal":
|
||||
*v = new(queryWithFragmentsBeingsOwnerAnimal)
|
||||
return json.Unmarshal(m, *v)
|
||||
case "":
|
||||
return fmt.Errorf(
|
||||
"Response was missing Being.__typename")
|
||||
default:
|
||||
return fmt.Errorf(
|
||||
`Unexpected concrete type for queryWithFragmentsBeingsOwnerBeing: "%v"`, tn.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsOwnerUser includes the requested fields of the GraphQL type User.
|
||||
type queryWithFragmentsBeingsOwnerUser struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
LuckyNumber int `json:"luckyNumber"`
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsUser includes the requested fields of the GraphQL type User.
|
||||
type queryWithFragmentsBeingsUser struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
LuckyNumber int `json:"luckyNumber"`
|
||||
Hair queryWithFragmentsBeingsHair `json:"hair"`
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
LuckyNumber int `json:"luckyNumber"`
|
||||
Hair queryWithFragmentsBeingsUserHair `json:"hair"`
|
||||
}
|
||||
|
||||
// queryWithFragmentsBeingsUserHair includes the requested fields of the GraphQL type Hair.
|
||||
type queryWithFragmentsBeingsUserHair struct {
|
||||
Color string `json:"color"`
|
||||
}
|
||||
|
||||
// queryWithFragmentsResponse is returned by queryWithFragments on success.
|
||||
|
||||
@@ -253,10 +253,7 @@ func TestFragments(t *testing.T) {
|
||||
require.Truef(t, ok, "got %T, not User", resp.Beings[0])
|
||||
assert.Equal(t, "1", user.Id)
|
||||
assert.Equal(t, "Yours Truly", user.Name)
|
||||
// TODO(benkraft): Uncomment once we fix the interface-field type-naming
|
||||
// bug that's causing this to get the wrong type (because we end up
|
||||
// generating two conflicting types).
|
||||
// assert.Equal(t, "Black", user.Hair.Color)
|
||||
assert.Equal(t, "Black", user.Hair.Color)
|
||||
assert.Equal(t, 17, user.LuckyNumber)
|
||||
|
||||
// Animal has, in total, the fields:
|
||||
@@ -282,7 +279,7 @@ func TestFragments(t *testing.T) {
|
||||
assert.Equal(t, "Yours Truly", animal.Owner.GetName())
|
||||
// (luckyNumber we have to cast for, again)
|
||||
|
||||
owner, ok := animal.Owner.(*queryWithFragmentsBeingsOwnerUser)
|
||||
owner, ok := animal.Owner.(*queryWithFragmentsBeingsAnimalOwnerUser)
|
||||
require.Truef(t, ok, "got %T, not User", animal.Owner)
|
||||
assert.Equal(t, "1", owner.Id)
|
||||
assert.Equal(t, "Yours Truly", owner.Name)
|
||||
|
||||
Reference in New Issue
Block a user