## Summary: In this commit I begin the journey to add the long-awaited support for interfaces (part of #8). Well, it's not the beginning: I already had some half-written broken code around. But it's the first fully functional support, and especially, the first *tested* support; it's probably best to review the nontrivially-changed code as if it were new. Conceptually, the code so far is pretty simple: we generate an interface type, and the implementations. (That code is in fact mostly unchanged.) The complexity comes in because encoding/json doesn't know how to unmarshal that. So we have to add an UnmarshalJSON method, which actually has to be on the types with interface-type fields, that knows how. I factored it into two methods, such that that UnmarshalJSON method is just glue, and then there's a separate function, corresponding to each interface-type, that actually does all the work. (If only one could just write it as an actual method!) The method uses the same trick suggested to me by a few others in another context to deserialize all but one field, then handle that field specially, which is discussed in the code. This still has some limitations, which will be lifted in future commits: - it doesn't allow for list-of-interface fields - it requires that you manually ask for `__typename` - it doesn't support fragments, i.e. you can only query for interface fields, not concrete-type-specific ones But it works, even in integration tests, which is progress! As a part of this, I added a proper config option for the "allow broken features" flag, since I need to be able to set it from the integration tests which are in a separate package (and actually shell out via `go generate`). I also renamed what was to be the first case (InterfaceNoFragments), and replaced it with a further-simplified version (avoiding list-of-interface fields. [1] https://github.com/benjaminjkraft/notes/blob/master/go-json-interfaces.md Issue: https://github.com/Khan/genqlient/issues/8 ## Test plan: make tesc 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, ✅ 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/52
120 lines
3.7 KiB
Go
120 lines
3.7 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"
|
|
)
|
|
|
|
// InterfaceNoFragmentsQueryResponse is returned by InterfaceNoFragmentsQuery on success.
|
|
type InterfaceNoFragmentsQueryResponse struct {
|
|
Root InterfaceNoFragmentsQueryRootTopic `json:"root"`
|
|
}
|
|
|
|
// 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"`
|
|
Children []InterfaceNoFragmentsQueryRootTopicChildrenContent `json:"children"`
|
|
}
|
|
|
|
// InterfaceNoFragmentsQueryRootTopicChildrenArticle includes the requested fields of the GraphQL type Article.
|
|
type InterfaceNoFragmentsQueryRootTopicChildrenArticle struct {
|
|
Typename string `json:"__typename"`
|
|
// ID is the identifier of the content.
|
|
Id testutil.ID `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// InterfaceNoFragmentsQueryRootTopicChildrenContent 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 InterfaceNoFragmentsQueryRootTopicChildrenContent interface {
|
|
implementsGraphQLInterfaceInterfaceNoFragmentsQueryRootTopicChildrenContent()
|
|
}
|
|
|
|
func (v *InterfaceNoFragmentsQueryRootTopicChildrenArticle) implementsGraphQLInterfaceInterfaceNoFragmentsQueryRootTopicChildrenContent() {
|
|
}
|
|
func (v *InterfaceNoFragmentsQueryRootTopicChildrenVideo) implementsGraphQLInterfaceInterfaceNoFragmentsQueryRootTopicChildrenContent() {
|
|
}
|
|
func (v *InterfaceNoFragmentsQueryRootTopicChildrenTopic) implementsGraphQLInterfaceInterfaceNoFragmentsQueryRootTopicChildrenContent() {
|
|
}
|
|
|
|
func __unmarshalInterfaceNoFragmentsQueryRootTopicChildrenContent(v *InterfaceNoFragmentsQueryRootTopicChildrenContent, 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(InterfaceNoFragmentsQueryRootTopicChildrenArticle)
|
|
return json.Unmarshal(m, *v)
|
|
case "Video":
|
|
*v = new(InterfaceNoFragmentsQueryRootTopicChildrenVideo)
|
|
return json.Unmarshal(m, *v)
|
|
case "Topic":
|
|
*v = new(InterfaceNoFragmentsQueryRootTopicChildrenTopic)
|
|
return json.Unmarshal(m, *v)
|
|
default:
|
|
return fmt.Errorf(
|
|
`Unexpected concrete type for InterfaceNoFragmentsQueryRootTopicChildrenContent: "%v"`, tn.TypeName)
|
|
}
|
|
}
|
|
|
|
// InterfaceNoFragmentsQueryRootTopicChildrenTopic includes the requested fields of the GraphQL type Topic.
|
|
type InterfaceNoFragmentsQueryRootTopicChildrenTopic struct {
|
|
Typename string `json:"__typename"`
|
|
// ID is the identifier of the content.
|
|
Id testutil.ID `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// InterfaceNoFragmentsQueryRootTopicChildrenVideo includes the requested fields of the GraphQL type Video.
|
|
type InterfaceNoFragmentsQueryRootTopicChildrenVideo 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
|
|
children {
|
|
__typename
|
|
id
|
|
name
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
&retval,
|
|
nil,
|
|
)
|
|
return &retval, err
|
|
}
|
|
|