Allow genqlient types to be marshaled safely (#120)

## Summary:
When genqlient generates output types, it generates whatever code is
necessary to unmarshal them.  Conversely, when it generates input types,
it generates whatever code is necessary to marshal.  This is all that's
needed for genqlient itself: it never needs to marshal output types or
unmarshal input types.

But maybe you do!  (For example, to put the responses in a cache, which
is the use case that @csilvers hit at Khan, although there are others
one can imagine.)  While we can't support every serialization format you
might want (at least not without adding plugins or some such), it's not
unreasonable to expect that since genqlient can read JSON, it can write
it too.  Sadly, in the past this was not true for types requiring custom
unmarshaling logic, for several reasons.

In this commit I implement logic to always write both marshalers and
unmarshalers whenever they're needed to be able to correctly round-trip
the types, even though genqlient doesn't do so.  I wasn't starting from
scratch, since of course we already write both marshalers and
unmarshalers in some cases.  But this ended up requiring surprisingly
large changes on the marshaling side, mostly to correctly support
embedding (which we use for named fragments).

Specifically, as the comments in `types.go` discuss, the most difficult
issue is spreads with duplicate fields, which translate to Go embedded
fields which end up hidden from the json-marshaler.  Ultimately, I had
to do things quite differently from unmarshaling, and essentially
flatten the type when we write marshaler.  But in the end it's not so
ugly -- indeed arguably it's cleaner!  Mainly it's just different.

One thing to note is that we do marshal `__typename` based on
what we know about the types; users need not fill it in (and if they
do we'll ignore it).  This seemed to me to be a better UX, and
didn't add much complexity.

In general, I begin to wonder whether using `encoding/json` at all is
really right for genqlient: we're doing a lot of work to appease it,
despite knowing what our types look like.  I think it would still be a
significant increase in lines of code to roll our own, but that code
would perhaps be simpler, and would surely be faster (although if we
just want the speed gains we could use another JSON-generator library,
see also #47).  Anyway, something to think about in the future.

## Test plan:
make tesc


Author: benjaminjkraft

Reviewers: csilvers, StevenACoffman, benjaminjkraft, dnerdy, aberkan, jvoll, mahtabsabet, MiguelCastillo

Required Reviewers: 

Approved By: StevenACoffman, dnerdy

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/120
This commit is contained in:
Ben Kraft
2021-09-29 10:30:43 -07:00
committed by GitHub
parent 1f65445127
commit f4c981031e
30 changed files with 4143 additions and 180 deletions
+2
View File
@@ -24,6 +24,8 @@ When releasing a new version:
### New features: ### New features:
- genqlient's types are now safe to JSON-marshal, which can be useful for putting them in a cache, for example. See the [docs](FAQ.md#-let-me-json-marshal-my-response-objects) for details.
### Bug fixes: ### Bug fixes:
## v0.2.0 ## v0.2.0
+13
View File
@@ -96,6 +96,19 @@ bindings:
Or, you can bind it to any other type, perhaps one with size-checked constructors; see the [`genqlient.yaml` documentation](`genqlient.yaml`) for more details. Or, you can bind it to any other type, perhaps one with size-checked constructors; see the [`genqlient.yaml` documentation](`genqlient.yaml`) for more details.
### … let me json-marshal my response objects
This is supported by default! All genqlient-generated types support both JSON-marshaling and unmarshaling, which can be useful for putting them in a cache, inspecting them by hand, using them in mocks (although this is [not recommended](#-test-my-graphql-apis)), or anything else you can do with JSON. It's not guaranteed that marshaling a genqlient type will produce the exact GraphQL input -- we try to get as close as we can but there are some limitations around Go zero values -- but unmarshaling again should produce the value genqlient returned. That is:
```go
resp, err := MyQuery(...)
// not guaranteed to match what the server sent (but close):
b, err := json.Marshal(resp)
// guaranteed to match resp:
var respAgain MyQueryResponse
err := json.Unmarshal(b, &resp)
```
## How do I make a query with … ## How do I make a query with …
### … a specific name for a field? ### … a specific name for a field?
+57 -23
View File
@@ -1,28 +1,60 @@
{{/* See unmarshal.go.tmpl for more on how this works; this is mostly just {{/* We generate MarshalJSON for much the same reasons as UnmarshalJSON -- see
parallel (and simplified -- we don't need to handle embedding). */}} unmarshal.go.tmpl for details. (Note we generate both even if genqlient
itself needs only UnmarshalJSON, for the benefit of callers who want to,
for example, put genqlient responses in a cache.) But our implementation
for marshaling is quite different.
Specifically, the treatment of field-visibility with embedded fields must
differ from both ordinary encoding/json and unmarshaling: we need to
choose exactly one conflicting field in all cases (whereas Go chooses at
most one and when unmarshaling we choose them all). See
goStructType.FlattenedFields in types.go for more discussion of embedding
and visibility.
To accomplish that, we essentially flatten out all the embedded fields
when marshaling, following those precedence rules. Then we basically
follow what we do in unmarshaling, but in reverse order: first we marshal
the special fields, then we glue everything together with the ordinary
fields.
We do one other thing differently, for the benefit of the marshal-helper
in marshal_helper.go.tmpl. While when unmarshaling it's easy to unmarshal
out the __typename field, then unmarshal out everything else, with
marshaling we can't do the same (at least not without some careful
JSON-stitching; the considerations are basically the same as those
discussed in FlattenedFields). So we write out a helper method
__premarshalJSON() which basically does all but the final JSON-marshal.
(Then the real MarshalJSON() just calls that, and then marshals.)
Thus a marshal-helper for this type, if any, can call __premarshalJSON()
directly, and embed its result. */}}
type __premarshal{{.GoName}} struct{
{{range .FlattenedFields -}}
{{if .NeedsMarshaling -}}
{{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"`
{{else}}
{{.GoName}} {{.GoType.Reference}} `json:"{{.JSONName}}"`
{{end}}
{{end}}
}
func (v *{{.GoName}}) MarshalJSON() ([]byte, error) { func (v *{{.GoName}}) MarshalJSON() ([]byte, error) {
{{/* We do the two passes in the opposite order of unmarshal: first, we premarshaled, err := v.__premarshalJSON()
marshal the special fields, then we assign those to the wrapper struct if err != nil {
and finish marshaling the whole object. But first we set up the return nil, err
object for the second part, so we can assign to it as we go. */}}
var fullObject struct{
*{{.GoName}}
{{range .Fields -}}
{{if .NeedsMarshaler -}}
{{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"`
{{end -}}
{{end -}}
{{ref "github.com/Khan/genqlient/graphql.NoMarshalJSON"}}
} }
fullObject.{{.GoName}} = v return json.Marshal(premarshaled)
}
{{range $field := .Fields -}} func (v *{{.GoName}}) __premarshalJSON() (*__premarshal{{.GoName}}, error) {
{{if $field.NeedsMarshaler -}} var retval __premarshal{{.GoName}}
{{range $field := .FlattenedFields -}}
{{if $field.NeedsMarshaling -}}
{ {
{{/* Here dst is the json.RawMessage, and src is the Go type */}} {{/* Here dst is the json.RawMessage, and src is the Go type. */}}
dst := &fullObject.{{$field.GoName}} dst := &retval.{{$field.GoName}}
src := v.{{$field.GoName}} src := v.{{$field.Selector}}
{{range $i := intRange $field.GoType.SliceDepth -}} {{range $i := intRange $field.GoType.SliceDepth -}}
*dst = make( *dst = make(
{{repeat (sub $field.GoType.SliceDepth $i) "[]"}}{{ref "encoding/json.RawMessage"}}, {{repeat (sub $field.GoType.SliceDepth $i) "[]"}}{{ref "encoding/json.RawMessage"}},
@@ -45,7 +77,7 @@ func (v *{{.GoName}}) MarshalJSON() ([]byte, error) {
{{if not $field.GoType.IsPointer}}&{{end}}src) {{if not $field.GoType.IsPointer}}&{{end}}src)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"Unable to marshal {{$.GoName}}.{{$field.GoName}}: %w", err) "Unable to marshal {{$.GoName}}.{{$field.Selector}}: %w", err)
} }
{{if $field.GoType.IsPointer -}} {{if $field.GoType.IsPointer -}}
}{{/* end if src != nil */}} }{{/* end if src != nil */}}
@@ -54,8 +86,10 @@ func (v *{{.GoName}}) MarshalJSON() ([]byte, error) {
} }
{{end -}} {{end -}}
} }
{{else -}}
retval.{{$field.GoName}} = v.{{$field.Selector}}
{{end -}}
{{end -}} {{end -}}
{{end}}
return {{ref "encoding/json.Marshal"}}(&fullObject) return &retval, nil
} }
+44
View File
@@ -0,0 +1,44 @@
{{/* This is somewhat parallel to unmarshal_helper.go.tmpl, but, as usual, in
reverse. Note the helper accepts a pointer-to-interface, for
consistency with unmarshaling and with the API we expect of custom
marshalers. */}}
func __marshal{{.GoName}}(v *{{.GoName}}) ([]byte, error) {
{{/* Determine the GraphQL typename, which the unmarshaler will need should
it be called on our output. */}}
var typename string
switch v := (*v).(type) {
{{range .Implementations -}}
case *{{.GoName}}:
typename = "{{.GraphQLName}}"
{{/* Now actually do the marshal, with the concrete type. (Go only
marshals embeds the way we want if they're structs.) Except that
won't work right if the implementation-type has its own
MarshalJSON method (maybe it in turn has an interface-typed
field), so we call the helper __premarshalJSON directly (see
marshal.go.tmpl). */}}
{{if .NeedsMarshaling -}}
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshal{{.GoName}}
}{typename, premarshaled}
{{else -}}
result := struct {
TypeName string `json:"__typename"`
*{{.GoName}}
}{typename, v}
{{end -}}
return json.Marshal(result)
{{end -}}
case nil:
return []byte("null"), nil
default:
return nil, {{ref "fmt.Errorf"}}(
`Unexpected concrete type for {{.GoName}}: "%T"`, v)
}
}
@@ -87,6 +87,42 @@ func __unmarshalComplexInlineFragmentsConflictingStuffContent(b []byte, v *Compl
} }
} }
func __marshalComplexInlineFragmentsConflictingStuffContent(v *ComplexInlineFragmentsConflictingStuffContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *ComplexInlineFragmentsConflictingStuffArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsConflictingStuffArticle
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsConflictingStuffVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsConflictingStuffVideo
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsConflictingStuffTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsConflictingStuffTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for ComplexInlineFragmentsConflictingStuffContent: "%T"`, v)
}
}
// ComplexInlineFragmentsConflictingStuffTopic includes the requested fields of the GraphQL type Topic. // ComplexInlineFragmentsConflictingStuffTopic includes the requested fields of the GraphQL type Topic.
type ComplexInlineFragmentsConflictingStuffTopic struct { type ComplexInlineFragmentsConflictingStuffTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -174,6 +210,46 @@ func __unmarshalComplexInlineFragmentsNestedStuffContent(b []byte, v *ComplexInl
} }
} }
func __marshalComplexInlineFragmentsNestedStuffContent(v *ComplexInlineFragmentsNestedStuffContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *ComplexInlineFragmentsNestedStuffArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsNestedStuffArticle
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsNestedStuffVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsNestedStuffVideo
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsNestedStuffTopic:
typename = "Topic"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalComplexInlineFragmentsNestedStuffTopic
}{typename, premarshaled}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for ComplexInlineFragmentsNestedStuffContent: "%T"`, v)
}
}
// ComplexInlineFragmentsNestedStuffTopic includes the requested fields of the GraphQL type Topic. // ComplexInlineFragmentsNestedStuffTopic includes the requested fields of the GraphQL type Topic.
type ComplexInlineFragmentsNestedStuffTopic struct { type ComplexInlineFragmentsNestedStuffTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -219,6 +295,45 @@ func (v *ComplexInlineFragmentsNestedStuffTopic) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalComplexInlineFragmentsNestedStuffTopic struct {
Typename string `json:"__typename"`
Children []json.RawMessage `json:"children"`
}
func (v *ComplexInlineFragmentsNestedStuffTopic) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *ComplexInlineFragmentsNestedStuffTopic) __premarshalJSON() (*__premarshalComplexInlineFragmentsNestedStuffTopic, error) {
var retval __premarshalComplexInlineFragmentsNestedStuffTopic
retval.Typename = v.Typename
{
dst := &retval.Children
src := v.Children
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = __marshalComplexInlineFragmentsNestedStuffTopicChildrenContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal ComplexInlineFragmentsNestedStuffTopic.Children: %w", err)
}
}
}
return &retval, nil
}
// ComplexInlineFragmentsNestedStuffTopicChildrenArticle includes the requested fields of the GraphQL type Article. // ComplexInlineFragmentsNestedStuffTopicChildrenArticle includes the requested fields of the GraphQL type Article.
type ComplexInlineFragmentsNestedStuffTopicChildrenArticle struct { type ComplexInlineFragmentsNestedStuffTopicChildrenArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -272,6 +387,42 @@ func (v *ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParen
return nil return nil
} }
type __premarshalComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopic struct {
Children []json.RawMessage `json:"children"`
}
func (v *ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopic) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopic) __premarshalJSON() (*__premarshalComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopic, error) {
var retval __premarshalComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopic
{
dst := &retval.Children
src := v.Children
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = __marshalComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopic.Children: %w", err)
}
}
}
return &retval, nil
}
// ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenArticle includes the requested fields of the GraphQL type Article. // ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenArticle includes the requested fields of the GraphQL type Article.
type ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenArticle struct { type ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -388,6 +539,42 @@ func __unmarshalComplexInlineFragmentsNestedStuffTopicChildrenArticleParentConte
} }
} }
func __marshalComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenContent(v *ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenArticle
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenVideo
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenContent: "%T"`, v)
}
}
// ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenTopic includes the requested fields of the GraphQL type Topic. // ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenTopic includes the requested fields of the GraphQL type Topic.
type ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenTopic struct { type ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -491,6 +678,42 @@ func __unmarshalComplexInlineFragmentsNestedStuffTopicChildrenContent(b []byte,
} }
} }
func __marshalComplexInlineFragmentsNestedStuffTopicChildrenContent(v *ComplexInlineFragmentsNestedStuffTopicChildrenContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *ComplexInlineFragmentsNestedStuffTopicChildrenArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsNestedStuffTopicChildrenArticle
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsNestedStuffTopicChildrenVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsNestedStuffTopicChildrenVideo
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsNestedStuffTopicChildrenTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsNestedStuffTopicChildrenTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for ComplexInlineFragmentsNestedStuffTopicChildrenContent: "%T"`, v)
}
}
// ComplexInlineFragmentsNestedStuffTopicChildrenTopic includes the requested fields of the GraphQL type Topic. // ComplexInlineFragmentsNestedStuffTopicChildrenTopic includes the requested fields of the GraphQL type Topic.
type ComplexInlineFragmentsNestedStuffTopicChildrenTopic struct { type ComplexInlineFragmentsNestedStuffTopicChildrenTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -609,6 +832,42 @@ func __unmarshalComplexInlineFragmentsRandomItemContent(b []byte, v *ComplexInli
} }
} }
func __marshalComplexInlineFragmentsRandomItemContent(v *ComplexInlineFragmentsRandomItemContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *ComplexInlineFragmentsRandomItemArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsRandomItemArticle
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsRandomItemVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsRandomItemVideo
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsRandomItemTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsRandomItemTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for ComplexInlineFragmentsRandomItemContent: "%T"`, v)
}
}
// ComplexInlineFragmentsRandomItemTopic includes the requested fields of the GraphQL type Topic. // ComplexInlineFragmentsRandomItemTopic includes the requested fields of the GraphQL type Topic.
type ComplexInlineFragmentsRandomItemTopic struct { type ComplexInlineFragmentsRandomItemTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -765,6 +1024,42 @@ func __unmarshalComplexInlineFragmentsRepeatedStuffContent(b []byte, v *ComplexI
} }
} }
func __marshalComplexInlineFragmentsRepeatedStuffContent(v *ComplexInlineFragmentsRepeatedStuffContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *ComplexInlineFragmentsRepeatedStuffArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsRepeatedStuffArticle
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsRepeatedStuffVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsRepeatedStuffVideo
}{typename, v}
return json.Marshal(result)
case *ComplexInlineFragmentsRepeatedStuffTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*ComplexInlineFragmentsRepeatedStuffTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for ComplexInlineFragmentsRepeatedStuffContent: "%T"`, v)
}
}
// ComplexInlineFragmentsRepeatedStuffTopic includes the requested fields of the GraphQL type Topic. // ComplexInlineFragmentsRepeatedStuffTopic includes the requested fields of the GraphQL type Topic.
type ComplexInlineFragmentsRepeatedStuffTopic struct { type ComplexInlineFragmentsRepeatedStuffTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -874,6 +1169,81 @@ func (v *ComplexInlineFragmentsResponse) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalComplexInlineFragmentsResponse struct {
Root ComplexInlineFragmentsRootTopic `json:"root"`
RandomItem json.RawMessage `json:"randomItem"`
RepeatedStuff json.RawMessage `json:"repeatedStuff"`
ConflictingStuff json.RawMessage `json:"conflictingStuff"`
NestedStuff json.RawMessage `json:"nestedStuff"`
}
func (v *ComplexInlineFragmentsResponse) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *ComplexInlineFragmentsResponse) __premarshalJSON() (*__premarshalComplexInlineFragmentsResponse, error) {
var retval __premarshalComplexInlineFragmentsResponse
retval.Root = v.Root
{
dst := &retval.RandomItem
src := v.RandomItem
var err error
*dst, err = __marshalComplexInlineFragmentsRandomItemContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal ComplexInlineFragmentsResponse.RandomItem: %w", err)
}
}
{
dst := &retval.RepeatedStuff
src := v.RepeatedStuff
var err error
*dst, err = __marshalComplexInlineFragmentsRepeatedStuffContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal ComplexInlineFragmentsResponse.RepeatedStuff: %w", err)
}
}
{
dst := &retval.ConflictingStuff
src := v.ConflictingStuff
var err error
*dst, err = __marshalComplexInlineFragmentsConflictingStuffContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal ComplexInlineFragmentsResponse.ConflictingStuff: %w", err)
}
}
{
dst := &retval.NestedStuff
src := v.NestedStuff
var err error
*dst, err = __marshalComplexInlineFragmentsNestedStuffContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal ComplexInlineFragmentsResponse.NestedStuff: %w", err)
}
}
return &retval, nil
}
// ComplexInlineFragmentsRootTopic includes the requested fields of the GraphQL type Topic. // ComplexInlineFragmentsRootTopic includes the requested fields of the GraphQL type Topic.
type ComplexInlineFragmentsRootTopic struct { type ComplexInlineFragmentsRootTopic struct {
// ID is documented in the Content interface. // ID is documented in the Content interface.
@@ -40,6 +40,64 @@ func (v *ComplexNamedFragmentsResponse) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalComplexNamedFragmentsResponse struct {
RandomItem json.RawMessage `json:"randomItem"`
RandomLeaf json.RawMessage `json:"randomLeaf"`
OtherLeaf json.RawMessage `json:"otherLeaf"`
}
func (v *ComplexNamedFragmentsResponse) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *ComplexNamedFragmentsResponse) __premarshalJSON() (*__premarshalComplexNamedFragmentsResponse, error) {
var retval __premarshalComplexNamedFragmentsResponse
{
dst := &retval.RandomItem
src := v.QueryFragment.InnerQueryFragment.RandomItem
var err error
*dst, err = __marshalInnerQueryFragmentRandomItemContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal ComplexNamedFragmentsResponse.QueryFragment.InnerQueryFragment.RandomItem: %w", err)
}
}
{
dst := &retval.RandomLeaf
src := v.QueryFragment.InnerQueryFragment.RandomLeaf
var err error
*dst, err = __marshalInnerQueryFragmentRandomLeafLeafContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal ComplexNamedFragmentsResponse.QueryFragment.InnerQueryFragment.RandomLeaf: %w", err)
}
}
{
dst := &retval.OtherLeaf
src := v.QueryFragment.InnerQueryFragment.OtherLeaf
var err error
*dst, err = __marshalInnerQueryFragmentOtherLeafLeafContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal ComplexNamedFragmentsResponse.QueryFragment.InnerQueryFragment.OtherLeaf: %w", err)
}
}
return &retval, nil
}
// ContentFields includes the GraphQL fields of Content requested by the fragment ContentFields. // ContentFields includes the GraphQL fields of Content requested by the fragment ContentFields.
// The GraphQL type's documentation follows. // The GraphQL type's documentation follows.
// //
@@ -113,6 +171,42 @@ func __unmarshalContentFields(b []byte, v *ContentFields) error {
} }
} }
func __marshalContentFields(v *ContentFields) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *ContentFieldsArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*ContentFieldsArticle
}{typename, v}
return json.Marshal(result)
case *ContentFieldsVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*ContentFieldsVideo
}{typename, v}
return json.Marshal(result)
case *ContentFieldsTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*ContentFieldsTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for ContentFields: "%T"`, v)
}
}
// ContentFields includes the GraphQL fields of Article requested by the fragment ContentFields. // ContentFields includes the GraphQL fields of Article requested by the fragment ContentFields.
// The GraphQL type's documentation follows. // The GraphQL type's documentation follows.
// //
@@ -211,6 +305,64 @@ func (v *InnerQueryFragment) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInnerQueryFragment struct {
RandomItem json.RawMessage `json:"randomItem"`
RandomLeaf json.RawMessage `json:"randomLeaf"`
OtherLeaf json.RawMessage `json:"otherLeaf"`
}
func (v *InnerQueryFragment) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InnerQueryFragment) __premarshalJSON() (*__premarshalInnerQueryFragment, error) {
var retval __premarshalInnerQueryFragment
{
dst := &retval.RandomItem
src := v.RandomItem
var err error
*dst, err = __marshalInnerQueryFragmentRandomItemContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InnerQueryFragment.RandomItem: %w", err)
}
}
{
dst := &retval.RandomLeaf
src := v.RandomLeaf
var err error
*dst, err = __marshalInnerQueryFragmentRandomLeafLeafContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InnerQueryFragment.RandomLeaf: %w", err)
}
}
{
dst := &retval.OtherLeaf
src := v.OtherLeaf
var err error
*dst, err = __marshalInnerQueryFragmentOtherLeafLeafContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InnerQueryFragment.OtherLeaf: %w", err)
}
}
return &retval, nil
}
// InnerQueryFragmentOtherLeafArticle includes the requested fields of the GraphQL type Article. // InnerQueryFragmentOtherLeafArticle includes the requested fields of the GraphQL type Article.
type InnerQueryFragmentOtherLeafArticle struct { type InnerQueryFragmentOtherLeafArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -242,6 +394,31 @@ func (v *InnerQueryFragmentOtherLeafArticle) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInnerQueryFragmentOtherLeafArticle struct {
Typename string `json:"__typename"`
Name string `json:"name"`
Url string `json:"url"`
}
func (v *InnerQueryFragmentOtherLeafArticle) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InnerQueryFragmentOtherLeafArticle) __premarshalJSON() (*__premarshalInnerQueryFragmentOtherLeafArticle, error) {
var retval __premarshalInnerQueryFragmentOtherLeafArticle
retval.Typename = v.Typename
retval.Name = v.ContentFieldsArticle.Name
retval.Url = v.ContentFieldsArticle.Url
return &retval, nil
}
// InnerQueryFragmentOtherLeafLeafContent includes the requested fields of the GraphQL interface LeafContent. // InnerQueryFragmentOtherLeafLeafContent includes the requested fields of the GraphQL interface LeafContent.
// //
// InnerQueryFragmentOtherLeafLeafContent is implemented by the following types: // InnerQueryFragmentOtherLeafLeafContent is implemented by the following types:
@@ -297,6 +474,42 @@ func __unmarshalInnerQueryFragmentOtherLeafLeafContent(b []byte, v *InnerQueryFr
} }
} }
func __marshalInnerQueryFragmentOtherLeafLeafContent(v *InnerQueryFragmentOtherLeafLeafContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InnerQueryFragmentOtherLeafArticle:
typename = "Article"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalInnerQueryFragmentOtherLeafArticle
}{typename, premarshaled}
return json.Marshal(result)
case *InnerQueryFragmentOtherLeafVideo:
typename = "Video"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalInnerQueryFragmentOtherLeafVideo
}{typename, premarshaled}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InnerQueryFragmentOtherLeafLeafContent: "%T"`, v)
}
}
// InnerQueryFragmentOtherLeafVideo includes the requested fields of the GraphQL type Video. // InnerQueryFragmentOtherLeafVideo includes the requested fields of the GraphQL type Video.
type InnerQueryFragmentOtherLeafVideo struct { type InnerQueryFragmentOtherLeafVideo struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -334,6 +547,37 @@ func (v *InnerQueryFragmentOtherLeafVideo) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInnerQueryFragmentOtherLeafVideo struct {
Typename string `json:"__typename"`
Id *testutil.ID `json:"id"`
Parent *MoreVideoFieldsParentTopic `json:"parent"`
Name string `json:"name"`
Url string `json:"url"`
}
func (v *InnerQueryFragmentOtherLeafVideo) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InnerQueryFragmentOtherLeafVideo) __premarshalJSON() (*__premarshalInnerQueryFragmentOtherLeafVideo, error) {
var retval __premarshalInnerQueryFragmentOtherLeafVideo
retval.Typename = v.Typename
retval.Id = v.MoreVideoFields.Id
retval.Parent = v.MoreVideoFields.Parent
retval.Name = v.ContentFieldsVideo.Name
retval.Url = v.ContentFieldsVideo.Url
return &retval, nil
}
// InnerQueryFragmentRandomItemArticle includes the requested fields of the GraphQL type Article. // InnerQueryFragmentRandomItemArticle includes the requested fields of the GraphQL type Article.
type InnerQueryFragmentRandomItemArticle struct { type InnerQueryFragmentRandomItemArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -368,6 +612,34 @@ func (v *InnerQueryFragmentRandomItemArticle) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInnerQueryFragmentRandomItemArticle struct {
Typename string `json:"__typename"`
Id testutil.ID `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
}
func (v *InnerQueryFragmentRandomItemArticle) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InnerQueryFragmentRandomItemArticle) __premarshalJSON() (*__premarshalInnerQueryFragmentRandomItemArticle, error) {
var retval __premarshalInnerQueryFragmentRandomItemArticle
retval.Typename = v.Typename
retval.Id = v.Id
retval.Name = v.Name
retval.Url = v.ContentFieldsArticle.Url
return &retval, nil
}
// InnerQueryFragmentRandomItemContent includes the requested fields of the GraphQL interface Content. // InnerQueryFragmentRandomItemContent includes the requested fields of the GraphQL interface Content.
// //
// InnerQueryFragmentRandomItemContent is implemented by the following types: // InnerQueryFragmentRandomItemContent is implemented by the following types:
@@ -459,6 +731,54 @@ func __unmarshalInnerQueryFragmentRandomItemContent(b []byte, v *InnerQueryFragm
} }
} }
func __marshalInnerQueryFragmentRandomItemContent(v *InnerQueryFragmentRandomItemContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InnerQueryFragmentRandomItemArticle:
typename = "Article"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalInnerQueryFragmentRandomItemArticle
}{typename, premarshaled}
return json.Marshal(result)
case *InnerQueryFragmentRandomItemVideo:
typename = "Video"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalInnerQueryFragmentRandomItemVideo
}{typename, premarshaled}
return json.Marshal(result)
case *InnerQueryFragmentRandomItemTopic:
typename = "Topic"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalInnerQueryFragmentRandomItemTopic
}{typename, premarshaled}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InnerQueryFragmentRandomItemContent: "%T"`, v)
}
}
// InnerQueryFragmentRandomItemTopic includes the requested fields of the GraphQL type Topic. // InnerQueryFragmentRandomItemTopic includes the requested fields of the GraphQL type Topic.
type InnerQueryFragmentRandomItemTopic struct { type InnerQueryFragmentRandomItemTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -493,6 +813,34 @@ func (v *InnerQueryFragmentRandomItemTopic) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInnerQueryFragmentRandomItemTopic struct {
Typename string `json:"__typename"`
Id testutil.ID `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
}
func (v *InnerQueryFragmentRandomItemTopic) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InnerQueryFragmentRandomItemTopic) __premarshalJSON() (*__premarshalInnerQueryFragmentRandomItemTopic, error) {
var retval __premarshalInnerQueryFragmentRandomItemTopic
retval.Typename = v.Typename
retval.Id = v.Id
retval.Name = v.Name
retval.Url = v.ContentFieldsTopic.Url
return &retval, nil
}
// InnerQueryFragmentRandomItemVideo includes the requested fields of the GraphQL type Video. // InnerQueryFragmentRandomItemVideo includes the requested fields of the GraphQL type Video.
type InnerQueryFragmentRandomItemVideo struct { type InnerQueryFragmentRandomItemVideo struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -533,6 +881,40 @@ func (v *InnerQueryFragmentRandomItemVideo) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInnerQueryFragmentRandomItemVideo struct {
Typename string `json:"__typename"`
Id testutil.ID `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Duration int `json:"duration"`
Thumbnail VideoFieldsThumbnail `json:"thumbnail"`
}
func (v *InnerQueryFragmentRandomItemVideo) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InnerQueryFragmentRandomItemVideo) __premarshalJSON() (*__premarshalInnerQueryFragmentRandomItemVideo, error) {
var retval __premarshalInnerQueryFragmentRandomItemVideo
retval.Typename = v.Typename
retval.Id = v.Id
retval.Name = v.Name
retval.Url = v.VideoFields.Url
retval.Duration = v.VideoFields.Duration
retval.Thumbnail = v.VideoFields.Thumbnail
return &retval, nil
}
// InnerQueryFragmentRandomLeafArticle includes the requested fields of the GraphQL type Article. // InnerQueryFragmentRandomLeafArticle includes the requested fields of the GraphQL type Article.
type InnerQueryFragmentRandomLeafArticle struct { type InnerQueryFragmentRandomLeafArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -564,6 +946,31 @@ func (v *InnerQueryFragmentRandomLeafArticle) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInnerQueryFragmentRandomLeafArticle struct {
Typename string `json:"__typename"`
Name string `json:"name"`
Url string `json:"url"`
}
func (v *InnerQueryFragmentRandomLeafArticle) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InnerQueryFragmentRandomLeafArticle) __premarshalJSON() (*__premarshalInnerQueryFragmentRandomLeafArticle, error) {
var retval __premarshalInnerQueryFragmentRandomLeafArticle
retval.Typename = v.Typename
retval.Name = v.ContentFieldsArticle.Name
retval.Url = v.ContentFieldsArticle.Url
return &retval, nil
}
// InnerQueryFragmentRandomLeafLeafContent includes the requested fields of the GraphQL interface LeafContent. // InnerQueryFragmentRandomLeafLeafContent includes the requested fields of the GraphQL interface LeafContent.
// //
// InnerQueryFragmentRandomLeafLeafContent is implemented by the following types: // InnerQueryFragmentRandomLeafLeafContent is implemented by the following types:
@@ -619,6 +1026,42 @@ func __unmarshalInnerQueryFragmentRandomLeafLeafContent(b []byte, v *InnerQueryF
} }
} }
func __marshalInnerQueryFragmentRandomLeafLeafContent(v *InnerQueryFragmentRandomLeafLeafContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InnerQueryFragmentRandomLeafArticle:
typename = "Article"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalInnerQueryFragmentRandomLeafArticle
}{typename, premarshaled}
return json.Marshal(result)
case *InnerQueryFragmentRandomLeafVideo:
typename = "Video"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalInnerQueryFragmentRandomLeafVideo
}{typename, premarshaled}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InnerQueryFragmentRandomLeafLeafContent: "%T"`, v)
}
}
// InnerQueryFragmentRandomLeafVideo includes the requested fields of the GraphQL type Video. // InnerQueryFragmentRandomLeafVideo includes the requested fields of the GraphQL type Video.
type InnerQueryFragmentRandomLeafVideo struct { type InnerQueryFragmentRandomLeafVideo struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -662,6 +1105,43 @@ func (v *InnerQueryFragmentRandomLeafVideo) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInnerQueryFragmentRandomLeafVideo struct {
Typename string `json:"__typename"`
Id testutil.ID `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Duration int `json:"duration"`
Thumbnail VideoFieldsThumbnail `json:"thumbnail"`
Parent *MoreVideoFieldsParentTopic `json:"parent"`
}
func (v *InnerQueryFragmentRandomLeafVideo) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InnerQueryFragmentRandomLeafVideo) __premarshalJSON() (*__premarshalInnerQueryFragmentRandomLeafVideo, error) {
var retval __premarshalInnerQueryFragmentRandomLeafVideo
retval.Typename = v.Typename
retval.Id = v.VideoFields.Id
retval.Name = v.VideoFields.Name
retval.Url = v.VideoFields.Url
retval.Duration = v.VideoFields.Duration
retval.Thumbnail = v.VideoFields.Thumbnail
retval.Parent = v.MoreVideoFields.Parent
return &retval, nil
}
// MoreVideoFields includes the GraphQL fields of Video requested by the fragment MoreVideoFields. // MoreVideoFields includes the GraphQL fields of Video requested by the fragment MoreVideoFields.
type MoreVideoFields struct { type MoreVideoFields struct {
// ID is documented in the Content interface. // ID is documented in the Content interface.
@@ -722,6 +1202,48 @@ func (v *MoreVideoFieldsParentTopic) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalMoreVideoFieldsParentTopic struct {
Name *string `json:"name"`
Url *string `json:"url"`
Children []json.RawMessage `json:"children"`
}
func (v *MoreVideoFieldsParentTopic) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *MoreVideoFieldsParentTopic) __premarshalJSON() (*__premarshalMoreVideoFieldsParentTopic, error) {
var retval __premarshalMoreVideoFieldsParentTopic
retval.Name = v.Name
retval.Url = v.Url
{
dst := &retval.Children
src := v.Children
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = __marshalMoreVideoFieldsParentTopicChildrenContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal MoreVideoFieldsParentTopic.Children: %w", err)
}
}
}
return &retval, nil
}
// MoreVideoFieldsParentTopicChildrenArticle includes the requested fields of the GraphQL type Article. // MoreVideoFieldsParentTopicChildrenArticle includes the requested fields of the GraphQL type Article.
type MoreVideoFieldsParentTopicChildrenArticle struct { type MoreVideoFieldsParentTopicChildrenArticle struct {
Typename *string `json:"__typename"` Typename *string `json:"__typename"`
@@ -792,6 +1314,46 @@ func __unmarshalMoreVideoFieldsParentTopicChildrenContent(b []byte, v *MoreVideo
} }
} }
func __marshalMoreVideoFieldsParentTopicChildrenContent(v *MoreVideoFieldsParentTopicChildrenContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *MoreVideoFieldsParentTopicChildrenArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*MoreVideoFieldsParentTopicChildrenArticle
}{typename, v}
return json.Marshal(result)
case *MoreVideoFieldsParentTopicChildrenVideo:
typename = "Video"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalMoreVideoFieldsParentTopicChildrenVideo
}{typename, premarshaled}
return json.Marshal(result)
case *MoreVideoFieldsParentTopicChildrenTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*MoreVideoFieldsParentTopicChildrenTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for MoreVideoFieldsParentTopicChildrenContent: "%T"`, v)
}
}
// MoreVideoFieldsParentTopicChildrenTopic includes the requested fields of the GraphQL type Topic. // MoreVideoFieldsParentTopicChildrenTopic includes the requested fields of the GraphQL type Topic.
type MoreVideoFieldsParentTopicChildrenTopic struct { type MoreVideoFieldsParentTopicChildrenTopic struct {
Typename *string `json:"__typename"` Typename *string `json:"__typename"`
@@ -828,6 +1390,40 @@ func (v *MoreVideoFieldsParentTopicChildrenVideo) UnmarshalJSON(b []byte) error
return nil return nil
} }
type __premarshalMoreVideoFieldsParentTopicChildrenVideo struct {
Typename *string `json:"__typename"`
Id testutil.ID `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Duration int `json:"duration"`
Thumbnail VideoFieldsThumbnail `json:"thumbnail"`
}
func (v *MoreVideoFieldsParentTopicChildrenVideo) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *MoreVideoFieldsParentTopicChildrenVideo) __premarshalJSON() (*__premarshalMoreVideoFieldsParentTopicChildrenVideo, error) {
var retval __premarshalMoreVideoFieldsParentTopicChildrenVideo
retval.Typename = v.Typename
retval.Id = v.VideoFields.Id
retval.Name = v.VideoFields.Name
retval.Url = v.VideoFields.Url
retval.Duration = v.VideoFields.Duration
retval.Thumbnail = v.VideoFields.Thumbnail
return &retval, nil
}
// QueryFragment includes the GraphQL fields of Query requested by the fragment QueryFragment. // QueryFragment includes the GraphQL fields of Query requested by the fragment QueryFragment.
// The GraphQL type's documentation follows. // The GraphQL type's documentation follows.
// //
@@ -861,6 +1457,64 @@ func (v *QueryFragment) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalQueryFragment struct {
RandomItem json.RawMessage `json:"randomItem"`
RandomLeaf json.RawMessage `json:"randomLeaf"`
OtherLeaf json.RawMessage `json:"otherLeaf"`
}
func (v *QueryFragment) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *QueryFragment) __premarshalJSON() (*__premarshalQueryFragment, error) {
var retval __premarshalQueryFragment
{
dst := &retval.RandomItem
src := v.InnerQueryFragment.RandomItem
var err error
*dst, err = __marshalInnerQueryFragmentRandomItemContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal QueryFragment.InnerQueryFragment.RandomItem: %w", err)
}
}
{
dst := &retval.RandomLeaf
src := v.InnerQueryFragment.RandomLeaf
var err error
*dst, err = __marshalInnerQueryFragmentRandomLeafLeafContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal QueryFragment.InnerQueryFragment.RandomLeaf: %w", err)
}
}
{
dst := &retval.OtherLeaf
src := v.InnerQueryFragment.OtherLeaf
var err error
*dst, err = __marshalInnerQueryFragmentOtherLeafLeafContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal QueryFragment.InnerQueryFragment.OtherLeaf: %w", err)
}
}
return &retval, nil
}
// VideoFields includes the GraphQL fields of Video requested by the fragment VideoFields. // VideoFields includes the GraphQL fields of Video requested by the fragment VideoFields.
type VideoFields struct { type VideoFields struct {
// ID is documented in the Content interface. // ID is documented in the Content interface.
@@ -897,6 +1551,37 @@ func (v *VideoFields) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalVideoFields struct {
Id testutil.ID `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Duration int `json:"duration"`
Thumbnail VideoFieldsThumbnail `json:"thumbnail"`
}
func (v *VideoFields) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *VideoFields) __premarshalJSON() (*__premarshalVideoFields, error) {
var retval __premarshalVideoFields
retval.Id = v.Id
retval.Name = v.Name
retval.Url = v.Url
retval.Duration = v.Duration
retval.Thumbnail = v.Thumbnail
return &retval, nil
}
// VideoFieldsThumbnail includes the requested fields of the GraphQL type Thumbnail. // VideoFieldsThumbnail includes the requested fields of the GraphQL type Thumbnail.
type VideoFieldsThumbnail struct { type VideoFieldsThumbnail struct {
Id testutil.ID `json:"id"` Id testutil.ID `json:"id"`
@@ -61,23 +61,95 @@ func (v *CustomMarshalUsersBornOnUser) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalCustomMarshalUsersBornOnUser struct {
Id testutil.ID `json:"id"`
Birthdate json.RawMessage `json:"birthdate"`
}
func (v *CustomMarshalUsersBornOnUser) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *CustomMarshalUsersBornOnUser) __premarshalJSON() (*__premarshalCustomMarshalUsersBornOnUser, error) {
var retval __premarshalCustomMarshalUsersBornOnUser
retval.Id = v.Id
{
dst := &retval.Birthdate
src := v.Birthdate
var err error
*dst, err = testutil.MarshalDate(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal CustomMarshalUsersBornOnUser.Birthdate: %w", err)
}
}
return &retval, nil
}
// __CustomMarshalInput is used internally by genqlient // __CustomMarshalInput is used internally by genqlient
type __CustomMarshalInput struct { type __CustomMarshalInput struct {
Date time.Time `json:"-"` Date time.Time `json:"-"`
} }
func (v *__CustomMarshalInput) MarshalJSON() ([]byte, error) { func (v *__CustomMarshalInput) UnmarshalJSON(b []byte) error {
var fullObject struct { if string(b) == "null" {
return nil
}
var firstPass struct {
*__CustomMarshalInput *__CustomMarshalInput
Date json.RawMessage `json:"date"` Date json.RawMessage `json:"date"`
graphql.NoMarshalJSON graphql.NoUnmarshalJSON
} }
fullObject.__CustomMarshalInput = v firstPass.__CustomMarshalInput = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
}
{
dst := &v.Date
src := firstPass.Date
if len(src) != 0 && string(src) != "null" {
err = testutil.UnmarshalDate(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal __CustomMarshalInput.Date: %w", err)
}
}
}
return nil
}
type __premarshal__CustomMarshalInput struct {
Date json.RawMessage `json:"date"`
}
func (v *__CustomMarshalInput) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *__CustomMarshalInput) __premarshalJSON() (*__premarshal__CustomMarshalInput, error) {
var retval __premarshal__CustomMarshalInput
{ {
dst := &fullObject.Date dst := &retval.Date
src := v.Date src := v.Date
var err error var err error
*dst, err = testutil.MarshalDate( *dst, err = testutil.MarshalDate(
@@ -87,8 +159,7 @@ func (v *__CustomMarshalInput) MarshalJSON() ([]byte, error) {
"Unable to marshal __CustomMarshalInput.Date: %w", err) "Unable to marshal __CustomMarshalInput.Date: %w", err)
} }
} }
return &retval, nil
return json.Marshal(&fullObject)
} }
func CustomMarshal( func CustomMarshal(
@@ -23,19 +23,110 @@ type __CustomMarshalSliceInput struct {
Datesssp [][][]*time.Time `json:"-"` Datesssp [][][]*time.Time `json:"-"`
} }
func (v *__CustomMarshalSliceInput) MarshalJSON() ([]byte, error) { func (v *__CustomMarshalSliceInput) UnmarshalJSON(b []byte) error {
var fullObject struct { if string(b) == "null" {
return nil
}
var firstPass struct {
*__CustomMarshalSliceInput *__CustomMarshalSliceInput
Datesss [][][]json.RawMessage `json:"datesss"` Datesss [][][]json.RawMessage `json:"datesss"`
Datesssp [][][]json.RawMessage `json:"datesssp"` Datesssp [][][]json.RawMessage `json:"datesssp"`
graphql.NoMarshalJSON graphql.NoUnmarshalJSON
} }
fullObject.__CustomMarshalSliceInput = v firstPass.__CustomMarshalSliceInput = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
}
{
dst := &v.Datesss
src := firstPass.Datesss
*dst = make(
[][][]time.Time,
len(src))
for i, src := range src {
dst := &(*dst)[i]
*dst = make(
[][]time.Time,
len(src))
for i, src := range src {
dst := &(*dst)[i]
*dst = make(
[]time.Time,
len(src))
for i, src := range src {
dst := &(*dst)[i]
if len(src) != 0 && string(src) != "null" {
err = testutil.UnmarshalDate(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal __CustomMarshalSliceInput.Datesss: %w", err)
}
}
}
}
}
}
{
dst := &v.Datesssp
src := firstPass.Datesssp
*dst = make(
[][][]*time.Time,
len(src))
for i, src := range src {
dst := &(*dst)[i]
*dst = make(
[][]*time.Time,
len(src))
for i, src := range src {
dst := &(*dst)[i]
*dst = make(
[]*time.Time,
len(src))
for i, src := range src {
dst := &(*dst)[i]
if len(src) != 0 && string(src) != "null" {
*dst = new(time.Time)
err = testutil.UnmarshalDate(
src, *dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal __CustomMarshalSliceInput.Datesssp: %w", err)
}
}
}
}
}
}
return nil
}
type __premarshal__CustomMarshalSliceInput struct {
Datesss [][][]json.RawMessage `json:"datesss"`
Datesssp [][][]json.RawMessage `json:"datesssp"`
}
func (v *__CustomMarshalSliceInput) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *__CustomMarshalSliceInput) __premarshalJSON() (*__premarshal__CustomMarshalSliceInput, error) {
var retval __premarshal__CustomMarshalSliceInput
{ {
dst := &fullObject.Datesss dst := &retval.Datesss
src := v.Datesss src := v.Datesss
*dst = make( *dst = make(
[][][]json.RawMessage, [][][]json.RawMessage,
@@ -65,7 +156,7 @@ func (v *__CustomMarshalSliceInput) MarshalJSON() ([]byte, error) {
} }
{ {
dst := &fullObject.Datesssp dst := &retval.Datesssp
src := v.Datesssp src := v.Datesssp
*dst = make( *dst = make(
[][][]json.RawMessage, [][][]json.RawMessage,
@@ -95,8 +186,7 @@ func (v *__CustomMarshalSliceInput) MarshalJSON() ([]byte, error) {
} }
} }
} }
return &retval, nil
return json.Marshal(&fullObject)
} }
func CustomMarshalSlice( func CustomMarshalSlice(
@@ -61,18 +61,75 @@ type UserQueryInput struct {
Birthdate time.Time `json:"-"` Birthdate time.Time `json:"-"`
} }
func (v *UserQueryInput) MarshalJSON() ([]byte, error) { func (v *UserQueryInput) UnmarshalJSON(b []byte) error {
var fullObject struct { if string(b) == "null" {
return nil
}
var firstPass struct {
*UserQueryInput *UserQueryInput
Birthdate json.RawMessage `json:"birthdate"` Birthdate json.RawMessage `json:"birthdate"`
graphql.NoMarshalJSON graphql.NoUnmarshalJSON
}
firstPass.UserQueryInput = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
} }
fullObject.UserQueryInput = v
{ {
dst := &v.Birthdate
src := firstPass.Birthdate
if len(src) != 0 && string(src) != "null" {
err = testutil.UnmarshalDate(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal UserQueryInput.Birthdate: %w", err)
}
}
}
return nil
}
dst := &fullObject.Birthdate type __premarshalUserQueryInput struct {
Email string `json:"email"`
Name string `json:"name"`
Id testutil.ID `json:"id"`
Role Role `json:"role"`
Names []string `json:"names"`
HasPokemon testutil.Pokemon `json:"hasPokemon"`
Birthdate json.RawMessage `json:"birthdate"`
}
func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *UserQueryInput) __premarshalJSON() (*__premarshalUserQueryInput, error) {
var retval __premarshalUserQueryInput
retval.Email = v.Email
retval.Name = v.Name
retval.Id = v.Id
retval.Role = v.Role
retval.Names = v.Names
retval.HasPokemon = v.HasPokemon
{
dst := &retval.Birthdate
src := v.Birthdate src := v.Birthdate
var err error var err error
*dst, err = testutil.MarshalDate( *dst, err = testutil.MarshalDate(
@@ -82,8 +139,7 @@ func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
"Unable to marshal UserQueryInput.Birthdate: %w", err) "Unable to marshal UserQueryInput.Birthdate: %w", err)
} }
} }
return &retval, nil
return json.Marshal(&fullObject)
} }
// __InputObjectQueryInput is used internally by genqlient // __InputObjectQueryInput is used internally by genqlient
@@ -63,6 +63,48 @@ func (v *InterfaceListFieldRootTopic) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInterfaceListFieldRootTopic struct {
Id testutil.ID `json:"id"`
Name string `json:"name"`
Children []json.RawMessage `json:"children"`
}
func (v *InterfaceListFieldRootTopic) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InterfaceListFieldRootTopic) __premarshalJSON() (*__premarshalInterfaceListFieldRootTopic, error) {
var retval __premarshalInterfaceListFieldRootTopic
retval.Id = v.Id
retval.Name = v.Name
{
dst := &retval.Children
src := v.Children
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = __marshalInterfaceListFieldRootTopicChildrenContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InterfaceListFieldRootTopic.Children: %w", err)
}
}
}
return &retval, nil
}
// InterfaceListFieldRootTopicChildrenArticle includes the requested fields of the GraphQL type Article. // InterfaceListFieldRootTopicChildrenArticle includes the requested fields of the GraphQL type Article.
type InterfaceListFieldRootTopicChildrenArticle struct { type InterfaceListFieldRootTopicChildrenArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -161,6 +203,42 @@ func __unmarshalInterfaceListFieldRootTopicChildrenContent(b []byte, v *Interfac
} }
} }
func __marshalInterfaceListFieldRootTopicChildrenContent(v *InterfaceListFieldRootTopicChildrenContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InterfaceListFieldRootTopicChildrenArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListFieldRootTopicChildrenArticle
}{typename, v}
return json.Marshal(result)
case *InterfaceListFieldRootTopicChildrenVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListFieldRootTopicChildrenVideo
}{typename, v}
return json.Marshal(result)
case *InterfaceListFieldRootTopicChildrenTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListFieldRootTopicChildrenTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InterfaceListFieldRootTopicChildrenContent: "%T"`, v)
}
}
// InterfaceListFieldRootTopicChildrenTopic includes the requested fields of the GraphQL type Topic. // InterfaceListFieldRootTopicChildrenTopic includes the requested fields of the GraphQL type Topic.
type InterfaceListFieldRootTopicChildrenTopic struct { type InterfaceListFieldRootTopicChildrenTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -224,6 +302,48 @@ func (v *InterfaceListFieldWithPointerTopic) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInterfaceListFieldWithPointerTopic struct {
Id testutil.ID `json:"id"`
Name string `json:"name"`
Children []json.RawMessage `json:"children"`
}
func (v *InterfaceListFieldWithPointerTopic) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InterfaceListFieldWithPointerTopic) __premarshalJSON() (*__premarshalInterfaceListFieldWithPointerTopic, error) {
var retval __premarshalInterfaceListFieldWithPointerTopic
retval.Id = v.Id
retval.Name = v.Name
{
dst := &retval.Children
src := v.Children
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = __marshalInterfaceListFieldWithPointerTopicChildrenContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InterfaceListFieldWithPointerTopic.Children: %w", err)
}
}
}
return &retval, nil
}
// InterfaceListFieldWithPointerTopicChildrenArticle includes the requested fields of the GraphQL type Article. // InterfaceListFieldWithPointerTopicChildrenArticle includes the requested fields of the GraphQL type Article.
type InterfaceListFieldWithPointerTopicChildrenArticle struct { type InterfaceListFieldWithPointerTopicChildrenArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -322,6 +442,42 @@ func __unmarshalInterfaceListFieldWithPointerTopicChildrenContent(b []byte, v *I
} }
} }
func __marshalInterfaceListFieldWithPointerTopicChildrenContent(v *InterfaceListFieldWithPointerTopicChildrenContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InterfaceListFieldWithPointerTopicChildrenArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListFieldWithPointerTopicChildrenArticle
}{typename, v}
return json.Marshal(result)
case *InterfaceListFieldWithPointerTopicChildrenVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListFieldWithPointerTopicChildrenVideo
}{typename, v}
return json.Marshal(result)
case *InterfaceListFieldWithPointerTopicChildrenTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListFieldWithPointerTopicChildrenTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InterfaceListFieldWithPointerTopicChildrenContent: "%T"`, v)
}
}
// InterfaceListFieldWithPointerTopicChildrenTopic includes the requested fields of the GraphQL type Topic. // InterfaceListFieldWithPointerTopicChildrenTopic includes the requested fields of the GraphQL type Topic.
type InterfaceListFieldWithPointerTopicChildrenTopic struct { type InterfaceListFieldWithPointerTopicChildrenTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -118,6 +118,42 @@ func __unmarshalInterfaceListOfListOfListsFieldListOfListsOfListsOfContent(b []b
} }
} }
func __marshalInterfaceListOfListOfListsFieldListOfListsOfListsOfContent(v *InterfaceListOfListOfListsFieldListOfListsOfListsOfContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InterfaceListOfListOfListsFieldListOfListsOfListsOfContentArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListOfListOfListsFieldListOfListsOfListsOfContentArticle
}{typename, v}
return json.Marshal(result)
case *InterfaceListOfListOfListsFieldListOfListsOfListsOfContentVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListOfListOfListsFieldListOfListsOfListsOfContentVideo
}{typename, v}
return json.Marshal(result)
case *InterfaceListOfListOfListsFieldListOfListsOfListsOfContentTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListOfListOfListsFieldListOfListsOfListsOfContentTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InterfaceListOfListOfListsFieldListOfListsOfListsOfContent: "%T"`, v)
}
}
// InterfaceListOfListOfListsFieldListOfListsOfListsOfContentArticle includes the requested fields of the GraphQL type Article. // InterfaceListOfListOfListsFieldListOfListsOfListsOfContentArticle includes the requested fields of the GraphQL type Article.
type InterfaceListOfListOfListsFieldListOfListsOfListsOfContentArticle struct { type InterfaceListOfListOfListsFieldListOfListsOfListsOfContentArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -232,6 +268,88 @@ func (v *InterfaceListOfListOfListsFieldResponse) UnmarshalJSON(b []byte) error
return nil return nil
} }
type __premarshalInterfaceListOfListOfListsFieldResponse struct {
ListOfListsOfListsOfContent [][][]json.RawMessage `json:"listOfListsOfListsOfContent"`
WithPointer [][][]json.RawMessage `json:"withPointer"`
}
func (v *InterfaceListOfListOfListsFieldResponse) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InterfaceListOfListOfListsFieldResponse) __premarshalJSON() (*__premarshalInterfaceListOfListOfListsFieldResponse, error) {
var retval __premarshalInterfaceListOfListOfListsFieldResponse
{
dst := &retval.ListOfListsOfListsOfContent
src := v.ListOfListsOfListsOfContent
*dst = make(
[][][]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
*dst = make(
[][]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = __marshalInterfaceListOfListOfListsFieldListOfListsOfListsOfContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InterfaceListOfListOfListsFieldResponse.ListOfListsOfListsOfContent: %w", err)
}
}
}
}
}
{
dst := &retval.WithPointer
src := v.WithPointer
*dst = make(
[][][]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
*dst = make(
[][]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
if src != nil {
var err error
*dst, err = __marshalInterfaceListOfListOfListsFieldWithPointerContent(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InterfaceListOfListOfListsFieldResponse.WithPointer: %w", err)
}
}
}
}
}
}
return &retval, nil
}
// InterfaceListOfListOfListsFieldWithPointerArticle includes the requested fields of the GraphQL type Article. // InterfaceListOfListOfListsFieldWithPointerArticle includes the requested fields of the GraphQL type Article.
type InterfaceListOfListOfListsFieldWithPointerArticle struct { type InterfaceListOfListOfListsFieldWithPointerArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -330,6 +448,42 @@ func __unmarshalInterfaceListOfListOfListsFieldWithPointerContent(b []byte, v *I
} }
} }
func __marshalInterfaceListOfListOfListsFieldWithPointerContent(v *InterfaceListOfListOfListsFieldWithPointerContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InterfaceListOfListOfListsFieldWithPointerArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListOfListOfListsFieldWithPointerArticle
}{typename, v}
return json.Marshal(result)
case *InterfaceListOfListOfListsFieldWithPointerVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListOfListOfListsFieldWithPointerVideo
}{typename, v}
return json.Marshal(result)
case *InterfaceListOfListOfListsFieldWithPointerTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*InterfaceListOfListOfListsFieldWithPointerTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InterfaceListOfListOfListsFieldWithPointerContent: "%T"`, v)
}
}
// InterfaceListOfListOfListsFieldWithPointerTopic includes the requested fields of the GraphQL type Topic. // InterfaceListOfListOfListsFieldWithPointerTopic includes the requested fields of the GraphQL type Topic.
type InterfaceListOfListOfListsFieldWithPointerTopic struct { type InterfaceListOfListOfListsFieldWithPointerTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -61,6 +61,45 @@ func (v *InterfaceNestingRootTopic) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInterfaceNestingRootTopic struct {
Id testutil.ID `json:"id"`
Children []json.RawMessage `json:"children"`
}
func (v *InterfaceNestingRootTopic) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InterfaceNestingRootTopic) __premarshalJSON() (*__premarshalInterfaceNestingRootTopic, error) {
var retval __premarshalInterfaceNestingRootTopic
retval.Id = v.Id
{
dst := &retval.Children
src := v.Children
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = __marshalInterfaceNestingRootTopicChildrenContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InterfaceNestingRootTopic.Children: %w", err)
}
}
}
return &retval, nil
}
// InterfaceNestingRootTopicChildrenArticle includes the requested fields of the GraphQL type Article. // InterfaceNestingRootTopicChildrenArticle includes the requested fields of the GraphQL type Article.
type InterfaceNestingRootTopicChildrenArticle struct { type InterfaceNestingRootTopicChildrenArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -165,6 +204,42 @@ func __unmarshalInterfaceNestingRootTopicChildrenContent(b []byte, v *InterfaceN
} }
} }
func __marshalInterfaceNestingRootTopicChildrenContent(v *InterfaceNestingRootTopicChildrenContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InterfaceNestingRootTopicChildrenArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNestingRootTopicChildrenArticle
}{typename, v}
return json.Marshal(result)
case *InterfaceNestingRootTopicChildrenVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNestingRootTopicChildrenVideo
}{typename, v}
return json.Marshal(result)
case *InterfaceNestingRootTopicChildrenTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNestingRootTopicChildrenTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InterfaceNestingRootTopicChildrenContent: "%T"`, v)
}
}
// InterfaceNestingRootTopicChildrenContentParentTopic includes the requested fields of the GraphQL type Topic. // InterfaceNestingRootTopicChildrenContentParentTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNestingRootTopicChildrenContentParentTopic struct { type InterfaceNestingRootTopicChildrenContentParentTopic struct {
// ID is documented in the Content interface. // ID is documented in the Content interface.
@@ -211,6 +286,45 @@ func (v *InterfaceNestingRootTopicChildrenContentParentTopic) UnmarshalJSON(b []
return nil return nil
} }
type __premarshalInterfaceNestingRootTopicChildrenContentParentTopic struct {
Id testutil.ID `json:"id"`
Children []json.RawMessage `json:"children"`
}
func (v *InterfaceNestingRootTopicChildrenContentParentTopic) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InterfaceNestingRootTopicChildrenContentParentTopic) __premarshalJSON() (*__premarshalInterfaceNestingRootTopicChildrenContentParentTopic, error) {
var retval __premarshalInterfaceNestingRootTopicChildrenContentParentTopic
retval.Id = v.Id
{
dst := &retval.Children
src := v.Children
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = __marshalInterfaceNestingRootTopicChildrenContentParentTopicChildrenContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InterfaceNestingRootTopicChildrenContentParentTopic.Children: %w", err)
}
}
}
return &retval, nil
}
// InterfaceNestingRootTopicChildrenContentParentTopicChildrenArticle includes the requested fields of the GraphQL type Article. // InterfaceNestingRootTopicChildrenContentParentTopicChildrenArticle includes the requested fields of the GraphQL type Article.
type InterfaceNestingRootTopicChildrenContentParentTopicChildrenArticle struct { type InterfaceNestingRootTopicChildrenContentParentTopicChildrenArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -309,6 +423,42 @@ func __unmarshalInterfaceNestingRootTopicChildrenContentParentTopicChildrenConte
} }
} }
func __marshalInterfaceNestingRootTopicChildrenContentParentTopicChildrenContent(v *InterfaceNestingRootTopicChildrenContentParentTopicChildrenContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InterfaceNestingRootTopicChildrenContentParentTopicChildrenArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNestingRootTopicChildrenContentParentTopicChildrenArticle
}{typename, v}
return json.Marshal(result)
case *InterfaceNestingRootTopicChildrenContentParentTopicChildrenVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNestingRootTopicChildrenContentParentTopicChildrenVideo
}{typename, v}
return json.Marshal(result)
case *InterfaceNestingRootTopicChildrenContentParentTopicChildrenTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNestingRootTopicChildrenContentParentTopicChildrenTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InterfaceNestingRootTopicChildrenContentParentTopicChildrenContent: "%T"`, v)
}
}
// InterfaceNestingRootTopicChildrenContentParentTopicChildrenTopic includes the requested fields of the GraphQL type Topic. // InterfaceNestingRootTopicChildrenContentParentTopicChildrenTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNestingRootTopicChildrenContentParentTopicChildrenTopic struct { type InterfaceNestingRootTopicChildrenContentParentTopicChildrenTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -108,6 +108,42 @@ func __unmarshalInterfaceNoFragmentsQueryRandomItemContent(b []byte, v *Interfac
} }
} }
func __marshalInterfaceNoFragmentsQueryRandomItemContent(v *InterfaceNoFragmentsQueryRandomItemContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InterfaceNoFragmentsQueryRandomItemArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNoFragmentsQueryRandomItemArticle
}{typename, v}
return json.Marshal(result)
case *InterfaceNoFragmentsQueryRandomItemVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNoFragmentsQueryRandomItemVideo
}{typename, v}
return json.Marshal(result)
case *InterfaceNoFragmentsQueryRandomItemTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNoFragmentsQueryRandomItemTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InterfaceNoFragmentsQueryRandomItemContent: "%T"`, v)
}
}
// InterfaceNoFragmentsQueryRandomItemTopic includes the requested fields of the GraphQL type Topic. // InterfaceNoFragmentsQueryRandomItemTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNoFragmentsQueryRandomItemTopic struct { type InterfaceNoFragmentsQueryRandomItemTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -228,6 +264,42 @@ func __unmarshalInterfaceNoFragmentsQueryRandomItemWithTypeNameContent(b []byte,
} }
} }
func __marshalInterfaceNoFragmentsQueryRandomItemWithTypeNameContent(v *InterfaceNoFragmentsQueryRandomItemWithTypeNameContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InterfaceNoFragmentsQueryRandomItemWithTypeNameArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNoFragmentsQueryRandomItemWithTypeNameArticle
}{typename, v}
return json.Marshal(result)
case *InterfaceNoFragmentsQueryRandomItemWithTypeNameVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNoFragmentsQueryRandomItemWithTypeNameVideo
}{typename, v}
return json.Marshal(result)
case *InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InterfaceNoFragmentsQueryRandomItemWithTypeNameContent: "%T"`, v)
}
}
// InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic includes the requested fields of the GraphQL type Topic. // InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic struct { type InterfaceNoFragmentsQueryRandomItemWithTypeNameTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -314,6 +386,69 @@ func (v *InterfaceNoFragmentsQueryResponse) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalInterfaceNoFragmentsQueryResponse struct {
Root InterfaceNoFragmentsQueryRootTopic `json:"root"`
RandomItem json.RawMessage `json:"randomItem"`
RandomItemWithTypeName json.RawMessage `json:"randomItemWithTypeName"`
WithPointer json.RawMessage `json:"withPointer"`
}
func (v *InterfaceNoFragmentsQueryResponse) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *InterfaceNoFragmentsQueryResponse) __premarshalJSON() (*__premarshalInterfaceNoFragmentsQueryResponse, error) {
var retval __premarshalInterfaceNoFragmentsQueryResponse
retval.Root = v.Root
{
dst := &retval.RandomItem
src := v.RandomItem
var err error
*dst, err = __marshalInterfaceNoFragmentsQueryRandomItemContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InterfaceNoFragmentsQueryResponse.RandomItem: %w", err)
}
}
{
dst := &retval.RandomItemWithTypeName
src := v.RandomItemWithTypeName
var err error
*dst, err = __marshalInterfaceNoFragmentsQueryRandomItemWithTypeNameContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InterfaceNoFragmentsQueryResponse.RandomItemWithTypeName: %w", err)
}
}
{
dst := &retval.WithPointer
src := v.WithPointer
if src != nil {
var err error
*dst, err = __marshalInterfaceNoFragmentsQueryWithPointerContent(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal InterfaceNoFragmentsQueryResponse.WithPointer: %w", err)
}
}
}
return &retval, nil
}
// InterfaceNoFragmentsQueryRootTopic includes the requested fields of the GraphQL type Topic. // InterfaceNoFragmentsQueryRootTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNoFragmentsQueryRootTopic struct { type InterfaceNoFragmentsQueryRootTopic struct {
// ID is documented in the Content interface. // ID is documented in the Content interface.
@@ -419,6 +554,42 @@ func __unmarshalInterfaceNoFragmentsQueryWithPointerContent(b []byte, v *Interfa
} }
} }
func __marshalInterfaceNoFragmentsQueryWithPointerContent(v *InterfaceNoFragmentsQueryWithPointerContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *InterfaceNoFragmentsQueryWithPointerArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNoFragmentsQueryWithPointerArticle
}{typename, v}
return json.Marshal(result)
case *InterfaceNoFragmentsQueryWithPointerVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNoFragmentsQueryWithPointerVideo
}{typename, v}
return json.Marshal(result)
case *InterfaceNoFragmentsQueryWithPointerTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*InterfaceNoFragmentsQueryWithPointerTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for InterfaceNoFragmentsQueryWithPointerContent: "%T"`, v)
}
}
// InterfaceNoFragmentsQueryWithPointerTopic includes the requested fields of the GraphQL type Topic. // InterfaceNoFragmentsQueryWithPointerTopic includes the requested fields of the GraphQL type Topic.
type InterfaceNoFragmentsQueryWithPointerTopic struct { type InterfaceNoFragmentsQueryWithPointerTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -27,18 +27,76 @@ type MyInput struct {
Birthdate *time.Time `json:"-"` Birthdate *time.Time `json:"-"`
} }
func (v *MyInput) MarshalJSON() ([]byte, error) { func (v *MyInput) UnmarshalJSON(b []byte) error {
var fullObject struct { if string(b) == "null" {
return nil
}
var firstPass struct {
*MyInput *MyInput
Birthdate json.RawMessage `json:"birthdate"` Birthdate json.RawMessage `json:"birthdate"`
graphql.NoMarshalJSON graphql.NoUnmarshalJSON
}
firstPass.MyInput = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
} }
fullObject.MyInput = v
{ {
dst := &v.Birthdate
src := firstPass.Birthdate
if len(src) != 0 && string(src) != "null" {
*dst = new(time.Time)
err = testutil.UnmarshalDate(
src, *dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal MyInput.Birthdate: %w", err)
}
}
}
return nil
}
dst := &fullObject.Birthdate type __premarshalMyInput struct {
Email *string `json:"email"`
Name *string `json:"name"`
Id *testutil.ID `json:"id"`
Role *Role `json:"role"`
Names []*string `json:"names"`
HasPokemon *testutil.Pokemon `json:"hasPokemon"`
Birthdate json.RawMessage `json:"birthdate"`
}
func (v *MyInput) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *MyInput) __premarshalJSON() (*__premarshalMyInput, error) {
var retval __premarshalMyInput
retval.Email = v.Email
retval.Name = v.Name
retval.Id = v.Id
retval.Role = v.Role
retval.Names = v.Names
retval.HasPokemon = v.HasPokemon
{
dst := &retval.Birthdate
src := v.Birthdate src := v.Birthdate
if src != nil { if src != nil {
var err error var err error
@@ -50,8 +108,7 @@ func (v *MyInput) MarshalJSON() ([]byte, error) {
} }
} }
} }
return &retval, nil
return json.Marshal(&fullObject)
} }
// MyMultipleDirectivesResponse is returned by MultipleDirectives on success. // MyMultipleDirectivesResponse is returned by MultipleDirectives on success.
@@ -116,18 +173,76 @@ type UserQueryInput struct {
Birthdate *time.Time `json:"-"` Birthdate *time.Time `json:"-"`
} }
func (v *UserQueryInput) MarshalJSON() ([]byte, error) { func (v *UserQueryInput) UnmarshalJSON(b []byte) error {
var fullObject struct { if string(b) == "null" {
return nil
}
var firstPass struct {
*UserQueryInput *UserQueryInput
Birthdate json.RawMessage `json:"birthdate"` Birthdate json.RawMessage `json:"birthdate"`
graphql.NoMarshalJSON graphql.NoUnmarshalJSON
}
firstPass.UserQueryInput = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
} }
fullObject.UserQueryInput = v
{ {
dst := &v.Birthdate
src := firstPass.Birthdate
if len(src) != 0 && string(src) != "null" {
*dst = new(time.Time)
err = testutil.UnmarshalDate(
src, *dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal UserQueryInput.Birthdate: %w", err)
}
}
}
return nil
}
dst := &fullObject.Birthdate type __premarshalUserQueryInput struct {
Email *string `json:"email"`
Name *string `json:"name"`
Id *testutil.ID `json:"id"`
Role *Role `json:"role"`
Names []*string `json:"names"`
HasPokemon *testutil.Pokemon `json:"hasPokemon"`
Birthdate json.RawMessage `json:"birthdate"`
}
func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *UserQueryInput) __premarshalJSON() (*__premarshalUserQueryInput, error) {
var retval __premarshalUserQueryInput
retval.Email = v.Email
retval.Name = v.Name
retval.Id = v.Id
retval.Role = v.Role
retval.Names = v.Names
retval.HasPokemon = v.HasPokemon
{
dst := &retval.Birthdate
src := v.Birthdate src := v.Birthdate
if src != nil { if src != nil {
var err error var err error
@@ -139,8 +254,7 @@ func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
} }
} }
} }
return &retval, nil
return json.Marshal(&fullObject)
} }
// __MultipleDirectivesInput is used internally by genqlient // __MultipleDirectivesInput is used internally by genqlient
@@ -75,18 +75,75 @@ type UserQueryInput struct {
Birthdate time.Time `json:"-"` Birthdate time.Time `json:"-"`
} }
func (v *UserQueryInput) MarshalJSON() ([]byte, error) { func (v *UserQueryInput) UnmarshalJSON(b []byte) error {
var fullObject struct { if string(b) == "null" {
return nil
}
var firstPass struct {
*UserQueryInput *UserQueryInput
Birthdate json.RawMessage `json:"birthdate"` Birthdate json.RawMessage `json:"birthdate"`
graphql.NoMarshalJSON graphql.NoUnmarshalJSON
}
firstPass.UserQueryInput = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
} }
fullObject.UserQueryInput = v
{ {
dst := &v.Birthdate
src := firstPass.Birthdate
if len(src) != 0 && string(src) != "null" {
err = testutil.UnmarshalDate(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal UserQueryInput.Birthdate: %w", err)
}
}
}
return nil
}
dst := &fullObject.Birthdate type __premarshalUserQueryInput struct {
Email string `json:"email"`
Name string `json:"name"`
Id testutil.ID `json:"id"`
Role Role `json:"role"`
Names []string `json:"names"`
HasPokemon testutil.Pokemon `json:"hasPokemon"`
Birthdate json.RawMessage `json:"birthdate"`
}
func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *UserQueryInput) __premarshalJSON() (*__premarshalUserQueryInput, error) {
var retval __premarshalUserQueryInput
retval.Email = v.Email
retval.Name = v.Name
retval.Id = v.Id
retval.Role = v.Role
retval.Names = v.Names
retval.HasPokemon = v.HasPokemon
{
dst := &retval.Birthdate
src := v.Birthdate src := v.Birthdate
var err error var err error
*dst, err = testutil.MarshalDate( *dst, err = testutil.MarshalDate(
@@ -96,8 +153,7 @@ func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
"Unable to marshal UserQueryInput.Birthdate: %w", err) "Unable to marshal UserQueryInput.Birthdate: %w", err)
} }
} }
return &retval, nil
return json.Marshal(&fullObject)
} }
// __OmitEmptyQueryInput is used internally by genqlient // __OmitEmptyQueryInput is used internally by genqlient
@@ -82,18 +82,76 @@ type UserQueryInput struct {
Birthdate *time.Time `json:"-"` Birthdate *time.Time `json:"-"`
} }
func (v *UserQueryInput) MarshalJSON() ([]byte, error) { func (v *UserQueryInput) UnmarshalJSON(b []byte) error {
var fullObject struct { if string(b) == "null" {
return nil
}
var firstPass struct {
*UserQueryInput *UserQueryInput
Birthdate json.RawMessage `json:"birthdate"` Birthdate json.RawMessage `json:"birthdate"`
graphql.NoMarshalJSON graphql.NoUnmarshalJSON
}
firstPass.UserQueryInput = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
} }
fullObject.UserQueryInput = v
{ {
dst := &v.Birthdate
src := firstPass.Birthdate
if len(src) != 0 && string(src) != "null" {
*dst = new(time.Time)
err = testutil.UnmarshalDate(
src, *dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal UserQueryInput.Birthdate: %w", err)
}
}
}
return nil
}
dst := &fullObject.Birthdate type __premarshalUserQueryInput struct {
Email *string `json:"email"`
Name *string `json:"name"`
Id *testutil.ID `json:"id"`
Role *Role `json:"role"`
Names []*string `json:"names"`
HasPokemon *testutil.Pokemon `json:"hasPokemon"`
Birthdate json.RawMessage `json:"birthdate"`
}
func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *UserQueryInput) __premarshalJSON() (*__premarshalUserQueryInput, error) {
var retval __premarshalUserQueryInput
retval.Email = v.Email
retval.Name = v.Name
retval.Id = v.Id
retval.Role = v.Role
retval.Names = v.Names
retval.HasPokemon = v.HasPokemon
{
dst := &retval.Birthdate
src := v.Birthdate src := v.Birthdate
if src != nil { if src != nil {
var err error var err error
@@ -105,8 +163,7 @@ func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
} }
} }
} }
return &retval, nil
return json.Marshal(&fullObject)
} }
// __PointersQueryInput is used internally by genqlient // __PointersQueryInput is used internally by genqlient
@@ -82,18 +82,75 @@ type UserQueryInput struct {
Birthdate time.Time `json:"-"` Birthdate time.Time `json:"-"`
} }
func (v *UserQueryInput) MarshalJSON() ([]byte, error) { func (v *UserQueryInput) UnmarshalJSON(b []byte) error {
var fullObject struct { if string(b) == "null" {
return nil
}
var firstPass struct {
*UserQueryInput *UserQueryInput
Birthdate json.RawMessage `json:"birthdate"` Birthdate json.RawMessage `json:"birthdate"`
graphql.NoMarshalJSON graphql.NoUnmarshalJSON
}
firstPass.UserQueryInput = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
} }
fullObject.UserQueryInput = v
{ {
dst := &v.Birthdate
src := firstPass.Birthdate
if len(src) != 0 && string(src) != "null" {
err = testutil.UnmarshalDate(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal UserQueryInput.Birthdate: %w", err)
}
}
}
return nil
}
dst := &fullObject.Birthdate type __premarshalUserQueryInput struct {
Email string `json:"email"`
Name string `json:"name"`
Id testutil.ID `json:"id"`
Role Role `json:"role"`
Names []string `json:"names"`
HasPokemon testutil.Pokemon `json:"hasPokemon"`
Birthdate json.RawMessage `json:"birthdate"`
}
func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *UserQueryInput) __premarshalJSON() (*__premarshalUserQueryInput, error) {
var retval __premarshalUserQueryInput
retval.Email = v.Email
retval.Name = v.Name
retval.Id = v.Id
retval.Role = v.Role
retval.Names = v.Names
retval.HasPokemon = v.HasPokemon
{
dst := &retval.Birthdate
src := v.Birthdate src := v.Birthdate
var err error var err error
*dst, err = testutil.MarshalDate( *dst, err = testutil.MarshalDate(
@@ -103,8 +160,7 @@ func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
"Unable to marshal UserQueryInput.Birthdate: %w", err) "Unable to marshal UserQueryInput.Birthdate: %w", err)
} }
} }
return &retval, nil
return json.Marshal(&fullObject)
} }
// __PointersQueryInput is used internally by genqlient // __PointersQueryInput is used internally by genqlient
@@ -109,6 +109,42 @@ func __unmarshalSimpleInlineFragmentRandomItemContent(b []byte, v *SimpleInlineF
} }
} }
func __marshalSimpleInlineFragmentRandomItemContent(v *SimpleInlineFragmentRandomItemContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *SimpleInlineFragmentRandomItemArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*SimpleInlineFragmentRandomItemArticle
}{typename, v}
return json.Marshal(result)
case *SimpleInlineFragmentRandomItemVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*SimpleInlineFragmentRandomItemVideo
}{typename, v}
return json.Marshal(result)
case *SimpleInlineFragmentRandomItemTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*SimpleInlineFragmentRandomItemTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for SimpleInlineFragmentRandomItemContent: "%T"`, v)
}
}
// SimpleInlineFragmentRandomItemTopic includes the requested fields of the GraphQL type Topic. // SimpleInlineFragmentRandomItemTopic includes the requested fields of the GraphQL type Topic.
type SimpleInlineFragmentRandomItemTopic struct { type SimpleInlineFragmentRandomItemTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -164,6 +200,36 @@ func (v *SimpleInlineFragmentResponse) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalSimpleInlineFragmentResponse struct {
RandomItem json.RawMessage `json:"randomItem"`
}
func (v *SimpleInlineFragmentResponse) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *SimpleInlineFragmentResponse) __premarshalJSON() (*__premarshalSimpleInlineFragmentResponse, error) {
var retval __premarshalSimpleInlineFragmentResponse
{
dst := &retval.RandomItem
src := v.RandomItem
var err error
*dst, err = __marshalSimpleInlineFragmentRandomItemContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal SimpleInlineFragmentResponse.RandomItem: %w", err)
}
}
return &retval, nil
}
func SimpleInlineFragment( func SimpleInlineFragment(
client graphql.Client, client graphql.Client,
) (*SimpleInlineFragmentResponse, error) { ) (*SimpleInlineFragmentResponse, error) {
@@ -108,6 +108,46 @@ func __unmarshalSimpleNamedFragmentRandomItemContent(b []byte, v *SimpleNamedFra
} }
} }
func __marshalSimpleNamedFragmentRandomItemContent(v *SimpleNamedFragmentRandomItemContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *SimpleNamedFragmentRandomItemArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*SimpleNamedFragmentRandomItemArticle
}{typename, v}
return json.Marshal(result)
case *SimpleNamedFragmentRandomItemVideo:
typename = "Video"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalSimpleNamedFragmentRandomItemVideo
}{typename, premarshaled}
return json.Marshal(result)
case *SimpleNamedFragmentRandomItemTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*SimpleNamedFragmentRandomItemTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for SimpleNamedFragmentRandomItemContent: "%T"`, v)
}
}
// SimpleNamedFragmentRandomItemTopic includes the requested fields of the GraphQL type Topic. // SimpleNamedFragmentRandomItemTopic includes the requested fields of the GraphQL type Topic.
type SimpleNamedFragmentRandomItemTopic struct { type SimpleNamedFragmentRandomItemTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -150,6 +190,40 @@ func (v *SimpleNamedFragmentRandomItemVideo) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalSimpleNamedFragmentRandomItemVideo struct {
Typename string `json:"__typename"`
Id testutil.ID `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Duration int `json:"duration"`
Thumbnail VideoFieldsThumbnail `json:"thumbnail"`
}
func (v *SimpleNamedFragmentRandomItemVideo) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *SimpleNamedFragmentRandomItemVideo) __premarshalJSON() (*__premarshalSimpleNamedFragmentRandomItemVideo, error) {
var retval __premarshalSimpleNamedFragmentRandomItemVideo
retval.Typename = v.Typename
retval.Id = v.Id
retval.Name = v.Name
retval.Url = v.VideoFields.Url
retval.Duration = v.VideoFields.Duration
retval.Thumbnail = v.VideoFields.Thumbnail
return &retval, nil
}
// SimpleNamedFragmentRandomLeafArticle includes the requested fields of the GraphQL type Article. // SimpleNamedFragmentRandomLeafArticle includes the requested fields of the GraphQL type Article.
type SimpleNamedFragmentRandomLeafArticle struct { type SimpleNamedFragmentRandomLeafArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -210,6 +284,38 @@ func __unmarshalSimpleNamedFragmentRandomLeafLeafContent(b []byte, v *SimpleName
} }
} }
func __marshalSimpleNamedFragmentRandomLeafLeafContent(v *SimpleNamedFragmentRandomLeafLeafContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *SimpleNamedFragmentRandomLeafArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*SimpleNamedFragmentRandomLeafArticle
}{typename, v}
return json.Marshal(result)
case *SimpleNamedFragmentRandomLeafVideo:
typename = "Video"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalSimpleNamedFragmentRandomLeafVideo
}{typename, premarshaled}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for SimpleNamedFragmentRandomLeafLeafContent: "%T"`, v)
}
}
// SimpleNamedFragmentRandomLeafVideo includes the requested fields of the GraphQL type Video. // SimpleNamedFragmentRandomLeafVideo includes the requested fields of the GraphQL type Video.
type SimpleNamedFragmentRandomLeafVideo struct { type SimpleNamedFragmentRandomLeafVideo struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -241,6 +347,40 @@ func (v *SimpleNamedFragmentRandomLeafVideo) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalSimpleNamedFragmentRandomLeafVideo struct {
Typename string `json:"__typename"`
Id testutil.ID `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Duration int `json:"duration"`
Thumbnail VideoFieldsThumbnail `json:"thumbnail"`
}
func (v *SimpleNamedFragmentRandomLeafVideo) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *SimpleNamedFragmentRandomLeafVideo) __premarshalJSON() (*__premarshalSimpleNamedFragmentRandomLeafVideo, error) {
var retval __premarshalSimpleNamedFragmentRandomLeafVideo
retval.Typename = v.Typename
retval.Id = v.VideoFields.Id
retval.Name = v.VideoFields.Name
retval.Url = v.VideoFields.Url
retval.Duration = v.VideoFields.Duration
retval.Thumbnail = v.VideoFields.Thumbnail
return &retval, nil
}
// SimpleNamedFragmentResponse is returned by SimpleNamedFragment on success. // SimpleNamedFragmentResponse is returned by SimpleNamedFragment on success.
type SimpleNamedFragmentResponse struct { type SimpleNamedFragmentResponse struct {
RandomItem SimpleNamedFragmentRandomItemContent `json:"-"` RandomItem SimpleNamedFragmentRandomItemContent `json:"-"`
@@ -294,6 +434,50 @@ func (v *SimpleNamedFragmentResponse) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalSimpleNamedFragmentResponse struct {
RandomItem json.RawMessage `json:"randomItem"`
RandomLeaf json.RawMessage `json:"randomLeaf"`
}
func (v *SimpleNamedFragmentResponse) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *SimpleNamedFragmentResponse) __premarshalJSON() (*__premarshalSimpleNamedFragmentResponse, error) {
var retval __premarshalSimpleNamedFragmentResponse
{
dst := &retval.RandomItem
src := v.RandomItem
var err error
*dst, err = __marshalSimpleNamedFragmentRandomItemContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal SimpleNamedFragmentResponse.RandomItem: %w", err)
}
}
{
dst := &retval.RandomLeaf
src := v.RandomLeaf
var err error
*dst, err = __marshalSimpleNamedFragmentRandomLeafLeafContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal SimpleNamedFragmentResponse.RandomLeaf: %w", err)
}
}
return &retval, nil
}
// VideoFields includes the GraphQL fields of Video requested by the fragment VideoFields. // VideoFields includes the GraphQL fields of Video requested by the fragment VideoFields.
type VideoFields struct { type VideoFields struct {
// ID is documented in the Content interface. // ID is documented in the Content interface.
@@ -99,6 +99,48 @@ func (v *StructOptionRootTopicChildrenContentParentTopic) UnmarshalJSON(b []byte
return nil return nil
} }
type __premarshalStructOptionRootTopicChildrenContentParentTopic struct {
Id testutil.ID `json:"id"`
Children []StructOptionRootTopicChildrenContentParentTopicChildrenContent `json:"children"`
InterfaceChildren []json.RawMessage `json:"interfaceChildren"`
}
func (v *StructOptionRootTopicChildrenContentParentTopic) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *StructOptionRootTopicChildrenContentParentTopic) __premarshalJSON() (*__premarshalStructOptionRootTopicChildrenContentParentTopic, error) {
var retval __premarshalStructOptionRootTopicChildrenContentParentTopic
retval.Id = v.Id
retval.Children = v.Children
{
dst := &retval.InterfaceChildren
src := v.InterfaceChildren
*dst = make(
[]json.RawMessage,
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = __marshalStructOptionRootTopicChildrenContentParentTopicInterfaceChildrenContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal StructOptionRootTopicChildrenContentParentTopic.InterfaceChildren: %w", err)
}
}
}
return &retval, nil
}
// StructOptionRootTopicChildrenContentParentTopicChildrenContent includes the requested fields of the GraphQL type Content. // StructOptionRootTopicChildrenContentParentTopicChildrenContent includes the requested fields of the GraphQL type Content.
// The GraphQL type's documentation follows. // The GraphQL type's documentation follows.
// //
@@ -207,6 +249,46 @@ func __unmarshalStructOptionRootTopicChildrenContentParentTopicInterfaceChildren
} }
} }
func __marshalStructOptionRootTopicChildrenContentParentTopicInterfaceChildrenContent(v *StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenArticle
}{typename, v}
return json.Marshal(result)
case *StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenVideo:
typename = "Video"
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
result := struct {
TypeName string `json:"__typename"`
*__premarshalStructOptionRootTopicChildrenContentParentTopicInterfaceChildrenVideo
}{typename, premarshaled}
return json.Marshal(result)
case *StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenContent: "%T"`, v)
}
}
// StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenTopic includes the requested fields of the GraphQL type Topic. // StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenTopic includes the requested fields of the GraphQL type Topic.
type StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenTopic struct { type StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenTopic struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -247,6 +329,31 @@ func (v *StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenVideo)
return nil return nil
} }
type __premarshalStructOptionRootTopicChildrenContentParentTopicInterfaceChildrenVideo struct {
Typename string `json:"__typename"`
Id testutil.ID `json:"id"`
Duration int `json:"duration"`
}
func (v *StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenVideo) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *StructOptionRootTopicChildrenContentParentTopicInterfaceChildrenVideo) __premarshalJSON() (*__premarshalStructOptionRootTopicChildrenContentParentTopicInterfaceChildrenVideo, error) {
var retval __premarshalStructOptionRootTopicChildrenContentParentTopicInterfaceChildrenVideo
retval.Typename = v.Typename
retval.Id = v.Id
retval.Duration = v.VideoFields.Duration
return &retval, nil
}
// StructOptionUser includes the requested fields of the GraphQL type User. // StructOptionUser includes the requested fields of the GraphQL type User.
// The GraphQL type's documentation follows. // The GraphQL type's documentation follows.
// //
@@ -97,6 +97,42 @@ func __unmarshalItem(b []byte, v *Item) error {
} }
} }
func __marshalItem(v *Item) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *ItemArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*ItemArticle
}{typename, v}
return json.Marshal(result)
case *ItemVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*ItemVideo
}{typename, v}
return json.Marshal(result)
case *ItemTopic:
typename = "Topic"
result := struct {
TypeName string `json:"__typename"`
*ItemTopic
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for Item: "%T"`, v)
}
}
// ItemArticle includes the requested fields of the GraphQL type Article. // ItemArticle includes the requested fields of the GraphQL type Article.
type ItemArticle struct { type ItemArticle struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -165,6 +201,42 @@ func (v *Resp) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalResp struct {
User User `json:"user"`
RandomItem json.RawMessage `json:"randomItem"`
Users []User `json:"users"`
}
func (v *Resp) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *Resp) __premarshalJSON() (*__premarshalResp, error) {
var retval __premarshalResp
retval.User = v.User
{
dst := &retval.RandomItem
src := v.RandomItem
var err error
*dst, err = __marshalItem(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal Resp.RandomItem: %w", err)
}
}
retval.Users = v.Users
return &retval, nil
}
// User includes the requested fields of the GraphQL type User. // User includes the requested fields of the GraphQL type User.
// The GraphQL type's documentation follows. // The GraphQL type's documentation follows.
// //
@@ -69,6 +69,34 @@ func __unmarshalUnionNoFragmentsQueryRandomLeafLeafContent(b []byte, v *UnionNoF
} }
} }
func __marshalUnionNoFragmentsQueryRandomLeafLeafContent(v *UnionNoFragmentsQueryRandomLeafLeafContent) ([]byte, error) {
var typename string
switch v := (*v).(type) {
case *UnionNoFragmentsQueryRandomLeafArticle:
typename = "Article"
result := struct {
TypeName string `json:"__typename"`
*UnionNoFragmentsQueryRandomLeafArticle
}{typename, v}
return json.Marshal(result)
case *UnionNoFragmentsQueryRandomLeafVideo:
typename = "Video"
result := struct {
TypeName string `json:"__typename"`
*UnionNoFragmentsQueryRandomLeafVideo
}{typename, v}
return json.Marshal(result)
case nil:
return []byte("null"), nil
default:
return nil, fmt.Errorf(
`Unexpected concrete type for UnionNoFragmentsQueryRandomLeafLeafContent: "%T"`, v)
}
}
// UnionNoFragmentsQueryRandomLeafVideo includes the requested fields of the GraphQL type Video. // UnionNoFragmentsQueryRandomLeafVideo includes the requested fields of the GraphQL type Video.
type UnionNoFragmentsQueryRandomLeafVideo struct { type UnionNoFragmentsQueryRandomLeafVideo struct {
Typename string `json:"__typename"` Typename string `json:"__typename"`
@@ -112,6 +140,36 @@ func (v *UnionNoFragmentsQueryResponse) UnmarshalJSON(b []byte) error {
return nil return nil
} }
type __premarshalUnionNoFragmentsQueryResponse struct {
RandomLeaf json.RawMessage `json:"randomLeaf"`
}
func (v *UnionNoFragmentsQueryResponse) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *UnionNoFragmentsQueryResponse) __premarshalJSON() (*__premarshalUnionNoFragmentsQueryResponse, error) {
var retval __premarshalUnionNoFragmentsQueryResponse
{
dst := &retval.RandomLeaf
src := v.RandomLeaf
var err error
*dst, err = __marshalUnionNoFragmentsQueryRandomLeafLeafContent(
&src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal UnionNoFragmentsQueryResponse.RandomLeaf: %w", err)
}
}
return &retval, nil
}
func UnionNoFragmentsQuery( func UnionNoFragmentsQuery(
client graphql.Client, client graphql.Client,
) (*UnionNoFragmentsQueryResponse, error) { ) (*UnionNoFragmentsQueryResponse, error) {
@@ -41,18 +41,75 @@ type UserQueryInput struct {
Birthdate time.Time `json:"-"` Birthdate time.Time `json:"-"`
} }
func (v *UserQueryInput) MarshalJSON() ([]byte, error) { func (v *UserQueryInput) UnmarshalJSON(b []byte) error {
var fullObject struct { if string(b) == "null" {
return nil
}
var firstPass struct {
*UserQueryInput *UserQueryInput
Birthdate json.RawMessage `json:"birthdate"` Birthdate json.RawMessage `json:"birthdate"`
graphql.NoMarshalJSON graphql.NoUnmarshalJSON
}
firstPass.UserQueryInput = v
err := json.Unmarshal(b, &firstPass)
if err != nil {
return err
} }
fullObject.UserQueryInput = v
{ {
dst := &v.Birthdate
src := firstPass.Birthdate
if len(src) != 0 && string(src) != "null" {
err = testutil.UnmarshalDate(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal UserQueryInput.Birthdate: %w", err)
}
}
}
return nil
}
dst := &fullObject.Birthdate type __premarshalUserQueryInput struct {
Email string `json:"email"`
Name string `json:"name"`
Id testutil.ID `json:"id"`
Role Role `json:"role"`
Names []string `json:"names"`
HasPokemon testutil.Pokemon `json:"hasPokemon"`
Birthdate json.RawMessage `json:"birthdate"`
}
func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
premarshaled, err := v.__premarshalJSON()
if err != nil {
return nil, err
}
return json.Marshal(premarshaled)
}
func (v *UserQueryInput) __premarshalJSON() (*__premarshalUserQueryInput, error) {
var retval __premarshalUserQueryInput
retval.Email = v.Email
retval.Name = v.Name
retval.Id = v.Id
retval.Role = v.Role
retval.Names = v.Names
retval.HasPokemon = v.HasPokemon
{
dst := &retval.Birthdate
src := v.Birthdate src := v.Birthdate
var err error var err error
*dst, err = testutil.MarshalDate( *dst, err = testutil.MarshalDate(
@@ -62,8 +119,7 @@ func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
"Unable to marshal UserQueryInput.Birthdate: %w", err) "Unable to marshal UserQueryInput.Birthdate: %w", err)
} }
} }
return &retval, nil
return json.Marshal(&fullObject)
} }
// __unexportedInput is used internally by genqlient // __unexportedInput is used internally by genqlient
+160 -41
View File
@@ -158,6 +158,17 @@ func (field *goStructField) IsEmbedded() bool {
return field.GoName == "" return field.GoName == ""
} }
// Selector returns the field's name, which is unqualified type-name if it's
// embedded.
func (field *goStructField) Selector() string {
if field.GoName != "" {
return field.GoName
}
// TODO(benkraft): This assumes the type is package-local, which is always
// true for embedded types for us, but isn't the most robust assumption.
return field.GoType.Unwrap().Reference()
}
// unmarshaler returns: // unmarshaler returns:
// - the name of the function to use to unmarshal this field // - the name of the function to use to unmarshal this field
// - true if this is a fully-qualified name (false if it is a package-local // - true if this is a fully-qualified name (false if it is a package-local
@@ -176,14 +187,6 @@ func (field *goStructField) unmarshaler() (qualifiedName string, needsImport boo
return "encoding/json.Unmarshal", true, field.IsEmbedded() return "encoding/json.Unmarshal", true, field.IsEmbedded()
} }
// NeedsUnmarshaler returns true if this field needs special handling when
// unmarshaling, e.g. if it's of interface type, embedded, or has a
// user-specified custom unmarshaler.
func (field *goStructField) NeedsUnmarshaler() bool {
_, _, ok := field.unmarshaler()
return ok
}
// Unmarshaler returns the Go name of the function to use to unmarshal this // Unmarshaler returns the Go name of the function to use to unmarshal this
// field (which may be "json.Unmarshal" if there's not a special one). // field (which may be "json.Unmarshal" if there's not a special one).
func (field *goStructField) Unmarshaler(g *generator) (string, error) { func (field *goStructField) Unmarshaler(g *generator) (string, error) {
@@ -198,34 +201,149 @@ func (field *goStructField) Unmarshaler(g *generator) (string, error) {
// - the fully-qualified name of the function to use to marshal this field // - the fully-qualified name of the function to use to marshal this field
// - true if we need to generate an marshaler at all, false if the default // - true if we need to generate an marshaler at all, false if the default
// behavior will suffice // behavior will suffice
func (field *goStructField) marshaler() (qualifiedName string, needsMarshaler bool) { func (field *goStructField) marshaler() (qualifiedName string, needsImport bool, needsMarshaler bool) {
// (there are no interfaces on the input side) switch typ := field.GoType.Unwrap().(type) {
opaque, ok := field.GoType.Unwrap().(*goOpaqueType) case *goOpaqueType:
if ok && opaque.Marshaler != "" { if typ.Marshaler != "" {
return opaque.Marshaler, true return typ.Marshaler, true, true
}
case *goInterfaceType:
return "__marshal" + typ.Reference(), false, true
} }
return "encoding/json.Marshal", field.IsEmbedded() return "encoding/json.Marshal", true, field.IsEmbedded()
}
// NeedsMarshaler returns true if this field needs special handling when
// marshaling, e.g. if it has a user-specified custom marshaler.
func (field *goStructField) NeedsMarshaler() bool {
_, ok := field.marshaler()
return ok
} }
// Marshaler returns the Go name of the function to use to marshal this // Marshaler returns the Go name of the function to use to marshal this
// field (which may be "json.Marshal" if there's not a special one). // field (which may be "json.Marshal" if there's not a special one).
func (field *goStructField) Marshaler(g *generator) (string, error) { func (field *goStructField) Marshaler(g *generator) (string, error) {
name, _ := field.marshaler() name, needsImport, _ := field.marshaler()
// Unlike unmarshaler, we never have a local name, and always need g.ref. if needsImport {
return g.ref(name) return g.ref(name)
}
return name, nil
}
// NeedsMarshaling returns true if this field needs special handling when
// marshaling and unmarshaling, e.g. if it has a user-specified custom
// (un)marshaler. Note if it needs one, it needs the other: even if the user
// only specified an unmarshaler, we need to add `json:"-"` to the field, which
// means we need to specially handling it when marshaling.
func (field *goStructField) NeedsMarshaling() bool {
_, _, ok1 := field.marshaler()
_, _, ok2 := field.unmarshaler()
return ok1 || ok2
}
// NeedsMarshaler returns true if any fields of this type need special
// handling when (un)marshaling (see goStructField.NeedsMarshaling).
func (typ *goStructType) NeedsMarshaling() bool {
for _, f := range typ.Fields {
if f.NeedsMarshaling() {
return true
}
}
return false
}
// selector represents a field and the path to get there from the type in
// question, and is used in FlattenedFields, below.
type selector struct {
*goStructField
// e.g. "OuterEmbed.InnerEmbed.LeafField"
Selector string
}
// FlattenedFields returns the fields of this type and its recursive embeds,
// and the paths to reach them (via those embeds), but with different
// visibility rules for conflicting fields than Go.
//
// (Before you read further, now's a good time to review Go's rules:
// https://golang.org/ref/spec#Selectors. Done? Good.)
//
// To illustrate the need, consider the following query:
// fragment A on T { id }
// fragment B on T { id }
// query Q { t { ...A ...B } }
// We generate types:
// type A struct { Id string `json:"id"` }
// type B struct { Id string `json:"id"` }
// type QT struct { A; B }
// According to Go's embedding rules, QT has no field Id: since QT.A.Id and
// QT.B.Id are at equal depth, neither wins and gets promoted. (Go's JSON
// library uses similar logic to decide which field to write to JSON, except
// with the additional rule that a field with a JSON tag wins over a field
// without; in our case both have such a field.)
//
// Those rules don't work for us. When unmarshaling, we want to fill in all
// the potentially-matching fields (QT.A.Id and QT.B.Id in this case), and when
// marshaling, we want to always marshal exactly one potentially-conflicting
// field; we're happy to use the Go visibility rules when they apply but we
// need to always marshal one field, even if there's not a clear best choice.
// For unmarshaling, our QT.UnmarshalJSON ends up unmarshaling the same JSON
// object into QT, QT.A, and QT.B, which gives us the behavior we want. But
// for marshaling, we need to resolve the conflicts: if we simply marshaled QT,
// QT.A, and QT.B, we'd have to do some JSON-surgery to join them, and we'd
// probably end up with duplicate fields, which leads to unpredictable behavior
// based on the reader. That's no good.
//
// So: instead, we have our own rules, which work like the Go rules, except
// that if there's a tie we choose the first field (in source order). (In
// practice, hopefully, they all match, but validating that is even more work
// for a fairly rare case.) This function returns, for each JSON-name, the Go
// field we want to use. In the example above, it would return:
// []selector{{<goStructField for QT.A.Id>, "A.Id"}}
func (typ *goStructType) FlattenedFields() ([]*selector, error) {
seenJSONNames := map[string]bool{}
retval := make([]*selector, 0, len(typ.Fields))
queue := make([]*selector, len(typ.Fields))
for i, field := range typ.Fields {
queue[i] = &selector{field, field.Selector()}
}
// Since our (non-embedded) fields always have JSON tags, the logic we want
// is simply: do a breadth-first search through the recursively embedded
// fields, and take the first one we see with a given JSON tag.
for len(queue) > 0 {
field := queue[0]
queue = queue[1:]
if field.IsEmbedded() {
typ, ok := field.GoType.(*goStructType)
if !ok {
// Should never happen: embeds correspond to named fragments,
// and even if the fragment is of interface type in GraphQL,
// either it's spread into a concrete type, or we are writing
// one of the implementations of the interface into which it's
// spread; either way we embed the corresponding implementation
// of the fragment.
return nil, errorf(nil,
"genqlient internal error: embedded field %s.%s was not a struct",
typ.GoName, field.GoName)
}
// Enqueue the embedded fields for our BFS.
for _, subField := range typ.Fields {
queue = append(queue,
&selector{subField, field.Selector + "." + subField.Selector()})
}
continue
}
if seenJSONNames[field.JSONName] {
// We already chose a selector for this JSON field. Skip it.
continue
}
// Else, we are the selector we are looking for.
seenJSONNames[field.JSONName] = true
retval = append(retval, field)
}
return retval, nil
} }
func (typ *goStructType) WriteDefinition(w io.Writer, g *generator) error { func (typ *goStructType) WriteDefinition(w io.Writer, g *generator) error {
writeDescription(w, structDescription(typ)) writeDescription(w, structDescription(typ))
needUnmarshaler, needMarshaler := false, false
fmt.Fprintf(w, "type %s struct {\n", typ.GoName) fmt.Fprintf(w, "type %s struct {\n", typ.GoName)
for _, field := range typ.Fields { for _, field := range typ.Fields {
writeDescription(w, field.Description) writeDescription(w, field.Description)
@@ -234,13 +352,8 @@ func (typ *goStructType) WriteDefinition(w io.Writer, g *generator) error {
jsonTag += ",omitempty" jsonTag += ",omitempty"
} }
jsonTag += `"` jsonTag += `"`
if !typ.IsInput && field.NeedsUnmarshaler() { if field.NeedsMarshaling() {
// certain types are handled in our UnmarshalJSON (see below) // certain types are handled in our (Un)MarshalJSON (see below)
needUnmarshaler = true
jsonTag = `"-"`
}
if typ.IsInput && field.NeedsMarshaler() {
needMarshaler = true
jsonTag = `"-"` jsonTag = `"-"`
} }
// Note for embedded types field.GoName is "", which produces the code // Note for embedded types field.GoName is "", which produces the code
@@ -274,19 +387,20 @@ func (typ *goStructType) WriteDefinition(w io.Writer, g *generator) error {
// same thing as interface-typed fields, except the user has defined the // same thing as interface-typed fields, except the user has defined the
// helper. // helper.
// //
// Note that in all cases we need only write an unmarshaler if this is an // Note that genqlient itself only uses unmarshalers for output types, and
// input type, and a marshaler if it's an output type. // marshalers for input types. But we write both in case you want to write
// your data to JSON for some reason (say to put it in a cache). (And we
// need to write both if we need to write either, because in such cases we
// write a `json:"-"` tag on the field.)
// //
// TODO(benkraft): If/when proposal #5901 is implemented (Go 1.18 at the // TODO(benkraft): If/when proposal #5901 is implemented (Go 1.18 at the
// earliest), we may be able to do some of this a simpler way. // earliest), we may be able to do some of this a simpler way.
if needUnmarshaler { if typ.NeedsMarshaling() {
err := g.render("unmarshal.go.tmpl", w, typ) err := g.render("unmarshal.go.tmpl", w, typ)
if err != nil { if err != nil {
return err return err
} }
} err = g.render("marshal.go.tmpl", w, typ)
if needMarshaler {
err := g.render("marshal.go.tmpl", w, typ)
if err != nil { if err != nil {
return err return err
} }
@@ -367,9 +481,14 @@ func (typ *goInterfaceType) WriteDefinition(w io.Writer, g *generator) error {
fmt.Fprintf(w, "\n") // blank line between each type's implementations fmt.Fprintf(w, "\n") // blank line between each type's implementations
} }
// Finally, write the unmarshal-helper, which will be called by struct // Finally, write the marshal- and unmarshal-helpers, which
// fields referencing this type (see goStructType.WriteDefinition). // will be called by struct fields referencing this type (see
return g.render("unmarshal_helper.go.tmpl", w, typ) // goStructType.WriteDefinition).
err := g.render("unmarshal_helper.go.tmpl", w, typ)
if err != nil {
return err
}
return g.render("marshal_helper.go.tmpl", w, typ)
} }
func (typ *goInterfaceType) Reference() string { return typ.GoName } func (typ *goInterfaceType) Reference() string { return typ.GoName }
+38 -17
View File
@@ -1,5 +1,23 @@
{{/* (the blank lines at the start are intentional, to separate {{/* We need to generate UnmarshalJSON methods for some types that we want to
UnmarshalJSON from the function it follows) */}} handle specially. Specifically, we generate an UnmarshalJSON for each
struct with a field meeting any of these criteria:
- a field whose type is configured with a custom unmarshaler
- an embedded (anonymous) field; technically we only need an
UnmarshalJSON if the embedded type needs one, but it's easier to
generate it unconditionally
- a field of interface type
Additionally, since we add `json:"-"` to fields we handle specially, for
any field which requires a MarshalJSON, we also generate an UnmarshalJSON,
and vice versa.
Given that, we want to specially handle the above-described fields, but
unmarshal everything else normally. To handle fields with custom
unmarshalers, first we unmarshal them into a json.RawMessage, then call
the custom unmarshaler. Interface-typed fields are similar, except
instead of a custom unmarshaler we call the helper we've generated
(see unmarshal_helper.go.tmpl). Embedded fields don't need the
json.RawMessage; we just unmarshal our input again into the embedded
field. */}}
func (v *{{.GoName}}) UnmarshalJSON(b []byte) error { func (v *{{.GoName}}) UnmarshalJSON(b []byte) error {
{{/* Standard convention for unmarshalers is to no-op on null. */}} {{/* Standard convention for unmarshalers is to no-op on null. */}}
@@ -7,16 +25,14 @@ func (v *{{.GoName}}) UnmarshalJSON(b []byte) error {
return nil return nil
} }
{{/* We want to specially handle certain fields, but unmarshal everything {{/* For our first pass, we ignore embedded fields, unmarshal all the
else normally. To handle abstract fields and fields with custom custom-unmarshaler or abstract fields into json.RawMessage, and
unmarshalers, first we unmarshal them into a json.RawMessage, and then unmarshal everything else normally. To do this, we want to call
handle those further, below. Embedded fields don't need the json.Unmarshal on the receiver (v). But if we do that naively on a
json.RawMessage; we just use our input again. Either way, we first value of type <.GoName>, it will call this function again, and recurse
want to call json.Unmarshal on the receiver (v). But if we do that infinitely. So we make a wrapper type which embeds both this type and
naively on a value of type `.Type`, it will call this function again, NoUmnarshalJSON, which prevents either's UnmarshalJSON method from
and recurse infinitely. So we make a wrapper type which embeds both being promoted. For more on why this is so difficult, see
this type and NoUmnarshalJSON, which prevents either's UnmarshalJSON
method from being promoted. For more on why this is so difficult, see
https://github.com/benjaminjkraft/notes/blob/master/go-json-interfaces.md. https://github.com/benjaminjkraft/notes/blob/master/go-json-interfaces.md.
(Note there are a few different ways "hide" the method, but this one (Note there are a few different ways "hide" the method, but this one
seems to be the best option that works if this type has embedded types seems to be the best option that works if this type has embedded types
@@ -28,7 +44,7 @@ func (v *{{.GoName}}) UnmarshalJSON(b []byte) error {
var firstPass struct{ var firstPass struct{
*{{.GoName}} *{{.GoName}}
{{range .Fields -}} {{range .Fields -}}
{{if and .NeedsUnmarshaler (not .IsEmbedded) -}} {{if and .NeedsMarshaling (not .IsEmbedded) -}}
{{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"` {{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"`
{{end -}} {{end -}}
{{end -}} {{end -}}
@@ -45,11 +61,16 @@ func (v *{{.GoName}}) UnmarshalJSON(b []byte) error {
{{/* Now, handle the fields needing special handling. */}} {{/* Now, handle the fields needing special handling. */}}
{{range $field := .Fields -}} {{range $field := .Fields -}}
{{if $field.NeedsUnmarshaler -}} {{if $field.NeedsMarshaling -}}
{{if $field.IsEmbedded -}} {{if $field.IsEmbedded -}}
{{/* Embedded fields are easier: we just unmarshal the same input into {{/* Embedded fields are easier: we just unmarshal the same input into
them. (They're also easier because they can't be lists, since they them. (They're also easier because they can't be lists, since they
arise from GraphQL fragment spreads.) */ -}} arise from GraphQL fragment spreads.)
Note that our behavior if you have two fields of the same name via
different embeds differs from ordinary json-unmarshaling: we unmarshal
into *all* of the fields. See goStructType.FlattenedFields in
types.go for more discussion of embedding and visibility. */ -}}
err = {{$field.Unmarshaler $.Generator}}( err = {{$field.Unmarshaler $.Generator}}(
b, &v.{{$field.GoType.Unwrap.Reference}}) b, &v.{{$field.GoType.Unwrap.Reference}})
if err != nil { if err != nil {
@@ -125,8 +146,8 @@ func (v *{{.GoName}}) UnmarshalJSON(b []byte) error {
} }
{{end -}} {{end -}}
} }
{{end -}}{{/* end if/else .IsEmbedded */ -}} {{end}}{{/* end if/else .IsEmbedded */ -}}
{{end -}}{{/* end if .NeedsUnmarshaler */ -}} {{end}}{{/* end if .NeedsMarshaling */ -}}
{{end}}{{/* end range .Fields */ -}} {{end}}{{/* end range .Fields */ -}}
return nil return nil
+5 -2
View File
@@ -1,5 +1,8 @@
{{/* (the blank lines at the start are intentional, to separate {{/* This template generates a helper for each interface type genqlient must
the helper from the function it follows) */}} unmarshal. This helper is called by the UnmarshalJSON of each (struct)
type with a field of the interface type, similar to how it calls custom
unmarshalers. The helper itself is fairly simple: it just parses out and
switches on __typename, then unmarshals into the relevant struct. */}}
func __unmarshal{{.GoName}}(b []byte, v *{{.GoName}}) error { func __unmarshal{{.GoName}}(b []byte, v *{{.GoName}}) error {
if string(b) == "null" { if string(b) == "null" {
File diff suppressed because it is too large Load Diff
+17 -12
View File
@@ -25,7 +25,7 @@ func TestSimpleQuery(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := simpleQuery(ctx, client) resp, err := simpleQuery(ctx, client)
require.NoError(t, err) require.NoError(t, err)
@@ -42,7 +42,7 @@ func TestServerError(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := failingQuery(ctx, client) resp, err := failingQuery(ctx, client)
// As long as we get some response back, we should still return a full // As long as we get some response back, we should still return a full
@@ -55,7 +55,7 @@ func TestServerError(t *testing.T) {
func TestNetworkError(t *testing.T) { func TestNetworkError(t *testing.T) {
ctx := context.Background() ctx := context.Background()
client := graphql.NewClient("https://nothing.invalid/graphql", http.DefaultClient) client := newRoundtripClient(t, "https://nothing.invalid/graphql")
resp, err := failingQuery(ctx, client) resp, err := failingQuery(ctx, client)
// As we guarantee in the README, even on network error you always get a // As we guarantee in the README, even on network error you always get a
@@ -75,6 +75,11 @@ func TestVariables(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
// This doesn't roundtrip successfully because the zero user gets marshaled
// as {"id": "", "name": "", ...}, not null. There's really no way to do
// this right in Go (without adding `pointer: true` just for this purpose),
// and unmarshal(marshal(resp)) == resp should still hold, so we don't
// worry about it.
client := graphql.NewClient(server.URL, http.DefaultClient) client := graphql.NewClient(server.URL, http.DefaultClient)
resp, err := queryWithVariables(ctx, client, "2") resp, err := queryWithVariables(ctx, client, "2")
@@ -99,7 +104,7 @@ func TestOmitempty(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := queryWithOmitempty(ctx, client, "2") resp, err := queryWithOmitempty(ctx, client, "2")
require.NoError(t, err) require.NoError(t, err)
@@ -126,7 +131,7 @@ func TestCustomMarshal(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := queryWithCustomMarshal(ctx, client, resp, err := queryWithCustomMarshal(ctx, client,
time.Date(2025, time.January, 1, 12, 34, 56, 789, time.UTC)) time.Date(2025, time.January, 1, 12, 34, 56, 789, time.UTC))
@@ -155,7 +160,7 @@ func TestCustomMarshalSlice(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := queryWithCustomMarshalSlice(ctx, client, resp, err := queryWithCustomMarshalSlice(ctx, client,
[]time.Time{time.Date(2025, time.January, 1, 12, 34, 56, 789, time.UTC)}) []time.Time{time.Date(2025, time.January, 1, 12, 34, 56, 789, time.UTC)})
@@ -189,7 +194,7 @@ func TestCustomMarshalOptional(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
date := time.Date(2025, time.January, 1, 12, 34, 56, 789, time.UTC) date := time.Date(2025, time.January, 1, 12, 34, 56, 789, time.UTC)
resp, err := queryWithCustomMarshalOptional(ctx, client, &date, nil) resp, err := queryWithCustomMarshalOptional(ctx, client, &date, nil)
@@ -223,7 +228,7 @@ func TestInterfaceNoFragments(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := queryWithInterfaceNoFragments(ctx, client, "1") resp, err := queryWithInterfaceNoFragments(ctx, client, "1")
require.NoError(t, err) require.NoError(t, err)
@@ -286,7 +291,7 @@ func TestInterfaceListField(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := queryWithInterfaceListField(ctx, client, resp, err := queryWithInterfaceListField(ctx, client,
[]string{"1", "3", "12847394823"}) []string{"1", "3", "12847394823"})
@@ -333,7 +338,7 @@ func TestInterfaceListPointerField(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := queryWithInterfaceListPointerField(ctx, client, resp, err := queryWithInterfaceListPointerField(ctx, client,
[]string{"1", "3", "12847394823"}) []string{"1", "3", "12847394823"})
@@ -387,7 +392,7 @@ func TestFragments(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := queryWithFragments(ctx, client, []string{"1", "3", "12847394823"}) resp, err := queryWithFragments(ctx, client, []string{"1", "3", "12847394823"})
require.NoError(t, err) require.NoError(t, err)
@@ -480,7 +485,7 @@ func TestNamedFragments(t *testing.T) {
ctx := context.Background() ctx := context.Background()
server := server.RunServer() server := server.RunServer()
defer server.Close() defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient) client := newRoundtripClient(t, server.URL)
resp, err := queryWithNamedFragments(ctx, client, []string{"1", "3", "12847394823"}) resp, err := queryWithNamedFragments(ctx, client, []string{"1", "3", "12847394823"})
require.NoError(t, err) require.NoError(t, err)
+111
View File
@@ -0,0 +1,111 @@
package integration
// Machinery for integration tests to round-trip check the JSON-marshalers and
// unmarshalers we generate.
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/Khan/genqlient/graphql"
"github.com/stretchr/testify/assert"
)
// lastResponseTransport is an HTTP transport that keeps track of the last response
// that passed through it.
type lastResponseTransport struct {
wrapped http.RoundTripper
lastResponseBody []byte
}
func (t *lastResponseTransport) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := t.wrapped.RoundTrip(req)
if err != nil {
return resp, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resp, fmt.Errorf("roundtrip failed: unreadable body: %w", err)
}
t.lastResponseBody = body
// Restore the body for the next reader:
resp.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return resp, err
}
// roundtripClient is a graphql.Client that checks that
// unmarshal(marshal(req)) == req && marshal(unmarshal(resp)) == resp
// for each request it processes.
type roundtripClient struct {
wrapped graphql.Client
transport *lastResponseTransport
t *testing.T
}
// Put JSON in a stable and human-readable format.
func (c *roundtripClient) formatJSON(b []byte) []byte {
// We don't care about key ordering, so do another roundtrip through
// interface{} to drop that.
var parsed interface{}
err := json.Unmarshal(b, &parsed)
if err != nil {
c.t.Fatal(err)
}
// When marshaling, add indents to make things human-readable.
b, err = json.MarshalIndent(parsed, "", " ")
if err != nil {
c.t.Fatal(err)
}
return b
}
func (c *roundtripClient) roundtripResponse(resp interface{}) {
var graphqlResponse struct {
Data json.RawMessage `json:"data"`
}
err := json.Unmarshal(c.transport.lastResponseBody, &graphqlResponse)
if err != nil {
c.t.Error(err)
return
}
body := c.formatJSON(graphqlResponse.Data)
// resp is constructed to be unmarshal(body), so just use it
bodyAgain, err := json.Marshal(resp)
if err != nil {
c.t.Error(err)
return
}
bodyAgain = c.formatJSON(bodyAgain)
assert.Equal(c.t, string(body), string(bodyAgain))
}
func (c *roundtripClient) MakeRequest(ctx context.Context, opName, query string, retval, variables interface{}) error {
// TODO(benkraft): Also check the variables round-trip. This is a bit less
// important since most of the code is the same (and input types are
// strictly simpler), and a bit hard to do because when asserting about
// structs we need to worry about things like equality of time.Time values.
err := c.wrapped.MakeRequest(ctx, opName, query, retval, variables)
if err != nil {
return err
}
c.roundtripResponse(retval)
return nil
}
func newRoundtripClient(t *testing.T, endpoint string) graphql.Client {
transport := &lastResponseTransport{wrapped: http.DefaultTransport}
return &roundtripClient{
wrapped: graphql.NewClient(endpoint, &http.Client{Transport: transport}),
transport: transport,
t: t,
}
}
+9
View File
@@ -31,6 +31,15 @@ func GetClientFromMyContext(ctx MyContext) (graphql.Client, error) { return
const dateFormat = "2006-01-02" const dateFormat = "2006-01-02"
func MarshalDate(t *time.Time) ([]byte, error) { func MarshalDate(t *time.Time) ([]byte, error) {
// nil should never happen but we might as well check. zero-time does
// happen because omitempty doesn't consider it zero; we'd prefer to write
// null than "0001-01-01".
//
// (I mean, we're tests. Who cares! But we may as well try to match what
// prod code would want.)
if t == nil || t.IsZero() {
return []byte("null"), nil
}
return []byte(`"` + t.Format(dateFormat) + `"`), nil return []byte(`"` + t.Format(dateFormat) + `"`), nil
} }