Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve enum value (with vars) validation timing #894

Merged
merged 3 commits into from Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions codegen/testserver/enum.graphql
@@ -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
@@ -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
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
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.