From 0432f3f434ec05d5fff0751ff2b34d40f0bf2479 Mon Sep 17 00:00:00 2001 From: Ben Kraft Date: Fri, 2 Apr 2021 16:34:15 -0700 Subject: [PATCH] miscellaneous test coverage --- README.md | 1 - .../testdata/queries/SimpleMutation.graphql | 10 ++++ .../queries/SimpleMutation.graphql.go | 46 +++++++++++++++++++ .../queries/SimpleMutation.graphql.json | 9 ++++ generate/testdata/queries/schema.graphql | 4 ++ generate/types.go | 21 ++++++--- 6 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 generate/testdata/queries/SimpleMutation.graphql create mode 100644 generate/testdata/queries/SimpleMutation.graphql.go create mode 100644 generate/testdata/queries/SimpleMutation.graphql.json diff --git a/README.md b/README.md index e92126f..384e012 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,6 @@ Config options: Other: - (+) improved validation and error checking -- (+) more tests - (+) documentation - get a designer to fix my bad logo-thing - custom scalar types (or custom mappings for standard scalars, if you want a special ID type say) diff --git a/generate/testdata/queries/SimpleMutation.graphql b/generate/testdata/queries/SimpleMutation.graphql new file mode 100644 index 0000000..d2afc81 --- /dev/null +++ b/generate/testdata/queries/SimpleMutation.graphql @@ -0,0 +1,10 @@ +# SimpleMutation creates a user. +# +# It has a long doc-comment, to test that we handle that correctly. +# What a long comment indeed. +mutation SimpleMutation($name: String!) { + createUser(name: $name) { + id + name + } +} diff --git a/generate/testdata/queries/SimpleMutation.graphql.go b/generate/testdata/queries/SimpleMutation.graphql.go new file mode 100644 index 0000000..11616d1 --- /dev/null +++ b/generate/testdata/queries/SimpleMutation.graphql.go @@ -0,0 +1,46 @@ +package test + +// Code generated by github.com/Khan/genqlient, DO NOT EDIT. + +import ( + "github.com/Khan/genqlient/graphql" +) + +type SimpleMutationCreateUser struct { + Id string `json:"id"` + Name string `json:"name"` +} + +type SimpleMutationResponse struct { + CreateUser SimpleMutationCreateUser `json:"createUser"` +} + +// SimpleMutation creates a user. +// +// It has a long doc-comment, to test that we handle that correctly. +// What a long comment indeed. +func SimpleMutation( + client graphql.Client, + name string, +) (*SimpleMutationResponse, error) { + variables := map[string]interface{}{ + "name": name, + } + + var retval SimpleMutationResponse + err := client.MakeRequest( + nil, + "SimpleMutation", + ` +mutation SimpleMutation ($name: String!) { + createUser(name: $name) { + id + name + } +} +`, + &retval, + variables, + ) + return &retval, err +} diff --git a/generate/testdata/queries/SimpleMutation.graphql.json b/generate/testdata/queries/SimpleMutation.graphql.json new file mode 100644 index 0000000..2191897 --- /dev/null +++ b/generate/testdata/queries/SimpleMutation.graphql.json @@ -0,0 +1,9 @@ +{ + "operations": [ + { + "operationName": "SimpleMutation", + "query": "\nmutation SimpleMutation ($name: String!) {\n\tcreateUser(name: $name) {\n\t\tid\n\t\tname\n\t}\n}\n", + "sourceLocation": "testdata/queries/SimpleMutation.graphql" + } + ] +} \ No newline at end of file diff --git a/generate/testdata/queries/schema.graphql b/generate/testdata/queries/schema.graphql index f697e04..27e0ee5 100644 --- a/generate/testdata/queries/schema.graphql +++ b/generate/testdata/queries/schema.graphql @@ -61,3 +61,7 @@ type Query { root: Topic! randomLeaf: LeafContent! } + +type Mutation { + createUser(name: String!, email: String): User +} diff --git a/generate/types.go b/generate/types.go index dfb110e..dda4922 100644 --- a/generate/types.go +++ b/generate/types.go @@ -14,16 +14,19 @@ type typeBuilder struct { *generator } -func (g *generator) baseTypeForOperation(operation ast.Operation) *ast.Definition { +func (g *generator) baseTypeForOperation(operation ast.Operation) (*ast.Definition, error) { switch operation { case ast.Query: - return g.schema.Query + return g.schema.Query, nil case ast.Mutation: - return g.schema.Mutation + return g.schema.Mutation, nil case ast.Subscription: - return g.schema.Subscription + if !allowBrokenFeatures { + return nil, fmt.Errorf("genqlient does not yet support subscriptions") + } + return g.schema.Subscription, nil default: - panic(fmt.Sprintf("unexpected operation: %v", operation)) + return nil, fmt.Errorf("unexpected operation: %v", operation) } } @@ -41,8 +44,12 @@ func (g *generator) getTypeForOperation(operation *ast.OperationDefinition) (nam return "", err } - return g.addTypeForDefinition( - operation.Name, name, g.baseTypeForOperation(operation.Operation), fields) + baseType, err := g.baseTypeForOperation(operation.Operation) + if err != nil { + return "", err + } + + return g.addTypeForDefinition(operation.Name, name, baseType, fields) } var builtinTypes = map[string]string{