dd719deb4e
## Summary: This has been a bit of a thorn since we started using genqlient in production: just as you might want to specify, say, `omitempty` on an argument, you might equally want to specify it on an input-type field. But there's no obvious syntax to do that, because the input-type field does not appear in the query (only the schema) so there's nowhere to put the `# @genqlient` directive. This commit, at last, fixes that problem, via a new option, `for`, which you use in an option applied to the entire operation (or fragment), and says, "actually, apply this directive to the given field, not the entire operation". (It's mainly useful for input types, but I allowed it for output types too; I could imagine it being convenient if you want to say you always use a certain type or type-name for a certain field.) It works basically like you expect: the inline options take precedence over `for` take precedence over query-global options. The implementation was fairly straightforward once I did a little refactoring, mostly in the directive-parsing and directive-merging (which are now combined, since merging is now a bit more complicated). With that in place, and extended to support `for`, we need only add the same wiring to input-fields that we have for other places you can put directives. I did not attempt to solve the issue I've now documented as #123, wherein conflicting options can lead to confusing behavior; the new `for` is a new and perhaps more attractive avenue to cause it but the issue remains the same and requires nontrivial refactoring (described in the issue) to solve. (The breakage isn't horrible for the most part; the option will just apply, or not apply, where you don't expect it to.) But while applying that logic, I noticed a problem, which is that we were inconsistently cascading operation-level options down to input-object fields. (I think this came out of the fact that initially I thought to cascade them, then realized that this could cause problems like #123 and intended to walk them back, but then accidentally only "fixed" it for `omitempty`. I guess until this change, operation-level options were rare enough, and input-field options messy enough, that no one noticed.) So in this commit I bring things back into consistency, by saying that they do cascade: with at least a sketch of a path forward to solve #123 via better validation, I think that's by far the clearest behavior. Issue: https://github.com/Khan/genqlient/issues/14 ## Test plan: make check Author: benjaminjkraft Reviewers: csilvers, StevenACoffman, benjaminjkraft, aberkan, dnerdy, jvoll, mahtabsabet, MiguelCastillo Required Reviewers: Approved By: csilvers, StevenACoffman 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/124
207 lines
5.2 KiB
Go
207 lines
5.2 KiB
Go
package test
|
|
|
|
// Code generated by github.com/Khan/genqlient, DO NOT EDIT.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/Khan/genqlient/graphql"
|
|
"github.com/Khan/genqlient/internal/testutil"
|
|
)
|
|
|
|
// OmitEmptyQueryResponse is returned by OmitEmptyQuery on success.
|
|
type OmitEmptyQueryResponse struct {
|
|
// user looks up a user by some stuff.
|
|
//
|
|
// See UserQueryInput for what stuff is supported.
|
|
// If query is null, returns the current user.
|
|
User OmitEmptyQueryUser `json:"user"`
|
|
Users []OmitEmptyQueryUsersUser `json:"users"`
|
|
MaybeConvert time.Time `json:"maybeConvert"`
|
|
Convert2 time.Time `json:"convert2"`
|
|
}
|
|
|
|
// OmitEmptyQueryUser includes the requested fields of the GraphQL type User.
|
|
// The GraphQL type's documentation follows.
|
|
//
|
|
// A User is a user!
|
|
type OmitEmptyQueryUser struct {
|
|
// id is the user's ID.
|
|
//
|
|
// It is stable, unique, and opaque, like all good IDs.
|
|
Id testutil.ID `json:"id"`
|
|
}
|
|
|
|
// OmitEmptyQueryUsersUser includes the requested fields of the GraphQL type User.
|
|
// The GraphQL type's documentation follows.
|
|
//
|
|
// A User is a user!
|
|
type OmitEmptyQueryUsersUser struct {
|
|
// id is the user's ID.
|
|
//
|
|
// It is stable, unique, and opaque, like all good IDs.
|
|
Id testutil.ID `json:"id"`
|
|
}
|
|
|
|
// Role is a type a user may have.
|
|
type Role string
|
|
|
|
const (
|
|
// What is a student?
|
|
//
|
|
// A student is primarily a person enrolled in a school or other educational institution and who is under learning with goals of acquiring knowledge, developing professions and achieving employment at desired field. In the broader sense, a student is anyone who applies themselves to the intensive intellectual engagement with some matter necessary to master it as part of some practical affair in which such mastery is basic or decisive.
|
|
//
|
|
// (from [Wikipedia](https://en.wikipedia.org/wiki/Student))
|
|
RoleStudent Role = "STUDENT"
|
|
// Teacher is a teacher, who teaches the students.
|
|
RoleTeacher Role = "TEACHER"
|
|
)
|
|
|
|
// UserQueryInput is the argument to Query.users.
|
|
//
|
|
// Ideally this would support anything and everything!
|
|
// Or maybe ideally it wouldn't.
|
|
// Really I'm just talking to make this documentation longer.
|
|
type UserQueryInput struct {
|
|
Email string `json:"email,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
// id looks the user up by ID. It's a great way to look up users.
|
|
Id testutil.ID `json:"id"`
|
|
Role Role `json:"role,omitempty"`
|
|
Names []string `json:"names,omitempty"`
|
|
HasPokemon testutil.Pokemon `json:"hasPokemon,omitempty"`
|
|
Birthdate time.Time `json:"-"`
|
|
}
|
|
|
|
func (v *UserQueryInput) UnmarshalJSON(b []byte) error {
|
|
|
|
if string(b) == "null" {
|
|
return nil
|
|
}
|
|
|
|
var firstPass struct {
|
|
*UserQueryInput
|
|
Birthdate json.RawMessage `json:"birthdate"`
|
|
graphql.NoUnmarshalJSON
|
|
}
|
|
firstPass.UserQueryInput = v
|
|
|
|
err := json.Unmarshal(b, &firstPass)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
{
|
|
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
|
|
}
|
|
|
|
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
|
|
var err error
|
|
*dst, err = testutil.MarshalDate(
|
|
&src)
|
|
if err != nil {
|
|
return nil, fmt.Errorf(
|
|
"Unable to marshal UserQueryInput.Birthdate: %w", err)
|
|
}
|
|
}
|
|
return &retval, nil
|
|
}
|
|
|
|
// __OmitEmptyQueryInput is used internally by genqlient
|
|
type __OmitEmptyQueryInput struct {
|
|
Query UserQueryInput `json:"query,omitempty"`
|
|
Queries []UserQueryInput `json:"queries,omitempty"`
|
|
Dt time.Time `json:"dt,omitempty"`
|
|
Tz string `json:"tz,omitempty"`
|
|
TzNoOmitEmpty string `json:"tzNoOmitEmpty"`
|
|
}
|
|
|
|
func OmitEmptyQuery(
|
|
client graphql.Client,
|
|
query UserQueryInput,
|
|
queries []UserQueryInput,
|
|
dt time.Time,
|
|
tz string,
|
|
tzNoOmitEmpty string,
|
|
) (*OmitEmptyQueryResponse, error) {
|
|
__input := __OmitEmptyQueryInput{
|
|
Query: query,
|
|
Queries: queries,
|
|
Dt: dt,
|
|
Tz: tz,
|
|
TzNoOmitEmpty: tzNoOmitEmpty,
|
|
}
|
|
var err error
|
|
|
|
var retval OmitEmptyQueryResponse
|
|
err = client.MakeRequest(
|
|
nil,
|
|
"OmitEmptyQuery",
|
|
`
|
|
query OmitEmptyQuery ($query: UserQueryInput, $queries: [UserQueryInput], $dt: DateTime, $tz: String, $tzNoOmitEmpty: String) {
|
|
user(query: $query) {
|
|
id
|
|
}
|
|
users(query: $queries) {
|
|
id
|
|
}
|
|
maybeConvert(dt: $dt, tz: $tz)
|
|
convert2: maybeConvert(dt: $dt, tz: $tzNoOmitEmpty)
|
|
}
|
|
`,
|
|
&retval,
|
|
&__input,
|
|
)
|
|
return &retval, err
|
|
}
|
|
|