Skip to content
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