summarylogtreecommitdiffstats
path: root/0001-Fix-default-values-of-input-objects.patch
diff options
context:
space:
mode:
Diffstat (limited to '0001-Fix-default-values-of-input-objects.patch')
-rw-r--r--0001-Fix-default-values-of-input-objects.patch97
1 files changed, 97 insertions, 0 deletions
diff --git a/0001-Fix-default-values-of-input-objects.patch b/0001-Fix-default-values-of-input-objects.patch
new file mode 100644
index 000000000000..a56bab58422b
--- /dev/null
+++ b/0001-Fix-default-values-of-input-objects.patch
@@ -0,0 +1,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
+