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

fix: input object with a nullable field set to null was being removed… #631

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ func coerceValue(ttype Input, value interface{}) interface{} {
}

for name, field := range ttype.Fields() {
fieldValue := coerceValue(field.Type, valueMap[name])
value, presentInValueMap := valueMap[name]
fieldValue := coerceValue(field.Type, value)
if isNullish(fieldValue) {
fieldValue = field.DefaultValue
}
if !isNullish(fieldValue) {
if !isNullish(fieldValue) || presentInValueMap {
obj[name] = fieldValue
}
}
Expand Down
37 changes: 37 additions & 0 deletions variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,43 @@ func TestVariables_ObjectsAndNullability_UsingVariables_ExecutesWithComplexInput
}
}

func TestVariables_ObjectsAndNullability_UsingVariables_PassesNullWhenNoDefaultIsProvided(t *testing.T) {

doc := `
query q($input: TestInputObject) {
fieldWithObjectInput(input: $input)
}
`
params := map[string]interface{}{
"input": map[string]interface{}{
"a": nil,
"b": []string{"bar"},
"c": "baz",
},
}
expected := &graphql.Result{
Data: map[string]interface{}{
"fieldWithObjectInput": `{"a":null,"b":["bar"],"c":"baz"}`,
},
}

withDefaultsAST := testutil.TestParse(t, doc)

// execute
ep := graphql.ExecuteParams{
Schema: variablesTestSchema,
AST: withDefaultsAST,
Args: params,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

func TestVariables_ObjectsAndNullability_UsingVariables_UsesDefaultValueWhenNotProvided(t *testing.T) {

doc := `
Expand Down