Refactor argument-handling to use a struct (#103)

## Summary:
In this commit I refactor the argument-generation logic to move most of
the code out of the template and into the type-generator.  This logic
predates #51, and I didn't think to update it there, but I think it
benefits from similar treatment, for similar reasons.

Specifically, the main change is to treat variables as another struct
type we can generate, rather than handling them inline as a
`map[string]interface{}`.  Users still pass them the same way, but
instead of putting them into a `map[string]interface{}` and JSONifying
that, we generate a struct and put them there.

This turns out to simplify things quite a lot, because we already have a
lot of code to generate types.  Notably, the omitempty code goes from a
dozen lines to basically two, and fixes a bug (#43) in the process,
because now that we have a struct, `json.Marshal` will do our work for
us! (And, once we have syntax for it (#14), we'll be able to handle
field-level omitempty basically for free.)  More importantly, it will
simplify custom marshalers (#38, forthcoming) significantly, since we do
all that logic at the containing-struct level, but will need to apply it
to arguments.

It does require two breaking changes:

1. For folks implementing the `graphql.Client` API (rather than just
   calling `NewClient`): we now pass them variables as an `interface{}`
   rather than a `map[string]interface{}`.  For most callers, including
   Khan/webapp, this is basically a one-line change to the signature of
   their `MakeRequest`, and it should be a lot more future-proof.
2. genqlient's handling of the `omitempty` option has changed to match
   that of `encoding/json`, in particular it now never considers structs
   "empty".  The difference was never intentional (I just didn't realize
   that behavior of `encoding/json`); arguably our behavior was more
   useful but I think that's outweighed by the value of consistency with
   `encoding/json` as well as the simpler and more correct
   implementation (fixing #43 is actually quite nontrivial otherwise).
   Once we have custom unmarshaler support (#38), users will be able to
   map a zero value to JSON null if they wish, which is mostly if not
   entirely equivalent for GraphQL's purposes.

Issue: https://github.com/Khan/genqlient/issues/38
Issue: https://github.com/Khan/genqlient/issues/43

## Test plan:
make check

Author: benjaminjkraft

Reviewers: StevenACoffman, 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,  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14)

Pull Request URL: https://github.com/Khan/genqlient/pull/103
This commit is contained in:
Ben Kraft
2021-09-22 17:16:36 -07:00
committed by GitHub
parent ab1aaed845
commit 5995653583
26 changed files with 395 additions and 206 deletions
+71 -13
View File
@@ -7,7 +7,7 @@ package generate
// into code, in types.go.
//
// The entrypoints are convertOperation, which builds the response-type for a
// query, and convertInputType, which builds the argument-types.
// query, and convertArguments, which builds the argument-types.
import (
"fmt"
@@ -139,15 +139,66 @@ var builtinTypes = map[string]string{
"ID": "string",
}
// convertInputType decides the Go type we will generate corresponding to an
// argument to a GraphQL operation.
func (g *generator) convertInputType(
typ *ast.Type,
options, queryOptions *genqlientDirective,
) (goType, error) {
// note prefix is ignored here (see generator.typeName), as is selectionSet
// (for input types we use the whole thing).
return g.convertType(nil, typ, nil, options, queryOptions)
// convertArguments builds the type of the GraphQL arguments to the given
// operation.
//
// This type is not exposed to the user; it's just used internally in the
// unmarshaler; and it's used as a container
func (g *generator) convertArguments(
operation *ast.OperationDefinition,
queryOptions *genqlientDirective,
) (*goStructType, error) {
if len(operation.VariableDefinitions) == 0 {
return nil, nil
}
name := "__" + operation.Name + "Input"
fields := make([]*goStructField, len(operation.VariableDefinitions))
for i, arg := range operation.VariableDefinitions {
_, directive, err := g.parsePrecedingComment(arg, arg.Position)
if err != nil {
return nil, err
}
options := queryOptions.merge(directive)
goName := upperFirst(arg.Variable)
// Some of the arguments don't apply here, namely the name-prefix (see
// names.go) and the selection-set (we use all the input type's fields,
// and so on recursively). See also the `case ast.InputObject` in
// convertDefinition, below.
goTyp, err := g.convertType(nil, arg.Type, nil, options, queryOptions)
if err != nil {
return nil, err
}
fields[i] = &goStructField{
GoName: goName,
GoType: goTyp,
JSONName: arg.Variable,
GraphQLName: arg.Variable,
Omitempty: options.GetOmitempty(),
}
}
goTyp := &goStructType{
GoName: name,
Fields: fields,
Selection: nil,
IsInput: true,
descriptionInfo: descriptionInfo{
CommentOverride: fmt.Sprintf("%s is used internally by genqlient", name),
// fake name, used by addType
GraphQLName: name,
},
}
goTypAgain, err := g.addType(goTyp, goTyp.GoName, operation.Position)
if err != nil {
return nil, err
}
goTyp, ok := goTypAgain.(*goStructType)
if !ok {
return nil, errorf(
operation.Position, "internal error: input type was %T", goTypAgain)
}
return goTyp, nil
}
// convertType decides the Go type we will generate corresponding to a
@@ -305,11 +356,16 @@ func (g *generator) convertDefinition(
for i, field := range def.Fields {
goName := upperFirst(field.Name)
// Several of the arguments don't really make sense here:
// Several of the arguments don't really make sense here
// (note field.Type is necessarily a scalar, input, or enum)
// - no field-specific options can apply, because this is
// a field in the type, not in the query (see also #14).
// - namePrefix is ignored for input types; see note in
// generator.typeName.
// - namePrefix is ignored for input types and enums (see
// names.go) and for scalars (they use client-specified
// names)
// - selectionSet is ignored for input types, because we
// just use all fields of the type; and it's nonexistent
// for scalars and enums, our only other possible types,
// TODO(benkraft): Can we refactor to avoid passing the values that
// will be ignored? We know field.Type is a scalar, enum, or input
// type. But plumbing that is a bit tricky in practice.
@@ -325,6 +381,8 @@ func (g *generator) convertDefinition(
JSONName: field.Name,
GraphQLName: field.Name,
Description: field.Description,
// TODO(benkraft): set Omitempty once we have a way for the
// user to specify it.
}
}
return goType, nil
+9 -40
View File
@@ -55,8 +55,11 @@ type operation struct {
Doc string `json:"-"`
// The body of the operation to send.
Body string `json:"query"`
// The arguments to the operation.
Args []argument `json:"-"`
// The type of the argument to the operation, which we use both internally
// and to construct the arguments. We do it this way so we can use the
// machinery we have for handling (and, specifically, json-marshaling)
// types.
Input *goStructType `json:"-"`
// The type-name for the operation's response type.
ResponseName string `json:"-"`
// The original filename from which we got this query.
@@ -69,14 +72,6 @@ type exportedOperations struct {
Operations []*operation `json:"operations"`
}
type argument struct {
GoName string
GoType string
GraphQLName string
IsSlice bool
Options *genqlientDirective
}
func newGenerator(
config *Config,
schema *ast.Schema,
@@ -125,29 +120,6 @@ func (g *generator) WriteTypes(w io.Writer) error {
return nil
}
func (g *generator) getArgument(
arg *ast.VariableDefinition,
operationDirective *genqlientDirective,
) (argument, error) {
_, directive, err := g.parsePrecedingComment(arg, arg.Position)
if err != nil {
return argument{}, err
}
graphQLName := arg.Variable
goTyp, err := g.convertInputType(arg.Type, directive, operationDirective)
if err != nil {
return argument{}, err
}
return argument{
GraphQLName: graphQLName,
GoName: lowerFirst(graphQLName),
GoType: goTyp.Reference(),
IsSlice: arg.Type.Elem != nil,
Options: operationDirective.merge(directive),
}, nil
}
// usedFragmentNames returns the named-fragments used by (i.e. spread into)
// this operation.
func (g *generator) usedFragments(op *ast.OperationDefinition) ast.FragmentDefinitionList {
@@ -277,12 +249,9 @@ func (g *generator) addOperation(op *ast.OperationDefinition) error {
return err
}
args := make([]argument, len(op.VariableDefinitions))
for i, arg := range op.VariableDefinitions {
args[i], err = g.getArgument(arg, directive)
if err != nil {
return err
}
inputType, err := g.convertArguments(op, directive)
if err != nil {
return err
}
responseType, err := g.convertOperation(op, directive)
@@ -313,7 +282,7 @@ func (g *generator) addOperation(op *ast.OperationDefinition) error {
// rather than in the template so exported operations will match
// *exactly* what we send to the server.
Body: "\n" + builder.String(),
Args: args,
Input: inputType,
ResponseName: responseType.Reference(),
SourceFilename: sourceFilename,
Config: g.Config, // for the convenience of the template
-3
View File
@@ -108,9 +108,6 @@ func TestGenerate(t *testing.T) {
t.Run("Build", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping build due to -short")
} else if sourceFilename == "Omitempty.graphql" {
t.Skip("TODO: enable after fixing " +
"https://github.com/Khan/genqlient/issues/43")
}
err := buildGoFile(sourceFilename, generated[goFilename])
+14 -23
View File
@@ -6,32 +6,23 @@ func {{.Name}}(
{{- if not .Config.ClientGetter -}}
client {{ref "github.com/Khan/genqlient/graphql.Client"}},
{{end}}
{{- range .Args -}}
{{.GoName}} {{.GoType}},
{{- if .Input -}}
{{- range .Input.Fields -}}
{{/* the GraphQL name here is the user-specified variable-name */ -}}
{{.GraphQLName}} {{.GoType.Reference}},
{{end -}}
{{end -}}
) (*{{.ResponseName}}, error) {
{{- if .Args -}}
variables := map[string]interface{}{
{{range .Args -}}
{{if not .Options.GetOmitempty -}}
"{{.GraphQLName}}": {{.GoName}},
{{- if .Input -}}
{{/* We need to avoid conflicting with any of the function's argument names
which are derived from the GraphQL argument names; notably `input` is
a common one. So we use a name that's not legal in GraphQL, namely
one starting with a double-underscore. */ -}}
__input := {{.Input.GoName}}{
{{range .Input.Fields -}}
{{.GoName}}: {{.GraphQLName}},
{{end -}}
{{end}}
}
{{range .Args -}}
{{if .Options.GetOmitempty -}}
{{if .IsSlice -}}
if len({{.GoName}}) > 0 {
{{else -}}
{{/* zero_{{.GoType}} would be a better name, but {{.GoType}} would require
munging since it might be, say, `time.Time`. */}}
var zero_{{.GoName}} {{.GoType}}
if {{.GoName}} != zero_{{.GoName}} {
{{end -}}
variables["{{.GraphQLName}}"] = {{.GoName}}
}
{{end}}
{{end}}
{{end -}}
var err error
@@ -48,7 +39,7 @@ func {{.Name}}(
"{{.Name}}",
`{{.Body}}`,
&retval,
{{if .Args}}variables{{else}}nil{{end}},
{{if .Input}}&__input{{else}}nil{{end}},
)
return &retval, err
}
@@ -8,6 +8,12 @@ import (
"github.com/Khan/genqlient/graphql"
)
// __convertTimezoneInput is used internally by genqlient
type __convertTimezoneInput struct {
Dt time.Time `json:"dt"`
Tz string `json:"tz"`
}
// convertTimezoneResponse is returned by convertTimezone on success.
type convertTimezoneResponse struct {
Convert time.Time `json:"convert"`
@@ -18,11 +24,10 @@ func convertTimezone(
dt time.Time,
tz string,
) (*convertTimezoneResponse, error) {
variables := map[string]interface{}{
"dt": dt,
"tz": tz,
__input := __convertTimezoneInput{
Dt: dt,
Tz: tz,
}
var err error
var retval convertTimezoneResponse
@@ -35,7 +40,7 @@ query convertTimezone ($dt: DateTime!, $tz: String) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -38,14 +38,18 @@ const (
RoleTeacher Role = "TEACHER"
)
// __InputEnumQueryInput is used internally by genqlient
type __InputEnumQueryInput struct {
Role Role `json:"role"`
}
func InputEnumQuery(
client graphql.Client,
role Role,
) (*InputEnumQueryResponse, error) {
variables := map[string]interface{}{
"role": role,
__input := __InputEnumQueryInput{
Role: role,
}
var err error
var retval InputEnumQueryResponse
@@ -60,7 +64,7 @@ query InputEnumQuery ($role: Role!) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -56,14 +56,18 @@ type UserQueryInput struct {
HasPokemon testutil.Pokemon `json:"hasPokemon"`
}
// __InputObjectQueryInput is used internally by genqlient
type __InputObjectQueryInput struct {
Query UserQueryInput `json:"query"`
}
func InputObjectQuery(
client graphql.Client,
query UserQueryInput,
) (*InputObjectQueryResponse, error) {
variables := map[string]interface{}{
"query": query,
__input := __InputObjectQueryInput{
Query: query,
}
var err error
var retval InputObjectQueryResponse
@@ -78,7 +82,7 @@ query InputObjectQuery ($query: UserQueryInput) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -27,14 +27,18 @@ type ListInputQueryUser struct {
Id testutil.ID `json:"id"`
}
// __ListInputQueryInput is used internally by genqlient
type __ListInputQueryInput struct {
Names []string `json:"names"`
}
func ListInputQuery(
client graphql.Client,
names []string,
) (*ListInputQueryResponse, error) {
variables := map[string]interface{}{
"names": names,
__input := __ListInputQueryInput{
Names: names,
}
var err error
var retval ListInputQueryResponse
@@ -49,7 +53,7 @@ query ListInputQuery ($names: [String]) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -72,6 +72,15 @@ type UserQueryInput struct {
HasPokemon testutil.Pokemon `json:"hasPokemon"`
}
// __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,
@@ -80,29 +89,13 @@ func OmitEmptyQuery(
tz string,
tzNoOmitEmpty string,
) (*OmitEmptyQueryResponse, error) {
variables := map[string]interface{}{
"tzNoOmitEmpty": tzNoOmitEmpty,
__input := __OmitEmptyQueryInput{
Query: query,
Queries: queries,
Dt: dt,
Tz: tz,
TzNoOmitEmpty: tzNoOmitEmpty,
}
var zero_query UserQueryInput
if query != zero_query {
variables["query"] = query
}
if len(queries) > 0 {
variables["queries"] = queries
}
var zero_dt time.Time
if dt != zero_dt {
variables["dt"] = dt
}
var zero_tz string
if tz != zero_tz {
variables["tz"] = tz
}
var err error
var retval OmitEmptyQueryResponse
@@ -122,7 +115,7 @@ query OmitEmptyQuery ($query: UserQueryInput, $queries: [UserQueryInput], $dt: D
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -79,18 +79,24 @@ type UserQueryInput struct {
HasPokemon *testutil.Pokemon `json:"hasPokemon"`
}
// __PointersQueryInput is used internally by genqlient
type __PointersQueryInput struct {
Query *UserQueryInput `json:"query"`
Dt time.Time `json:"dt"`
Tz *string `json:"tz"`
}
func PointersQuery(
client graphql.Client,
query UserQueryInput,
query *UserQueryInput,
dt time.Time,
tz string,
tz *string,
) (*PointersQueryResponse, error) {
variables := map[string]interface{}{
"query": query,
"dt": dt,
"tz": tz,
__input := __PointersQueryInput{
Query: query,
Dt: dt,
Tz: tz,
}
var err error
var retval PointersQueryResponse
@@ -113,7 +119,7 @@ query PointersQuery ($query: UserQueryInput, $dt: DateTime, $tz: String) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -79,18 +79,24 @@ type UserQueryInput struct {
HasPokemon testutil.Pokemon `json:"hasPokemon"`
}
// __PointersQueryInput is used internally by genqlient
type __PointersQueryInput struct {
Query *UserQueryInput `json:"query"`
Dt *time.Time `json:"dt"`
Tz string `json:"tz"`
}
func PointersQuery(
client graphql.Client,
query *UserQueryInput,
dt *time.Time,
tz string,
) (*PointersQueryResponse, error) {
variables := map[string]interface{}{
"query": query,
"dt": dt,
"tz": tz,
__input := __PointersQueryInput{
Query: query,
Dt: dt,
Tz: tz,
}
var err error
var retval PointersQueryResponse
@@ -113,7 +119,7 @@ query PointersQuery ($query: UserQueryInput, $dt: DateTime, $tz: String) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -37,14 +37,18 @@ type GetPokemonSiblingsUserGenqlientPokemon struct {
Level int `json:"level"`
}
// __GetPokemonSiblingsInput is used internally by genqlient
type __GetPokemonSiblingsInput struct {
Input testutil.Pokemon `json:"input"`
}
func GetPokemonSiblings(
client graphql.Client,
input testutil.Pokemon,
) (*GetPokemonSiblingsResponse, error) {
variables := map[string]interface{}{
"input": input,
__input := __GetPokemonSiblingsInput{
Input: input,
}
var err error
var retval GetPokemonSiblingsResponse
@@ -69,7 +73,7 @@ query GetPokemonSiblings ($input: PokemonInput!) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -36,14 +36,18 @@ type RecursiveInput struct {
Rec []RecursiveInput `json:"rec"`
}
// __RecursionInput is used internally by genqlient
type __RecursionInput struct {
Input RecursiveInput `json:"input"`
}
func Recursion(
client graphql.Client,
input RecursiveInput,
) (*RecursionResponse, error) {
variables := map[string]interface{}{
"input": input,
__input := __RecursionInput{
Input: input,
}
var err error
var retval RecursionResponse
@@ -64,7 +68,7 @@ query Recursion ($input: RecursiveInput!) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -27,14 +27,18 @@ type SimpleInputQueryUser struct {
Id testutil.ID `json:"id"`
}
// __SimpleInputQueryInput is used internally by genqlient
type __SimpleInputQueryInput struct {
Name string `json:"name"`
}
func SimpleInputQuery(
client graphql.Client,
name string,
) (*SimpleInputQueryResponse, error) {
variables := map[string]interface{}{
"name": name,
__input := __SimpleInputQueryInput{
Name: name,
}
var err error
var retval SimpleInputQueryResponse
@@ -49,7 +53,7 @@ query SimpleInputQuery ($name: String!) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -24,6 +24,11 @@ type SimpleMutationResponse struct {
CreateUser SimpleMutationCreateUser `json:"createUser"`
}
// __SimpleMutationInput is used internally by genqlient
type __SimpleMutationInput struct {
Name string `json:"name"`
}
// SimpleMutation creates a user.
//
// It has a long doc-comment, to test that we handle that correctly.
@@ -32,10 +37,9 @@ func SimpleMutation(
client graphql.Client,
name string,
) (*SimpleMutationResponse, error) {
variables := map[string]interface{}{
"name": name,
__input := __SimpleMutationInput{
Name: name,
}
var err error
var retval SimpleMutationResponse
@@ -51,7 +55,7 @@ mutation SimpleMutation ($name: String!) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
@@ -36,6 +36,11 @@ type UserQueryInput struct {
HasPokemon testutil.Pokemon `json:"hasPokemon"`
}
// __unexportedInput is used internally by genqlient
type __unexportedInput struct {
Query UserQueryInput `json:"query"`
}
// unexportedResponse is returned by unexported on success.
type unexportedResponse struct {
// user looks up a user by some stuff.
@@ -60,10 +65,9 @@ func unexported(
client graphql.Client,
query UserQueryInput,
) (*unexportedResponse, error) {
variables := map[string]interface{}{
"query": query,
__input := __unexportedInput{
Query: query,
}
var err error
var retval unexportedResponse
@@ -78,7 +82,7 @@ query unexported ($query: UserQueryInput) {
}
`,
&retval,
variables,
&__input,
)
return &retval, err
}
+12 -4
View File
@@ -139,6 +139,7 @@ type goStructField struct {
GoType goType
JSONName string // i.e. the field's alias in this query
GraphQLName string // i.e. the field's name in its type-def
Omitempty bool // only used on input types
Description string
}
@@ -162,19 +163,23 @@ func (typ *goStructType) WriteDefinition(w io.Writer, g *generator) error {
fmt.Fprintf(w, "type %s struct {\n", typ.GoName)
for _, field := range typ.Fields {
writeDescription(w, field.Description)
jsonName := field.JSONName
jsonTag := `"` + field.JSONName
if field.Omitempty {
jsonTag += ",omitempty"
}
jsonTag += `"`
if field.IsAbstract() {
// abstract types are handled in our UnmarshalJSON (see below)
needUnmarshaler = true
jsonName = "-"
jsonTag = `"-"`
}
if field.IsEmbedded() {
// embedded fields also need UnmarshalJSON handling (see below)
needUnmarshaler = true
fmt.Fprintf(w, "\t%s `json:\"-\"`\n", field.GoType.Unwrap().Reference())
} else {
fmt.Fprintf(w, "\t%s %s `json:\"%s\"`\n",
field.GoName, field.GoType.Reference(), jsonName)
fmt.Fprintf(w, "\t%s %s `json:%s`\n",
field.GoName, field.GoType.Reference(), jsonTag)
}
}
fmt.Fprintf(w, "}\n")
@@ -198,6 +203,9 @@ func (typ *goStructType) WriteDefinition(w io.Writer, g *generator) error {
// select the same field, or several fragments select the same field -- the
// JSON library will only fill one of those (the least-nested one); we want
// to fill them all.
//
// 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.
if !needUnmarshaler {
return nil
}