Skip to content

Commit

Permalink
Appropriately Handle Falsy Default Field Values (#1623)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilhelmeek authored Oct 7, 2021
1 parent 1a0b19f commit 210c1aa
Show file tree
Hide file tree
Showing 8 changed files with 486 additions and 15 deletions.
2 changes: 1 addition & 1 deletion codegen/input.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
asMap[k] = v
}
{{ range $field := .Fields}}
{{- if $field.Default}}
{{- if notNil "Default" $field }}
if _, present := asMap[{{$field.Name|quote}}] ; !present {
asMap[{{$field.Name|quote}}] = {{ $field.Default | dump }}
}
Expand Down
20 changes: 20 additions & 0 deletions codegen/testserver/defaults.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extend type Query {
defaultParameters(
falsyBoolean: Boolean = false
truthyBoolean: Boolean = true
): DefaultParametersMirror!
}

extend type Mutation {
defaultInput(input: DefaultInput!): DefaultParametersMirror!
}

input DefaultInput {
falsyBoolean: Boolean = false
truthyBoolean: Boolean = true
}

type DefaultParametersMirror {
falsyBoolean: Boolean
truthyBoolean: Boolean
}
68 changes: 68 additions & 0 deletions codegen/testserver/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package testserver

import (
"context"
"testing"

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

func assertDefaults(t *testing.T, ret *DefaultParametersMirror) {
require.NotNil(t, ret)
require.NotNil(t, ret.FalsyBoolean)
require.Equal(t, *ret.FalsyBoolean, false)
require.NotNil(t, ret.TruthyBoolean)
require.Equal(t, *ret.TruthyBoolean, true)
}

func TestDefaults(t *testing.T) {
resolvers := &Stub{}
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))
c := client.New(srv)

t.Run("default field parameters", func(t *testing.T) {
resolvers.QueryResolver.DefaultParameters = func(
ctx context.Context,
falsyBoolean, truthyBoolean *bool,
) (*DefaultParametersMirror, error) {
return &DefaultParametersMirror{
FalsyBoolean: falsyBoolean,
TruthyBoolean: truthyBoolean,
}, nil
}

var resp struct{ DefaultParameters *DefaultParametersMirror }
err := c.Post(`query {
defaultParameters {
falsyBoolean
truthyBoolean
}
}`, &resp)
require.NoError(t, err)
assertDefaults(t, resp.DefaultParameters)
})

t.Run("default input fields", func(t *testing.T) {
resolvers.MutationResolver.DefaultInput = func(
ctx context.Context,
input DefaultInput,
) (*DefaultParametersMirror, error) {
return &DefaultParametersMirror{
FalsyBoolean: input.FalsyBoolean,
TruthyBoolean: input.TruthyBoolean,
}, nil
}

var resp struct{ DefaultInput *DefaultParametersMirror }
err := c.Post(`mutation {
defaultInput(input: {}) {
falsyBoolean
truthyBoolean
}
}`, &resp)
require.NoError(t, err)
assertDefaults(t, resp.DefaultInput)
})
}
Loading

0 comments on commit 210c1aa

Please sign in to comment.