github.com/99designs/gqlgen@v0.17.45/codegen/testserver/singlefile/enums_test.go (about) 1 package singlefile 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/99designs/gqlgen/client" 10 "github.com/99designs/gqlgen/graphql/handler" 11 ) 12 13 func TestEnumsResolver(t *testing.T) { 14 resolvers := &Stub{} 15 resolvers.QueryResolver.EnumInInput = func(ctx context.Context, input *InputWithEnumValue) (EnumTest, error) { 16 return input.Enum, nil 17 } 18 19 c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))) 20 21 t.Run("input with valid enum value", func(t *testing.T) { 22 var resp struct { 23 EnumInInput EnumTest 24 } 25 c.MustPost(`query { 26 enumInInput(input: {enum: OK}) 27 } 28 `, &resp) 29 require.Equal(t, resp.EnumInInput, EnumTestOk) 30 }) 31 32 t.Run("input with invalid enum value", func(t *testing.T) { 33 var resp struct { 34 EnumInInput EnumTest 35 } 36 err := c.Post(`query { 37 enumInInput(input: {enum: INVALID}) 38 } 39 `, &resp) 40 require.EqualError(t, err, `http 422: {"errors":[{"message":"Value \"INVALID\" does not exist in \"EnumTest!\" enum.","locations":[{"line":2,"column":30}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`) 41 }) 42 43 t.Run("input with invalid enum value via vars", func(t *testing.T) { 44 var resp struct { 45 EnumInInput EnumTest 46 } 47 err := c.Post(`query ($input: InputWithEnumValue) { 48 enumInInput(input: $input) 49 } 50 `, &resp, client.Var("input", map[string]interface{}{"enum": "INVALID"})) 51 require.EqualError(t, err, `http 422: {"errors":[{"message":"INVALID is not a valid EnumTest","path":["variable","input","enum"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`) 52 }) 53 }