From 4838c6f3bf4d198be8ebc81f5f39cf08bfd7c27b Mon Sep 17 00:00:00 2001 From: Richard Musiol Date: Sun, 20 Nov 2016 11:27:12 +0100 Subject: [PATCH] fix optional input fields (fixes #25) --- graphql_test.go | 28 +++++++++++++++++++++++++++- internal/exec/exec.go | 3 +++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/graphql_test.go b/graphql_test.go index ad5c99d70a..5241617875 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -764,7 +764,7 @@ func TestMutation(t *testing.T) { Variables: map[string]interface{}{ "ep": "JEDI", "review": map[string]interface{}{ - "stars": float64(5), + "stars": 5, "commentary": "This is a great movie!", }, }, @@ -778,6 +778,32 @@ func TestMutation(t *testing.T) { `, }, + { + Schema: starwarsSchema, + Query: ` + mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) { + createReview(episode: $ep, review: $review) { + stars + commentary + } + } + `, + Variables: map[string]interface{}{ + "ep": "EMPIRE", + "review": map[string]interface{}{ + "stars": float64(4), + }, + }, + ExpectedResult: ` + { + "createReview": { + "stars": 4, + "commentary": null + } + } + `, + }, + { Schema: starwarsSchema, Query: ` diff --git a/internal/exec/exec.go b/internal/exec/exec.go index c5d3b1fd7b..ed9daf75ab 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -690,6 +690,9 @@ func coerceMap(r *request, io *common.InputMap, m map[string]interface{}) (map[s for _, iv := range io.Fields { value, ok := m[iv.Name] if !ok { + if _, nonNull := unwrapNonNull(iv.Type); !nonNull { + continue + } if iv.Default == nil { return nil, errors.Errorf("missing %q", iv.Name) }