github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/defaults_test.go (about) 1 package followschema 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/operandinc/gqlgen/client" 8 "github.com/operandinc/gqlgen/graphql/handler" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func assertDefaults(t *testing.T, ret *DefaultParametersMirror) { 13 require.NotNil(t, ret) 14 require.NotNil(t, ret.FalsyBoolean) 15 require.Equal(t, *ret.FalsyBoolean, false) 16 require.NotNil(t, ret.TruthyBoolean) 17 require.Equal(t, *ret.TruthyBoolean, true) 18 } 19 20 func TestDefaults(t *testing.T) { 21 resolvers := &Stub{} 22 srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})) 23 c := client.New(srv) 24 25 t.Run("default field parameters", func(t *testing.T) { 26 resolvers.QueryResolver.DefaultParameters = func( 27 ctx context.Context, 28 falsyBoolean, truthyBoolean *bool, 29 ) (*DefaultParametersMirror, error) { 30 return &DefaultParametersMirror{ 31 FalsyBoolean: falsyBoolean, 32 TruthyBoolean: truthyBoolean, 33 }, nil 34 } 35 36 var resp struct{ DefaultParameters *DefaultParametersMirror } 37 err := c.Post(`query { 38 defaultParameters { 39 falsyBoolean 40 truthyBoolean 41 } 42 }`, &resp) 43 require.NoError(t, err) 44 assertDefaults(t, resp.DefaultParameters) 45 }) 46 47 t.Run("default input fields", func(t *testing.T) { 48 resolvers.MutationResolver.DefaultInput = func( 49 ctx context.Context, 50 input DefaultInput, 51 ) (*DefaultParametersMirror, error) { 52 return &DefaultParametersMirror{ 53 FalsyBoolean: input.FalsyBoolean, 54 TruthyBoolean: input.TruthyBoolean, 55 }, nil 56 } 57 58 var resp struct{ DefaultInput *DefaultParametersMirror } 59 err := c.Post(`mutation { 60 defaultInput(input: {}) { 61 falsyBoolean 62 truthyBoolean 63 } 64 }`, &resp) 65 require.NoError(t, err) 66 assertDefaults(t, resp.DefaultInput) 67 }) 68 }