1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
From 29075f857c0b49389c6c4193c8044e0673e45aa8 Mon Sep 17 00:00:00 2001
From: Sven Schneider <>
Date: Wed, 13 Apr 2016 15:38:45 +0200
Subject: [PATCH] Fix default values of input objects.
When a query did not specify a field in an input object this field was
ignored, even if a default value was set for it.
---
executor_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
values.go | 8 ++++++--
2 files changed, 56 insertions(+), 2 deletions(-)
diff --git a/executor_test.go b/executor_test.go
index 7922d8c..74911b2 100644
--- a/executor_test.go
+++ b/executor_test.go
@@ -1230,6 +1230,56 @@ func TestQuery_ExecutionDoesNotAddErrorsFromFieldResolveFn(t *testing.T) {
}
}
+func TestQuery_InputObjectUsesFieldDefaultValueFn(t *testing.T) {
+ inputType := graphql.NewInputObject(graphql.InputObjectConfig{
+ Name: "Input",
+ Fields: graphql.InputObjectConfigFieldMap{
+ "default": &graphql.InputObjectFieldConfig{
+ Type: graphql.String,
+ DefaultValue: "bar",
+ },
+ },
+ })
+ q := graphql.NewObject(graphql.ObjectConfig{
+ Name: "Query",
+ Fields: graphql.Fields{
+ "a": &graphql.Field{
+ Type: graphql.String,
+ Args: graphql.FieldConfigArgument{
+ "foo": &graphql.ArgumentConfig{
+ Type: graphql.NewNonNull(inputType),
+ },
+ },
+ Resolve: func(p graphql.ResolveParams) (interface{}, error) {
+ val := p.Args["foo"].(map[string]interface{})
+ def, ok := val["default"]
+ if !ok || def == nil {
+ return nil, errors.New("queryError: No 'default' param")
+ }
+ if def.(string) != "bar" {
+ return nil, errors.New("queryError: 'default' param has wrong value")
+ }
+ return "ok", nil
+ },
+ },
+ },
+ })
+ schema, err := graphql.NewSchema(graphql.SchemaConfig{
+ Query: q,
+ })
+ if err != nil {
+ t.Fatalf("unexpected error, got: %v", err)
+ }
+ query := `{ a(foo: {}) }`
+ result := graphql.Do(graphql.Params{
+ Schema: schema,
+ RequestString: query,
+ })
+ if len(result.Errors) != 0 {
+ t.Fatalf("wrong result, unexpected errors: %+v", result.Errors)
+ }
+}
+
func TestMutation_ExecutionAddsErrorsFromFieldResolveFn(t *testing.T) {
mError := errors.New("mutationError")
q := graphql.NewObject(graphql.ObjectConfig{
diff --git a/values.go b/values.go
index 6b3ff16..ada4407 100644
--- a/values.go
+++ b/values.go
@@ -364,10 +364,14 @@ func valueFromAST(valueAST ast.Value, ttype Input, variables map[string]interfac
obj := map[string]interface{}{}
for fieldName, field := range ttype.Fields() {
fieldAST, ok := fieldASTs[fieldName]
+ fieldValue := field.DefaultValue
if !ok || fieldAST == nil {
- continue
+ if fieldValue == nil {
+ continue
+ }
+ } else {
+ fieldValue = valueFromAST(fieldAST.Value, field.Type, variables)
}
- fieldValue := valueFromAST(fieldAST.Value, field.Type, variables)
if isNullish(fieldValue) {
fieldValue = field.DefaultValue
}
--
2.8.0
|