Skip to content

Commit

Permalink
Merge pull request #894 from 99designs/enum-var-value-coercion
Browse files Browse the repository at this point in the history
Improve enum value (with vars) validation timing
  • Loading branch information
vvakame authored Nov 12, 2019
2 parents 9cfd817 + 568433a commit 15b3058
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":"INVALID is not a valid EnumTest","path":["variable","input","enum"]}],"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 @@ -188,6 +188,9 @@ func (r *queryResolver) EmbeddedCase2(ctx context.Context) (*EmbeddedCase2, erro
func (r *queryResolver) EmbeddedCase3(ctx context.Context) (*EmbeddedCase3, 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 15b3058

Please sign in to comment.