Skip to content

Commit

Permalink
add test for vektah/gqlparser#109
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Oct 9, 2019
1 parent 4c35356 commit fc4e513
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 0 deletions.
12 changes: 12 additions & 0 deletions codegen/testserver/enum.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
enum EnumTest {
OK
NG
}

input InputWithEnumValue {
enum: EnumTest!
}

extend type Query {
enumInInput(input: InputWithEnumValue): EnumTest!
}
52 changes: 52 additions & 0 deletions codegen/testserver/enums_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package testserver

import (
"context"
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/stretchr/testify/require"
)

func TestEnumsResolver(t *testing.T) {
resolvers := &Stub{}
resolvers.QueryResolver.EnumInInput = func(ctx context.Context, input *InputWithEnumValue) (EnumTest, error) {
return input.Enum, nil
}

c := client.New(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))

t.Run("input with valid enum value", func(t *testing.T) {
var resp struct {
EnumInInput EnumTest
}
c.MustPost(`query {
enumInInput(input: {enum: OK})
}
`, &resp)
require.Equal(t, resp.EnumInInput, EnumTestOk)
})

t.Run("input with invalid enum value", func(t *testing.T) {
var resp struct {
EnumInInput EnumTest
}
err := c.Post(`query {
enumInInput(input: {enum: INVALID})
}
`, &resp)
require.EqualError(t, err, `http 422: {"errors":[{"message":"Expected type EnumTest!, found INVALID.","locations":[{"line":2,"column":30}]}],"data":null}`)
})

t.Run("input with invalid enum value via vars", func(t *testing.T) {
var resp struct {
EnumInInput EnumTest
}
err := c.Post(`query ($input: InputWithEnumValue) {
enumInInput(input: $input)
}
`, &resp, client.Var("input", map[string]interface{}{"enum": "INVALID"}))
require.EqualError(t, err, `http 422: {"errors":[{"message":"Expected type EnumTest!, found INVALID.","locations":[{"line":2,"column":30}]}],"data":null}`)
})
}
135 changes: 135 additions & 0 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions codegen/testserver/generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func TestEnums(t *testing.T) {
require.Equal(t, StatusOk, AllStatus[0])
require.Equal(t, StatusError, AllStatus[1])
})

t.Run("invalid enum values", func(t *testing.T) {
require.Equal(t, StatusOk, AllStatus[0])
require.Equal(t, StatusError, AllStatus[1])
})
}

func TestUnionFragments(t *testing.T) {
Expand Down
45 changes: 45 additions & 0 deletions codegen/testserver/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ func (r *queryResolver) DirectiveDouble(ctx context.Context) (*string, error) {
func (r *queryResolver) DirectiveUnimplemented(ctx context.Context) (*string, error) {
panic("not implemented")
}
func (r *queryResolver) EnumInInput(ctx context.Context, input *InputWithEnumValue) (EnumTest, error) {
panic("not implemented")
}
func (r *queryResolver) Shapes(ctx context.Context) ([]Shape, error) {
panic("not implemented")
}
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc4e513

Please sign in to comment.