Add support for interfaces, part 2: list-of-interface (#54)
## Summary: In this commit I remove one of the limitations of our support for interfaces, from #52, by adding support for list-of-interface fields. This was surprisingly complex! The issue is that, as before, it's the containing type that has to do all the glue work -- and it's that glue work that is complicated by list-of-interface fields. All in all, it's not that much new code, and by far the hard part is just 20 lines in the UnmarshalJSON template (which come with almost twice as many lines of comments to explain them). It may be easiest to start by reading some of the generated code, and then read the template. I also added support for such fields with `pointer: true` specified, such that the type is `[][]...[]*MyInterface`, although I don't know why you would want that. This does *not* allow e.g. `*[]*[][]*MyInterface`; that would require a way to specify it (see #16) but also add some extra complexity (as we'd have to actually walk the type-unwrap chain properly, instead of just counting the number of slices and whether there's a pointer). Issue: https://github.com/Khan/genqlient/issues/8 ## Test plan: make check Author: benjaminjkraft Reviewers: dnerdy, benjaminjkraft, aberkan, csilvers, MiguelCastillo Required Reviewers: Approved by: dnerdy Checks: ⌛ Test (1.17), ⌛ Test (1.16), ⌛ Test (1.15), ⌛ Test (1.14), ⌛ Test (1.13), ⌛ Lint, ⌛ Lint, ⌛ Test (1.17), ⌛ Test (1.16), ⌛ Test (1.15), ⌛ Test (1.14), ⌛ Test (1.13) Pull request URL: https://github.com/Khan/genqlient/pull/54
This commit is contained in:
@@ -10,6 +10,183 @@ import (
|
||||
"github.com/Khan/genqlient/graphql"
|
||||
)
|
||||
|
||||
// queryWithInterfaceListFieldBeingsAnimal includes the requested fields of the GraphQL type Animal.
|
||||
type queryWithInterfaceListFieldBeingsAnimal struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// queryWithInterfaceListFieldBeingsBeing includes the requested fields of the GraphQL type Being.
|
||||
type queryWithInterfaceListFieldBeingsBeing interface {
|
||||
implementsGraphQLInterfacequeryWithInterfaceListFieldBeingsBeing()
|
||||
}
|
||||
|
||||
func (v *queryWithInterfaceListFieldBeingsUser) implementsGraphQLInterfacequeryWithInterfaceListFieldBeingsBeing() {
|
||||
}
|
||||
func (v *queryWithInterfaceListFieldBeingsAnimal) implementsGraphQLInterfacequeryWithInterfaceListFieldBeingsBeing() {
|
||||
}
|
||||
|
||||
func __unmarshalqueryWithInterfaceListFieldBeingsBeing(v *queryWithInterfaceListFieldBeingsBeing, 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(queryWithInterfaceListFieldBeingsUser)
|
||||
return json.Unmarshal(m, *v)
|
||||
case "Animal":
|
||||
*v = new(queryWithInterfaceListFieldBeingsAnimal)
|
||||
return json.Unmarshal(m, *v)
|
||||
default:
|
||||
return fmt.Errorf(
|
||||
`Unexpected concrete type for queryWithInterfaceListFieldBeingsBeing: "%v"`, tn.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
// queryWithInterfaceListFieldBeingsUser includes the requested fields of the GraphQL type User.
|
||||
type queryWithInterfaceListFieldBeingsUser struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// queryWithInterfaceListFieldResponse is returned by queryWithInterfaceListField on success.
|
||||
type queryWithInterfaceListFieldResponse struct {
|
||||
Beings []queryWithInterfaceListFieldBeingsBeing `json:"-"`
|
||||
}
|
||||
|
||||
func (v *queryWithInterfaceListFieldResponse) UnmarshalJSON(b []byte) error {
|
||||
|
||||
type queryWithInterfaceListFieldResponseWrapper queryWithInterfaceListFieldResponse
|
||||
|
||||
var firstPass struct {
|
||||
*queryWithInterfaceListFieldResponseWrapper
|
||||
Beings []json.RawMessage `json:"beings"`
|
||||
}
|
||||
firstPass.queryWithInterfaceListFieldResponseWrapper = (*queryWithInterfaceListFieldResponseWrapper)(v)
|
||||
|
||||
err := json.Unmarshal(b, &firstPass)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
{
|
||||
target := &v.Beings
|
||||
raw := firstPass.Beings
|
||||
*target = make(
|
||||
[]queryWithInterfaceListFieldBeingsBeing,
|
||||
len(raw))
|
||||
for i, raw := range raw {
|
||||
target := &(*target)[i]
|
||||
err = __unmarshalqueryWithInterfaceListFieldBeingsBeing(
|
||||
target, raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// queryWithInterfaceListPointerFieldBeingsAnimal includes the requested fields of the GraphQL type Animal.
|
||||
type queryWithInterfaceListPointerFieldBeingsAnimal struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// queryWithInterfaceListPointerFieldBeingsBeing includes the requested fields of the GraphQL type Being.
|
||||
type queryWithInterfaceListPointerFieldBeingsBeing interface {
|
||||
implementsGraphQLInterfacequeryWithInterfaceListPointerFieldBeingsBeing()
|
||||
}
|
||||
|
||||
func (v *queryWithInterfaceListPointerFieldBeingsUser) implementsGraphQLInterfacequeryWithInterfaceListPointerFieldBeingsBeing() {
|
||||
}
|
||||
func (v *queryWithInterfaceListPointerFieldBeingsAnimal) implementsGraphQLInterfacequeryWithInterfaceListPointerFieldBeingsBeing() {
|
||||
}
|
||||
|
||||
func __unmarshalqueryWithInterfaceListPointerFieldBeingsBeing(v *queryWithInterfaceListPointerFieldBeingsBeing, 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(queryWithInterfaceListPointerFieldBeingsUser)
|
||||
return json.Unmarshal(m, *v)
|
||||
case "Animal":
|
||||
*v = new(queryWithInterfaceListPointerFieldBeingsAnimal)
|
||||
return json.Unmarshal(m, *v)
|
||||
default:
|
||||
return fmt.Errorf(
|
||||
`Unexpected concrete type for queryWithInterfaceListPointerFieldBeingsBeing: "%v"`, tn.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
// queryWithInterfaceListPointerFieldBeingsUser includes the requested fields of the GraphQL type User.
|
||||
type queryWithInterfaceListPointerFieldBeingsUser struct {
|
||||
Typename string `json:"__typename"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// queryWithInterfaceListPointerFieldResponse is returned by queryWithInterfaceListPointerField on success.
|
||||
type queryWithInterfaceListPointerFieldResponse struct {
|
||||
Beings []*queryWithInterfaceListPointerFieldBeingsBeing `json:"-"`
|
||||
}
|
||||
|
||||
func (v *queryWithInterfaceListPointerFieldResponse) UnmarshalJSON(b []byte) error {
|
||||
|
||||
type queryWithInterfaceListPointerFieldResponseWrapper queryWithInterfaceListPointerFieldResponse
|
||||
|
||||
var firstPass struct {
|
||||
*queryWithInterfaceListPointerFieldResponseWrapper
|
||||
Beings []json.RawMessage `json:"beings"`
|
||||
}
|
||||
firstPass.queryWithInterfaceListPointerFieldResponseWrapper = (*queryWithInterfaceListPointerFieldResponseWrapper)(v)
|
||||
|
||||
err := json.Unmarshal(b, &firstPass)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
{
|
||||
target := &v.Beings
|
||||
raw := firstPass.Beings
|
||||
*target = make(
|
||||
[]*queryWithInterfaceListPointerFieldBeingsBeing,
|
||||
len(raw))
|
||||
for i, raw := range raw {
|
||||
target := &(*target)[i]
|
||||
*target = new(queryWithInterfaceListPointerFieldBeingsBeing)
|
||||
err = __unmarshalqueryWithInterfaceListPointerFieldBeingsBeing(
|
||||
*target, raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// queryWithInterfaceNoFragmentsBeing includes the requested fields of the GraphQL type Being.
|
||||
type queryWithInterfaceNoFragmentsBeing interface {
|
||||
implementsGraphQLInterfacequeryWithInterfaceNoFragmentsBeing()
|
||||
@@ -87,12 +264,15 @@ func (v *queryWithInterfaceNoFragmentsResponse) UnmarshalJSON(b []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = __unmarshalqueryWithInterfaceNoFragmentsBeing(
|
||||
&v.Being, firstPass.Being)
|
||||
if err != nil {
|
||||
return err
|
||||
{
|
||||
target := &v.Being
|
||||
raw := firstPass.Being
|
||||
err = __unmarshalqueryWithInterfaceNoFragmentsBeing(
|
||||
target, raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -202,3 +382,59 @@ query queryWithInterfaceNoFragments ($id: ID!) {
|
||||
)
|
||||
return &retval, err
|
||||
}
|
||||
|
||||
func queryWithInterfaceListField(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
ids []string,
|
||||
) (*queryWithInterfaceListFieldResponse, error) {
|
||||
variables := map[string]interface{}{
|
||||
"ids": ids,
|
||||
}
|
||||
|
||||
var retval queryWithInterfaceListFieldResponse
|
||||
err := client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithInterfaceListField",
|
||||
`
|
||||
query queryWithInterfaceListField ($ids: [ID!]!) {
|
||||
beings(ids: $ids) {
|
||||
__typename
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
variables,
|
||||
)
|
||||
return &retval, err
|
||||
}
|
||||
|
||||
func queryWithInterfaceListPointerField(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
ids []string,
|
||||
) (*queryWithInterfaceListPointerFieldResponse, error) {
|
||||
variables := map[string]interface{}{
|
||||
"ids": ids,
|
||||
}
|
||||
|
||||
var retval queryWithInterfaceListPointerFieldResponse
|
||||
err := client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithInterfaceListPointerField",
|
||||
`
|
||||
query queryWithInterfaceListPointerField ($ids: [ID!]!) {
|
||||
beings(ids: $ids) {
|
||||
__typename
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
variables,
|
||||
)
|
||||
return &retval, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user