Files
genqlient/generate/testdata/snapshots/TestGenerate-InterfaceNoFragments.graphql-InterfaceNoFragments.graphql.go
T
Ben KraftandGitHub 65f9e90f2b Add support for interfaces, part 3: automatically add __typename (#56)
## Summary:
In #52, I added support for interface types, but with the simplifying
restriction (among others) that the user must request the field
`__typename`.  In this commit, I remove this restriction.

The basic idea is simple: we preprocess the query to add `__typename`.
The implementation isn't much more complicated!  Although it required
some new wiring in a few places.

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

## Test plan:
make check


Author: benjaminjkraft

Reviewers: dnerdy, 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/56
2021-08-25 11:59:45 -07:00

316 lines
9.9 KiB
Go

package test
// Code generated by github.com/Khan/genqlient, DO NOT EDIT.
import (
"encoding/json"
"fmt"
"github.com/Khan/genqlient/graphql"
"github.com/Khan/genqlient/internal/testutil"
)
// InterfaceNoFragmentsQueryRandomItemArticle includes the requested fields of the GraphQL type Article.
type InterfaceNoFragmentsQueryRandomItemArticle struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id testutil.ID `json:"id"`
Name string `json:"name"`
}
// InterfaceNoFragmentsQueryRandomItemContent includes the requested fields of the GraphQL type Content.
// The GraphQL type's documentation follows.
//
// Content is implemented by various types like Article, Video, and Topic.
type InterfaceNoFragmentsQueryRandomItemContent interface {
implementsGraphQLInterfaceInterfaceNoFragmentsQueryRandomItemContent()
}
func (v *InterfaceNoFragmentsQueryRandomItemArticle) implementsGraphQLInterfaceInterfaceNoFragmentsQueryRandomItemContent() {
}
func (v *InterfaceNoFragmentsQueryRandomItemVideo) implementsGraphQLInterfaceInterfaceNoFragmentsQueryRandomItemContent() {
}
func (v *InterfaceNoFragmentsQueryRandomItemTopic) implementsGraphQLInterfaceInterfaceNoFragmentsQueryRandomItemContent() {
}
func __unmarshalInterfaceNoFragmentsQueryRandomItemContent(v *InterfaceNoFragmentsQueryRandomItemContent, 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 "Article":
*v = new(InterfaceNoFragmentsQueryRandomItemArticle)
return json.Unmarshal(m, *v)
case "Video":
*v = new(InterfaceNoFragmentsQueryRandomItemVideo)
return json.Unmarshal(m, *v)
case "Topic":
*v = new(InterfaceNoFragmentsQueryRandomItemTopic)
return json.Unmarshal(m, *v)
default:
return fmt.Errorf(
`Unexpected concrete type for InterfaceNoFragmentsQueryRandomItemContent: "%v"`, tn.TypeName)
}
}
// InterfaceNoFragmentsQueryRandomItemTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNoFragmentsQueryRandomItemTopic struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id testutil.ID `json:"id"`
Name string `json:"name"`
}
// InterfaceNoFragmentsQueryRandomItemVideo includes the requested fields of the GraphQL type Video.
type InterfaceNoFragmentsQueryRandomItemVideo struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id testutil.ID `json:"id"`
Name string `json:"name"`
}
// InterfaceNoFragmentsQueryRandomItemWithTypeNameArticle includes the requested fields of the GraphQL type Article.
type InterfaceNoFragmentsQueryRandomItemWithTypeNameArticle struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id testutil.ID `json:"id"`
Name string `json:"name"`
}
// InterfaceNoFragmentsQueryRandomItemWithTypeNameContent includes the requested fields of the GraphQL type Content.
// The GraphQL type's documentation follows.
//
// Content is implemented by various types like Article, Video, and Topic.
type InterfaceNoFragmentsQueryRandomItemWithTypeNameContent interface {
implementsGraphQLInterfaceInterfaceNoFragmentsQueryRandomItemWithTypeNameContent()
}
func (v *InterfaceNoFragmentsQueryRandomItemWithTypeNameArticle) implementsGraphQLInterfaceInterfaceNoFragmentsQueryRandomItemWithTypeNameContent() {
}
func (v *InterfaceNoFragmentsQueryRandomItemWithTypeNameVideo) implementsGraphQLInterfaceInterfaceNoFragmentsQueryRandomItemWithTypeNameContent() {
}
func (v *InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic) implementsGraphQLInterfaceInterfaceNoFragmentsQueryRandomItemWithTypeNameContent() {
}
func __unmarshalInterfaceNoFragmentsQueryRandomItemWithTypeNameContent(v *InterfaceNoFragmentsQueryRandomItemWithTypeNameContent, 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 "Article":
*v = new(InterfaceNoFragmentsQueryRandomItemWithTypeNameArticle)
return json.Unmarshal(m, *v)
case "Video":
*v = new(InterfaceNoFragmentsQueryRandomItemWithTypeNameVideo)
return json.Unmarshal(m, *v)
case "Topic":
*v = new(InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic)
return json.Unmarshal(m, *v)
default:
return fmt.Errorf(
`Unexpected concrete type for InterfaceNoFragmentsQueryRandomItemWithTypeNameContent: "%v"`, tn.TypeName)
}
}
// InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id testutil.ID `json:"id"`
Name string `json:"name"`
}
// InterfaceNoFragmentsQueryRandomItemWithTypeNameVideo includes the requested fields of the GraphQL type Video.
type InterfaceNoFragmentsQueryRandomItemWithTypeNameVideo struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id testutil.ID `json:"id"`
Name string `json:"name"`
}
// InterfaceNoFragmentsQueryResponse is returned by InterfaceNoFragmentsQuery on success.
type InterfaceNoFragmentsQueryResponse struct {
Root InterfaceNoFragmentsQueryRootTopic `json:"root"`
RandomItem InterfaceNoFragmentsQueryRandomItemContent `json:"-"`
RandomItemWithTypeName InterfaceNoFragmentsQueryRandomItemWithTypeNameContent `json:"-"`
WithPointer *InterfaceNoFragmentsQueryWithPointerContent `json:"-"`
}
func (v *InterfaceNoFragmentsQueryResponse) UnmarshalJSON(b []byte) error {
type InterfaceNoFragmentsQueryResponseWrapper InterfaceNoFragmentsQueryResponse
var firstPass struct {
*InterfaceNoFragmentsQueryResponseWrapper
RandomItem json.RawMessage `json:"randomItem"`
RandomItemWithTypeName json.RawMessage `json:"randomItemWithTypeName"`
WithPointer json.RawMessage `json:"withPointer"`
}
firstPass.InterfaceNoFragmentsQueryResponseWrapper = (*InterfaceNoFragmentsQueryResponseWrapper)(v)
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
}
{
target := &v.RandomItem
raw := firstPass.RandomItem
err = __unmarshalInterfaceNoFragmentsQueryRandomItemContent(
target, raw)
if err != nil {
return err
}
}
{
target := &v.RandomItemWithTypeName
raw := firstPass.RandomItemWithTypeName
err = __unmarshalInterfaceNoFragmentsQueryRandomItemWithTypeNameContent(
target, raw)
if err != nil {
return err
}
}
{
target := &v.WithPointer
raw := firstPass.WithPointer
*target = new(InterfaceNoFragmentsQueryWithPointerContent)
err = __unmarshalInterfaceNoFragmentsQueryWithPointerContent(
*target, raw)
if err != nil {
return err
}
}
return nil
}
// InterfaceNoFragmentsQueryRootTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNoFragmentsQueryRootTopic struct {
// ID is documented in the Content interface.
Id testutil.ID `json:"id"`
Name string `json:"name"`
}
// InterfaceNoFragmentsQueryWithPointerArticle includes the requested fields of the GraphQL type Article.
type InterfaceNoFragmentsQueryWithPointerArticle struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id *testutil.ID `json:"id"`
Name *string `json:"name"`
}
// InterfaceNoFragmentsQueryWithPointerContent includes the requested fields of the GraphQL type Content.
// The GraphQL type's documentation follows.
//
// Content is implemented by various types like Article, Video, and Topic.
type InterfaceNoFragmentsQueryWithPointerContent interface {
implementsGraphQLInterfaceInterfaceNoFragmentsQueryWithPointerContent()
}
func (v *InterfaceNoFragmentsQueryWithPointerArticle) implementsGraphQLInterfaceInterfaceNoFragmentsQueryWithPointerContent() {
}
func (v *InterfaceNoFragmentsQueryWithPointerVideo) implementsGraphQLInterfaceInterfaceNoFragmentsQueryWithPointerContent() {
}
func (v *InterfaceNoFragmentsQueryWithPointerTopic) implementsGraphQLInterfaceInterfaceNoFragmentsQueryWithPointerContent() {
}
func __unmarshalInterfaceNoFragmentsQueryWithPointerContent(v *InterfaceNoFragmentsQueryWithPointerContent, 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 "Article":
*v = new(InterfaceNoFragmentsQueryWithPointerArticle)
return json.Unmarshal(m, *v)
case "Video":
*v = new(InterfaceNoFragmentsQueryWithPointerVideo)
return json.Unmarshal(m, *v)
case "Topic":
*v = new(InterfaceNoFragmentsQueryWithPointerTopic)
return json.Unmarshal(m, *v)
default:
return fmt.Errorf(
`Unexpected concrete type for InterfaceNoFragmentsQueryWithPointerContent: "%v"`, tn.TypeName)
}
}
// InterfaceNoFragmentsQueryWithPointerTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNoFragmentsQueryWithPointerTopic struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id *testutil.ID `json:"id"`
Name *string `json:"name"`
}
// InterfaceNoFragmentsQueryWithPointerVideo includes the requested fields of the GraphQL type Video.
type InterfaceNoFragmentsQueryWithPointerVideo struct {
Typename string `json:"__typename"`
// ID is the identifier of the content.
Id *testutil.ID `json:"id"`
Name *string `json:"name"`
}
func InterfaceNoFragmentsQuery(
client graphql.Client,
) (*InterfaceNoFragmentsQueryResponse, error) {
var retval InterfaceNoFragmentsQueryResponse
err := client.MakeRequest(
nil,
"InterfaceNoFragmentsQuery",
`
query InterfaceNoFragmentsQuery {
root {
id
name
}
randomItem {
__typename
id
name
}
randomItemWithTypeName: randomItem {
__typename
id
name
}
withPointer: randomItem {
__typename
id
name
}
}
`,
&retval,
nil,
)
return &retval, err
}