Skip to content

Commit

Permalink
fix: preserve null input arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza authored Nov 29, 2022
2 parents 09272f3 + dea0a49 commit 26b2739
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
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
36 changes: 36 additions & 0 deletions variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,42 @@ func TestVariables_ObjectsAndNullability_UsingVariables_UsesDefaultValueWhenNotP
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
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_ProperlyParsesSingleValueToList(t *testing.T) {
params := map[string]interface{}{
"input": map[string]interface{}{
Expand Down

0 comments on commit 26b2739

Please sign in to comment.