Allow creating aliases for builtin types, using typename. (#133)

## Summary:
This lets you write code like:
```
query x {
   # @genqlient(typename: "MyString")
   someStringField
}
```
and genqlient will do
```
typename MyString string
type x struct {
   someStringField MyString
}
```

This was not difficult to implement, though it required introducing a
new identifier type.  The main difficulty I had was weird test
failures, that it turns out was due to the tests putting a bunch of
fields on the same line, so that the genqlient directive on the
previous line applied to all of them, accidentally.  This became a
problem when `typename` suddenly started being respected for builtin
types!  I fixed it by just spreading out the queries a bit.

Fixes #130

## Test plan:
make check

Author: csilvers

Reviewers: dnerdy, StevenACoffman, benjaminjkraft

Required Reviewers:

Approved By: StevenACoffman

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/133
This commit is contained in:
Craig Silverstein
2021-10-05 08:49:50 -07:00
committed by GitHub
parent 59b6df6aab
commit a52e55632f
8 changed files with 121 additions and 46 deletions
+7 -2
View File
@@ -1,6 +1,11 @@
query ConflictingTypeNames {
# @genqlient(typename: "T")
f { g }
f {
g
}
# @genqlient(typename: "T")
otherF: f { g h }
otherF: f {
g
h
}
}
+13 -3
View File
@@ -1,10 +1,20 @@
# @genqlient(typename: "Resp")
query TypeNames {
# @genqlient(typename: "User")
user { id name }
user {
id
name
}
# @genqlient(typename: "Item")
randomItem { id name }
randomItem {
id
# @genqlient(typename: "NameType")
name
}
# (ok to reuse the name as long as they match)
# @genqlient(typename: "User")
users { id name }
users {
id
name
}
}
@@ -29,7 +29,7 @@ type Item interface {
// ID is the identifier of the content.
GetId() testutil.ID
// GetName returns the interface-field "name" from its implementation.
GetName() string
GetName() NameType
}
func (v *ItemArticle) implementsGraphQLInterfaceItem() {}
@@ -109,7 +109,7 @@ type ItemArticle struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id testutil.ID `json:"id"`
Name string `json:"name"`
Name NameType `json:"name"`
}
// GetTypename returns ItemArticle.Typename, and is useful for accessing the field via an interface.
@@ -119,14 +119,14 @@ func (v *ItemArticle) GetTypename() string { return v.Typename }
func (v *ItemArticle) GetId() testutil.ID { return v.Id }
// GetName returns ItemArticle.Name, and is useful for accessing the field via an interface.
func (v *ItemArticle) GetName() string { return v.Name }
func (v *ItemArticle) GetName() NameType { return v.Name }
// ItemTopic includes the requested fields of the GraphQL type Topic.
type ItemTopic struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id testutil.ID `json:"id"`
Name string `json:"name"`
Name NameType `json:"name"`
}
// GetTypename returns ItemTopic.Typename, and is useful for accessing the field via an interface.
@@ -136,14 +136,14 @@ func (v *ItemTopic) GetTypename() string { return v.Typename }
func (v *ItemTopic) GetId() testutil.ID { return v.Id }
// GetName returns ItemTopic.Name, and is useful for accessing the field via an interface.
func (v *ItemTopic) GetName() string { return v.Name }
func (v *ItemTopic) GetName() NameType { return v.Name }
// ItemVideo includes the requested fields of the GraphQL type Video.
type ItemVideo struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id testutil.ID `json:"id"`
Name string `json:"name"`
Name NameType `json:"name"`
}
// GetTypename returns ItemVideo.Typename, and is useful for accessing the field via an interface.
@@ -153,7 +153,9 @@ func (v *ItemVideo) GetTypename() string { return v.Typename }
func (v *ItemVideo) GetId() testutil.ID { return v.Id }
// GetName returns ItemVideo.Name, and is useful for accessing the field via an interface.
func (v *ItemVideo) GetName() string { return v.Name }
func (v *ItemVideo) GetName() NameType { return v.Name }
type NameType string
// Resp is returned by TypeNames on success.
type Resp struct {