Files
genqlient/internal/integration/generated.go
T
Ben Kraft 1e87553788 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
2021-08-25 11:57:24 -07:00

441 lines
11 KiB
Go

package integration
// Code generated by github.com/Khan/genqlient, DO NOT EDIT.
import (
"context"
"encoding/json"
"fmt"
"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()
}
func (v *queryWithInterfaceNoFragmentsBeingUser) implementsGraphQLInterfacequeryWithInterfaceNoFragmentsBeing() {
}
func (v *queryWithInterfaceNoFragmentsBeingAnimal) implementsGraphQLInterfacequeryWithInterfaceNoFragmentsBeing() {
}
func __unmarshalqueryWithInterfaceNoFragmentsBeing(v *queryWithInterfaceNoFragmentsBeing, 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(queryWithInterfaceNoFragmentsBeingUser)
return json.Unmarshal(m, *v)
case "Animal":
*v = new(queryWithInterfaceNoFragmentsBeingAnimal)
return json.Unmarshal(m, *v)
default:
return fmt.Errorf(
`Unexpected concrete type for queryWithInterfaceNoFragmentsBeing: "%v"`, tn.TypeName)
}
}
// queryWithInterfaceNoFragmentsBeingAnimal includes the requested fields of the GraphQL type Animal.
type queryWithInterfaceNoFragmentsBeingAnimal struct {
Typename string `json:"__typename"`
Id string `json:"id"`
Name string `json:"name"`
}
// queryWithInterfaceNoFragmentsBeingUser includes the requested fields of the GraphQL type User.
type queryWithInterfaceNoFragmentsBeingUser struct {
Typename string `json:"__typename"`
Id string `json:"id"`
Name string `json:"name"`
}
// queryWithInterfaceNoFragmentsMeUser includes the requested fields of the GraphQL type User.
type queryWithInterfaceNoFragmentsMeUser struct {
Id string `json:"id"`
Name string `json:"name"`
}
// queryWithInterfaceNoFragmentsResponse is returned by queryWithInterfaceNoFragments on success.
type queryWithInterfaceNoFragmentsResponse struct {
Being queryWithInterfaceNoFragmentsBeing `json:"-"`
Me queryWithInterfaceNoFragmentsMeUser `json:"me"`
}
func (v *queryWithInterfaceNoFragmentsResponse) UnmarshalJSON(b []byte) error {
type queryWithInterfaceNoFragmentsResponseWrapper queryWithInterfaceNoFragmentsResponse
var firstPass struct {
*queryWithInterfaceNoFragmentsResponseWrapper
Being json.RawMessage `json:"being"`
}
firstPass.queryWithInterfaceNoFragmentsResponseWrapper = (*queryWithInterfaceNoFragmentsResponseWrapper)(v)
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
}
{
target := &v.Being
raw := firstPass.Being
err = __unmarshalqueryWithInterfaceNoFragmentsBeing(
target, raw)
if err != nil {
return err
}
}
return nil
}
// queryWithVariablesResponse is returned by queryWithVariables on success.
type queryWithVariablesResponse struct {
User queryWithVariablesUser `json:"user"`
}
// queryWithVariablesUser includes the requested fields of the GraphQL type User.
type queryWithVariablesUser struct {
Id string `json:"id"`
Name string `json:"name"`
LuckyNumber int `json:"luckyNumber"`
}
// simpleQueryMeUser includes the requested fields of the GraphQL type User.
type simpleQueryMeUser struct {
Id string `json:"id"`
Name string `json:"name"`
LuckyNumber int `json:"luckyNumber"`
}
// simpleQueryResponse is returned by simpleQuery on success.
type simpleQueryResponse struct {
Me simpleQueryMeUser `json:"me"`
}
func simpleQuery(
ctx context.Context,
client graphql.Client,
) (*simpleQueryResponse, error) {
var retval simpleQueryResponse
err := client.MakeRequest(
ctx,
"simpleQuery",
`
query simpleQuery {
me {
id
name
luckyNumber
}
}
`,
&retval,
nil,
)
return &retval, err
}
func queryWithVariables(
ctx context.Context,
client graphql.Client,
id string,
) (*queryWithVariablesResponse, error) {
variables := map[string]interface{}{
"id": id,
}
var retval queryWithVariablesResponse
err := client.MakeRequest(
ctx,
"queryWithVariables",
`
query queryWithVariables ($id: ID!) {
user(id: $id) {
id
name
luckyNumber
}
}
`,
&retval,
variables,
)
return &retval, err
}
func queryWithInterfaceNoFragments(
ctx context.Context,
client graphql.Client,
id string,
) (*queryWithInterfaceNoFragmentsResponse, error) {
variables := map[string]interface{}{
"id": id,
}
var retval queryWithInterfaceNoFragmentsResponse
err := client.MakeRequest(
ctx,
"queryWithInterfaceNoFragments",
`
query queryWithInterfaceNoFragments ($id: ID!) {
being(id: $id) {
__typename
id
name
}
me {
id
name
}
}
`,
&retval,
variables,
)
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
}