From 95b6f323a880d9034cacf048f0db0c783ca772e9 Mon Sep 17 00:00:00 2001 From: Cody Ley-Han Date: Wed, 22 Aug 2018 13:47:34 -0700 Subject: [PATCH 01/15] finds fields by json struct tag --- codegen/util.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/codegen/util.go b/codegen/util.go index fae94adeadd..4f6dc66b012 100644 --- a/codegen/util.go +++ b/codegen/util.go @@ -3,6 +3,7 @@ package codegen import ( "fmt" "go/types" + "reflect" "regexp" "strings" @@ -128,6 +129,14 @@ func findField(typ *types.Struct, name string) *types.Var { if strings.EqualFold(field.Name(), name) { return field } + + tags := reflect.StructTag(typ.Tag(i)) + + if val, ok := tags.Lookup("json"); ok { + if strings.EqualFold(val, name) { + return field + } + } } return nil } From c5849929105209bcdaba2f86ab65cb8b21d6190d Mon Sep 17 00:00:00 2001 From: Cody Ley-Han Date: Thu, 23 Aug 2018 18:16:58 -0700 Subject: [PATCH 02/15] adds binding by passed tag --- codegen/config.go | 1 + codegen/input_build.go | 2 +- codegen/object_build.go | 2 +- codegen/util.go | 61 +++++++++++++++++++++++++++++++---------- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/codegen/config.go b/codegen/config.go index 0ba1f65dabc..f5fc8abd348 100644 --- a/codegen/config.go +++ b/codegen/config.go @@ -64,6 +64,7 @@ type Config struct { Model PackageConfig `yaml:"model"` Resolver PackageConfig `yaml:"resolver,omitempty"` Models TypeMap `yaml:"models,omitempty"` + StructTag string `yaml:"struct_tag,omitempty"` FilePath string `yaml:"-"` diff --git a/codegen/input_build.go b/codegen/input_build.go index c333201522d..1059601a3c5 100644 --- a/codegen/input_build.go +++ b/codegen/input_build.go @@ -27,7 +27,7 @@ func (cfg *Config) buildInputs(namedTypes NamedTypes, prog *loader.Program, impo } if def != nil { input.Marshaler = buildInputMarshaler(typ, def) - bindErrs := bindObject(def.Type(), input, imports) + bindErrs := bindObject(def.Type(), input, imports, cfg.StructTag) if len(bindErrs) > 0 { return nil, bindErrs } diff --git a/codegen/object_build.go b/codegen/object_build.go index 95602a9138d..686b2bfe2a4 100644 --- a/codegen/object_build.go +++ b/codegen/object_build.go @@ -28,7 +28,7 @@ func (cfg *Config) buildObjects(types NamedTypes, prog *loader.Program, imports return nil, err } if def != nil { - for _, bindErr := range bindObject(def.Type(), obj, imports) { + for _, bindErr := range bindObject(def.Type(), obj, imports, cfg.StructTag) { log.Println(bindErr.Error()) log.Println(" Adding resolver method") } diff --git a/codegen/util.go b/codegen/util.go index fae94adeadd..0e880ec6601 100644 --- a/codegen/util.go +++ b/codegen/util.go @@ -3,6 +3,7 @@ package codegen import ( "fmt" "go/types" + "reflect" "regexp" "strings" @@ -104,19 +105,46 @@ func findMethod(typ *types.Named, name string) *types.Func { return nil } -func findField(typ *types.Struct, name string) *types.Var { +// findField attempts to match the name to a struct field with the following +// priorites: +// 1. If struct tag is passed then struct tag has highest priority +// 2. Field in an embedded struct +// 3. Actual Field name +func findField(typ *types.Struct, name, structTag string) (*types.Var, error) { + var foundField *types.Var + for i := 0; i < typ.NumFields(); i++ { field := typ.Field(i) + + if structTag != "" { + tags := reflect.StructTag(typ.Tag(i)) + if val, ok := tags.Lookup(structTag); ok { + if strings.EqualFold(val, name) { + if foundField != nil { + return nil, errors.Errorf("tag %s is ambigious; multiple fields have the same tag value of %s", structTag, val) + } + } + } + } + if field.Anonymous() { if named, ok := field.Type().(*types.Struct); ok { - if f := findField(named, name); f != nil { - return f + f, err := findField(named, name, structTag) + if err != nil { + return nil, err + } + if f != nil && foundField == nil { + foundField = f } } if named, ok := field.Type().Underlying().(*types.Struct); ok { - if f := findField(named, name); f != nil { - return f + f, err := findField(named, name, structTag) + if err != nil { + return nil, err + } + if f != nil && foundField == nil { + foundField = f } } } @@ -125,11 +153,16 @@ func findField(typ *types.Struct, name string) *types.Var { continue } - if strings.EqualFold(field.Name(), name) { - return field + if strings.EqualFold(field.Name(), name) && foundField == nil { + foundField = field } } - return nil + + if foundField == nil { + return nil, fmt.Errorf("no field named %s", name) + } + + return foundField, nil } type BindError struct { @@ -161,7 +194,7 @@ func (b BindErrors) Error() string { return strings.Join(errs, "\n\n") } -func bindObject(t types.Type, object *Object, imports *Imports) BindErrors { +func bindObject(t types.Type, object *Object, imports *Imports, structTag string) BindErrors { var errs BindErrors for i := range object.Fields { field := &object.Fields[i] @@ -177,7 +210,7 @@ func bindObject(t types.Type, object *Object, imports *Imports) BindErrors { } // otherwise try binding to a var - varErr := bindVar(imports, t, field) + varErr := bindVar(imports, t, field, structTag) if varErr != nil { errs = append(errs, BindError{ @@ -231,7 +264,7 @@ func bindMethod(imports *Imports, t types.Type, field *Field) error { return nil } -func bindVar(imports *Imports, t types.Type, field *Field) error { +func bindVar(imports *Imports, t types.Type, field *Field, structTag string) error { underlying, ok := t.Underlying().(*types.Struct) if !ok { return fmt.Errorf("not a struct") @@ -241,9 +274,9 @@ func bindVar(imports *Imports, t types.Type, field *Field) error { if field.GoFieldName != "" { goName = field.GoFieldName } - structField := findField(underlying, goName) - if structField == nil { - return fmt.Errorf("no field named %s", field.GQLName) + structField, err := findField(underlying, goName, structTag) + if err != nil { + return err } if err := validateTypeBinding(imports, field, structField.Type()); err != nil { From 7cfd9772cb10bb4cb75cef188c0c4635aecb8663 Mon Sep 17 00:00:00 2001 From: Cody Ley-Han Date: Thu, 23 Aug 2018 21:35:57 -0700 Subject: [PATCH 03/15] fixes field selection priority --- codegen/util.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/codegen/util.go b/codegen/util.go index 7be4ab8b6d6..a1681911013 100644 --- a/codegen/util.go +++ b/codegen/util.go @@ -112,6 +112,7 @@ func findMethod(typ *types.Named, name string) *types.Func { // 3. Actual Field name func findField(typ *types.Struct, name, structTag string) (*types.Var, error) { var foundField *types.Var + foundFieldWasTag := false for i := 0; i < typ.NumFields(); i++ { field := typ.Field(i) @@ -120,9 +121,12 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) { tags := reflect.StructTag(typ.Tag(i)) if val, ok := tags.Lookup(structTag); ok { if strings.EqualFold(val, name) { - if foundField != nil { + if foundField != nil && foundFieldWasTag { return nil, errors.Errorf("tag %s is ambigious; multiple fields have the same tag value of %s", structTag, val) } + + foundField = field + foundFieldWasTag = true } } } @@ -156,14 +160,6 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) { if strings.EqualFold(field.Name(), name) && foundField == nil { foundField = field } - - tags := reflect.StructTag(typ.Tag(i)) - - if val, ok := tags.Lookup("json"); ok { - if strings.EqualFold(val, name) { - return field - } - } } if foundField == nil { From 56768d6ba53088c47390944813b0c13cb97e4ae4 Mon Sep 17 00:00:00 2001 From: Cody Ley-Han Date: Thu, 23 Aug 2018 23:43:01 -0700 Subject: [PATCH 04/15] adds tests for findField --- codegen/util_test.go | 80 ++++++++++++++++++++++++++++++++++++++++++ docs/content/config.md | 3 ++ 2 files changed, 83 insertions(+) diff --git a/codegen/util_test.go b/codegen/util_test.go index cb41170ddb6..0c6935cc3d9 100644 --- a/codegen/util_test.go +++ b/codegen/util_test.go @@ -1,6 +1,11 @@ package codegen import ( + "go/ast" + "go/importer" + "go/parser" + "go/token" + "go/types" "testing" "github.com/stretchr/testify/require" @@ -12,3 +17,78 @@ func TestNormalizeVendor(t *testing.T) { require.Equal(t, "*bar/baz", normalizeVendor("*foo/vendor/bar/baz")) require.Equal(t, "*[]*bar/baz", normalizeVendor("*[]*foo/vendor/bar/baz")) } + +func TestFindField(t *testing.T) { + input := ` +package test + +type Std struct { + Name string + Value int +} +type Anon struct { + Name string + Tags +} +type Tags struct { + Bar string ` + "`" + `gqlgen:"foo"` + "`" + ` + Foo int ` + "`" + `gqlgen:"bar"` + "`" + ` +} +type Amb struct { + Bar string ` + "`" + `gqlgen:"foo"` + "`" + ` + Foo int ` + "`" + `gqlgen:"foo"` + "`" + ` +} +` + scope, err := parseScope(input, "test") + require.NoError(t, err) + + std := scope.Lookup("Std").Type().Underlying().(*types.Struct) + anon := scope.Lookup("Anon").Type().Underlying().(*types.Struct) + tags := scope.Lookup("Tags").Type().Underlying().(*types.Struct) + amb := scope.Lookup("Amb").Type().Underlying().(*types.Struct) + + tests := []struct { + Name string + Struct *types.Struct + Field string + Tag string + Expected string + ShouldError bool + }{ + {"Finds a field by name with no tag", std, "name", "", "Name", false}, + {"Finds a field by name when passed tag but tag not used", std, "name", "gqlgen", "Name", false}, + {"Ignores tags when not passed a tag", tags, "foo", "", "Foo", false}, + {"Picks field with tag over field name when passed a tag", tags, "foo", "gqlgen", "Bar", false}, + {"Errors when ambigious", amb, "foo", "gqlgen", "", true}, + {"Finds a field that is in embedded struct", anon, "bar", "", "Bar", false}, + } + + for _, tt := range tests { + tt := tt + field, err := findField(tt.Struct, tt.Field, tt.Tag) + if tt.ShouldError { + require.Nil(t, field, tt.Name) + require.Error(t, err, tt.Name) + } else { + require.NoError(t, err, tt.Name) + require.Equal(t, tt.Expected, field.Name(), tt.Name) + } + } +} + +func parseScope(input interface{}, packageName string) (*types.Scope, error) { + // test setup to parse the types + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, "test.go", input, 0) + if err != nil { + return nil, err + } + + conf := types.Config{Importer: importer.Default()} + pkg, err := conf.Check(packageName, fset, []*ast.File{f}, nil) + if err != nil { + return nil, err + } + + return pkg.Scope(), nil +} diff --git a/docs/content/config.md b/docs/content/config.md index 8415fa37684..c6052ee9edc 100644 --- a/docs/content/config.md +++ b/docs/content/config.md @@ -27,6 +27,9 @@ resolver: filename: resolver.go # where to write them type: Resolver # whats the resolver root implementation type called? +# Optional, turns on binding to field names by tag provided +struct_tag: json + # Tell gqlgen about any existing models you want to reuse for # graphql. These normally come from the db or a remote api. models: From 02873495e1f85ae83e38cf79e52a2122a845986f Mon Sep 17 00:00:00 2001 From: vvakame Date: Fri, 24 Aug 2018 16:23:58 +0900 Subject: [PATCH 05/15] use goroutine about processing each array elements --- codegen/object.go | 31 +- codegen/testserver/generated.go | 335 ++++++++++++++-------- example/chat/generated.go | 267 +++++++++++------- example/config/generated.go | 267 +++++++++++------- example/dataloader/generated.go | 396 ++++++++++++++++---------- example/scalars/generated.go | 267 +++++++++++------- example/selection/generated.go | 335 ++++++++++++++-------- example/starwars/generated.go | 485 +++++++++++++++++++++----------- example/todo/generated.go | 267 +++++++++++------- integration/generated.go | 307 +++++++++++++------- 10 files changed, 1910 insertions(+), 1047 deletions(-) diff --git a/codegen/object.go b/codegen/object.go index 93d1952102f..fc0669b4ad1 100644 --- a/codegen/object.go +++ b/codegen/object.go @@ -212,28 +212,37 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ } var arr = "arr" + strconv.Itoa(depth) var index = "idx" + strconv.Itoa(depth) + var wg = "wg" + strconv.Itoa(depth) var usePtr bool if len(remainingMods) == 1 && !isPtr { usePtr = true } - return tpl(`{{.arr}} := graphql.Array{} + return tpl(` + {{.arr}} := make(graphql.Array, len({{.val}})) + var {{.wg}} sync.WaitGroup + {{.wg}}.Add(len({{.val}})) for {{.index}} := range {{.val}} { - {{- if not .isScalar }} - rctx := &graphql.ResolverContext{ - Index: &{{.index}}, - Result: {{ if .usePtr }}&{{end}}{{.val}}[{{.index}}], - } - ctx := graphql.WithResolverContext(ctx, rctx) - {{- end}} - {{.arr}} = append({{.arr}}, func() graphql.Marshaler { - {{ .next }} - }()) + go func({{.index}} int) { + defer {{.wg}}.Done() + {{- if not .isScalar }} + rctx := &graphql.ResolverContext{ + Index: &{{.index}}, + Result: {{ if .usePtr }}&{{end}}{{.val}}[{{.index}}], + } + ctx := graphql.WithResolverContext(ctx, rctx) + {{- end}} + {{.arr}}[{{.index}}] = func() graphql.Marshaler { + {{ .next }} + }() + }({{.index}}) } + {{.wg}}.Wait() return {{.arr}}`, map[string]interface{}{ "val": val, "arr": arr, "index": index, + "wg": wg, "isScalar": f.IsScalar, "usePtr": usePtr, "next": f.doWriteJson(val+"["+index+"]", remainingMods[1:], astType.Elem, false, depth+1), diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 3402ee3dcef..9c423de3274 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -872,33 +872,47 @@ func (ec *executionContext) _Query_nestedOutputs(ctx context.Context, field grap } res := resTmp.([][]*OuterObject) rctx.Result = res - arr1 := graphql.Array{} - for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { - arr2 := graphql.Array{} - for idx2 := range res[idx1] { - rctx := &graphql.ResolverContext{ - Index: &idx2, - Result: res[idx1][idx2], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr2 = append(arr2, func() graphql.Marshaler { - - if res[idx1][idx2] == nil { - return graphql.Null - } - return ec._OuterObject(ctx, field.Selections, res[idx1][idx2]) - }()) + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) + for idx1 := range res { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: res[idx1], } - return arr2 - }()) + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { + + arr2 := make(graphql.Array, len(res[idx1])) + var wg2 sync.WaitGroup + wg2.Add(len(res[idx1])) + for idx2 := range res[idx1] { + go func(idx2 int) { + defer wg2.Done() + rctx := &graphql.ResolverContext{ + Index: &idx2, + Result: res[idx1][idx2], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr2[idx2] = func() graphql.Marshaler { + + if res[idx1][idx2] == nil { + return graphql.Null + } + + return ec._OuterObject(ctx, field.Selections, res[idx1][idx2]) + }() + }(idx2) + } + wg2.Wait() + return arr2 + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -957,22 +971,29 @@ func (ec *executionContext) _Query_shapes(ctx context.Context, field graphql.Col } res := resTmp.([]*Shape) rctx.Result = res - arr1 := graphql.Array{} - for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { - if res[idx1] == nil { - return graphql.Null + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) + for idx1 := range res { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: res[idx1], } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { + + if res[idx1] == nil { + return graphql.Null + } - return ec._Shape(ctx, field.Selections, res[idx1]) - }()) + return ec._Shape(ctx, field.Selections, res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1546,12 +1567,19 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -1574,18 +1602,25 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1819,18 +1854,25 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2101,18 +2143,25 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2213,18 +2262,25 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap } res := resTmp.([]introspection.Directive) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Directive(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Directive(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2366,18 +2422,25 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co } res := resTmp.([]introspection.Field) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Field(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Field(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2397,18 +2460,25 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2428,18 +2498,25 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2471,18 +2548,25 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq } res := resTmp.([]introspection.EnumValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___EnumValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2502,18 +2586,25 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } diff --git a/example/chat/generated.go b/example/chat/generated.go index b9be4e6d379..2c840e3fe6f 100644 --- a/example/chat/generated.go +++ b/example/chat/generated.go @@ -196,18 +196,25 @@ func (ec *executionContext) _Chatroom_messages(ctx context.Context, field graphq } res := resTmp.([]Message) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Message(ctx, field.Selections, &res[idx1]) - }()) + return ec._Message(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -726,12 +733,19 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -754,18 +768,25 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -999,18 +1020,25 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1281,18 +1309,25 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1393,18 +1428,25 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap } res := resTmp.([]introspection.Directive) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Directive(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Directive(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1546,18 +1588,25 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co } res := resTmp.([]introspection.Field) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Field(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Field(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1577,18 +1626,25 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1608,18 +1664,25 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1651,18 +1714,25 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq } res := resTmp.([]introspection.EnumValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___EnumValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1682,18 +1752,25 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } diff --git a/example/config/generated.go b/example/config/generated.go index fa61d1c9894..60bd6d0387f 100644 --- a/example/config/generated.go +++ b/example/config/generated.go @@ -226,18 +226,25 @@ func (ec *executionContext) _Query_todos(ctx context.Context, field graphql.Coll } res := resTmp.([]Todo) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Todo(ctx, field.Selections, &res[idx1]) - }()) + return ec._Todo(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -648,12 +655,19 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -676,18 +690,25 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -921,18 +942,25 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1203,18 +1231,25 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1315,18 +1350,25 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap } res := resTmp.([]introspection.Directive) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Directive(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Directive(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1468,18 +1510,25 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co } res := resTmp.([]introspection.Field) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Field(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Field(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1499,18 +1548,25 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1530,18 +1586,25 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1573,18 +1636,25 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq } res := resTmp.([]introspection.EnumValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___EnumValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1604,18 +1674,25 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } diff --git a/example/dataloader/generated.go b/example/dataloader/generated.go index 278d5f7285c..c3718bd5770 100644 --- a/example/dataloader/generated.go +++ b/example/dataloader/generated.go @@ -325,18 +325,25 @@ func (ec *executionContext) _Customer_orders(ctx context.Context, field graphql. } res := resTmp.([]Order) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Order(ctx, field.Selections, &res[idx1]) - }()) + return ec._Order(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -521,18 +528,25 @@ func (ec *executionContext) _Order_items(ctx context.Context, field graphql.Coll } res := resTmp.([]Item) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Item(ctx, field.Selections, &res[idx1]) - }()) + return ec._Item(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -604,18 +618,25 @@ func (ec *executionContext) _Query_customers(ctx context.Context, field graphql. } res := resTmp.([]Customer) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Customer(ctx, field.Selections, &res[idx1]) - }()) + return ec._Customer(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -658,18 +679,25 @@ func (ec *executionContext) _Query_torture1d(ctx context.Context, field graphql. } res := resTmp.([]Customer) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Customer(ctx, field.Selections, &res[idx1]) - }()) + return ec._Customer(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -723,29 +751,43 @@ func (ec *executionContext) _Query_torture2d(ctx context.Context, field graphql. } res := resTmp.([][]Customer) rctx.Result = res - arr1 := graphql.Array{} - for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { - arr2 := graphql.Array{} - for idx2 := range res[idx1] { - rctx := &graphql.ResolverContext{ - Index: &idx2, - Result: &res[idx1][idx2], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr2 = append(arr2, func() graphql.Marshaler { - return ec._Customer(ctx, field.Selections, &res[idx1][idx2]) - }()) + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) + for idx1 := range res { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: res[idx1], } - return arr2 - }()) + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { + + arr2 := make(graphql.Array, len(res[idx1])) + var wg2 sync.WaitGroup + wg2.Add(len(res[idx1])) + for idx2 := range res[idx1] { + go func(idx2 int) { + defer wg2.Done() + rctx := &graphql.ResolverContext{ + Index: &idx2, + Result: &res[idx1][idx2], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr2[idx2] = func() graphql.Marshaler { + + return ec._Customer(ctx, field.Selections, &res[idx1][idx2]) + }() + }(idx2) + } + wg2.Wait() + return arr2 + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -911,12 +953,19 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -939,18 +988,25 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1184,18 +1240,25 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1466,18 +1529,25 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1578,18 +1648,25 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap } res := resTmp.([]introspection.Directive) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Directive(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Directive(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1731,18 +1808,25 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co } res := resTmp.([]introspection.Field) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Field(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Field(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1762,18 +1846,25 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1793,18 +1884,25 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1836,18 +1934,25 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq } res := resTmp.([]introspection.EnumValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___EnumValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1867,18 +1972,25 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } diff --git a/example/scalars/generated.go b/example/scalars/generated.go index b10520bbe00..9d60f27e8c0 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -277,18 +277,25 @@ func (ec *executionContext) _Query_search(ctx context.Context, field graphql.Col } res := resTmp.([]model.User) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._User(ctx, field.Selections, &res[idx1]) - }()) + return ec._User(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -687,12 +694,19 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -715,18 +729,25 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -960,18 +981,25 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1242,18 +1270,25 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1354,18 +1389,25 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap } res := resTmp.([]introspection.Directive) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Directive(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Directive(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1507,18 +1549,25 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co } res := resTmp.([]introspection.Field) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Field(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Field(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1538,18 +1587,25 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1569,18 +1625,25 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1612,18 +1675,25 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq } res := resTmp.([]introspection.EnumValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___EnumValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1643,18 +1713,25 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } diff --git a/example/selection/generated.go b/example/selection/generated.go index ceaa5d93c66..6d127531080 100644 --- a/example/selection/generated.go +++ b/example/selection/generated.go @@ -176,12 +176,19 @@ func (ec *executionContext) _Like_selection(ctx context.Context, field graphql.C } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -201,12 +208,19 @@ func (ec *executionContext) _Like_collected(ctx context.Context, field graphql.C } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -309,12 +323,19 @@ func (ec *executionContext) _Post_selection(ctx context.Context, field graphql.C } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -334,12 +355,19 @@ func (ec *executionContext) _Post_collected(ctx context.Context, field graphql.C } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -399,18 +427,25 @@ func (ec *executionContext) _Query_events(ctx context.Context, field graphql.Col } res := resTmp.([]Event) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Event(ctx, field.Selections, &res[idx1]) - }()) + return ec._Event(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -576,12 +611,19 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -604,18 +646,25 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -849,18 +898,25 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1131,18 +1187,25 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1243,18 +1306,25 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap } res := resTmp.([]introspection.Directive) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Directive(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Directive(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1396,18 +1466,25 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co } res := resTmp.([]introspection.Field) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Field(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Field(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1427,18 +1504,25 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1458,18 +1542,25 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1501,18 +1592,25 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq } res := resTmp.([]introspection.EnumValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___EnumValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1532,18 +1630,25 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } diff --git a/example/starwars/generated.go b/example/starwars/generated.go index 8237b1371da..c38bc0db7f0 100644 --- a/example/starwars/generated.go +++ b/example/starwars/generated.go @@ -238,18 +238,25 @@ func (ec *executionContext) _Droid_friends(ctx context.Context, field graphql.Co } res := resTmp.([]Character) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Character(ctx, field.Selections, &res[idx1]) - }()) + return ec._Character(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -327,12 +334,19 @@ func (ec *executionContext) _Droid_appearsIn(ctx context.Context, field graphql. } res := resTmp.([]Episode) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return res[idx1] - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return res[idx1] + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -441,18 +455,25 @@ func (ec *executionContext) _FriendsConnection_edges(ctx context.Context, field } res := resTmp.([]FriendsEdge) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._FriendsEdge(ctx, field.Selections, &res[idx1]) - }()) + return ec._FriendsEdge(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -472,18 +493,25 @@ func (ec *executionContext) _FriendsConnection_friends(ctx context.Context, fiel } res := resTmp.([]Character) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Character(ctx, field.Selections, &res[idx1]) - }()) + return ec._Character(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -766,18 +794,25 @@ func (ec *executionContext) _Human_friends(ctx context.Context, field graphql.Co } res := resTmp.([]Character) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Character(ctx, field.Selections, &res[idx1]) - }()) + return ec._Character(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -855,12 +890,19 @@ func (ec *executionContext) _Human_appearsIn(ctx context.Context, field graphql. } res := resTmp.([]Episode) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return res[idx1] - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return res[idx1] + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -880,18 +922,25 @@ func (ec *executionContext) _Human_starships(ctx context.Context, field graphql. } res := resTmp.([]Starship) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Starship(ctx, field.Selections, &res[idx1]) - }()) + return ec._Starship(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1238,18 +1287,25 @@ func (ec *executionContext) _Query_reviews(ctx context.Context, field graphql.Co } res := resTmp.([]Review) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Review(ctx, field.Selections, &res[idx1]) - }()) + return ec._Review(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1284,18 +1340,25 @@ func (ec *executionContext) _Query_search(ctx context.Context, field graphql.Col } res := resTmp.([]SearchResult) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._SearchResult(ctx, field.Selections, &res[idx1]) - }()) + return ec._SearchResult(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1744,18 +1807,32 @@ func (ec *executionContext) _Starship_history(ctx context.Context, field graphql } res := resTmp.([][]int) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - arr2 := graphql.Array{} - for idx2 := range res[idx1] { - arr2 = append(arr2, func() graphql.Marshaler { - return graphql.MarshalInt(res[idx1][idx2]) - }()) - } - return arr2 - }()) + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + + arr2 := make(graphql.Array, len(res[idx1])) + var wg2 sync.WaitGroup + wg2.Add(len(res[idx1])) + for idx2 := range res[idx1] { + go func(idx2 int) { + defer wg2.Done() + arr2[idx2] = func() graphql.Marshaler { + return graphql.MarshalInt(res[idx1][idx2]) + }() + }(idx2) + } + wg2.Wait() + return arr2 + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1861,12 +1938,19 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -1889,18 +1973,25 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2134,18 +2225,25 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2416,18 +2514,25 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2528,18 +2633,25 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap } res := resTmp.([]introspection.Directive) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Directive(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Directive(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2681,18 +2793,25 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co } res := resTmp.([]introspection.Field) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Field(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Field(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2712,18 +2831,25 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2743,18 +2869,25 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2786,18 +2919,25 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq } res := resTmp.([]introspection.EnumValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___EnumValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -2817,18 +2957,25 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } diff --git a/example/todo/generated.go b/example/todo/generated.go index 13164a1e6e9..ebe2a66a04a 100644 --- a/example/todo/generated.go +++ b/example/todo/generated.go @@ -346,18 +346,25 @@ func (ec *executionContext) _MyQuery_todos(ctx context.Context, field graphql.Co } res := resTmp.([]Todo) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Todo(ctx, field.Selections, &res[idx1]) - }()) + return ec._Todo(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -629,12 +636,19 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -657,18 +671,25 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -902,18 +923,25 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1184,18 +1212,25 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1296,18 +1331,25 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap } res := resTmp.([]introspection.Directive) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Directive(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Directive(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1449,18 +1491,25 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co } res := resTmp.([]introspection.Field) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Field(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Field(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1480,18 +1529,25 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1511,18 +1567,25 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1554,18 +1617,25 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq } res := resTmp.([]introspection.EnumValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___EnumValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1585,18 +1655,25 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } diff --git a/integration/generated.go b/integration/generated.go index 1bbad672438..21f86a3cf4b 100644 --- a/integration/generated.go +++ b/integration/generated.go @@ -203,12 +203,19 @@ func (ec *executionContext) _Element_mismatched(ctx context.Context, field graph } res := resTmp.([]bool) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalBoolean(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalBoolean(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -301,22 +308,29 @@ func (ec *executionContext) _Query_path(ctx context.Context, field graphql.Colle } res := resTmp.([]*models.Element) rctx.Result = res - arr1 := graphql.Array{} - for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { - if res[idx1] == nil { - return graphql.Null + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) + for idx1 := range res { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: res[idx1], } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec._Element(ctx, field.Selections, res[idx1]) - }()) + if res[idx1] == nil { + return graphql.Null + } + + return ec._Element(ctx, field.Selections, res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -575,12 +589,19 @@ func (ec *executionContext) _User_likes(ctx context.Context, field graphql.Colle } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -737,12 +758,19 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr } res := resTmp.([]string) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - arr1 = append(arr1, func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }()) - } + go func(idx1 int) { + defer wg1.Done() + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() + }(idx1) + } + wg1.Wait() return arr1 } @@ -765,18 +793,25 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1010,18 +1045,25 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1292,18 +1334,25 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1404,18 +1453,25 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap } res := resTmp.([]introspection.Directive) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Directive(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Directive(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1557,18 +1613,25 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co } res := resTmp.([]introspection.Field) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Field(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Field(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1588,18 +1651,25 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1619,18 +1689,25 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra } res := resTmp.([]introspection.Type) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___Type(ctx, field.Selections, &res[idx1]) - }()) + return ec.___Type(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1662,18 +1739,25 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq } res := resTmp.([]introspection.EnumValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___EnumValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } @@ -1693,18 +1777,25 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph } res := resTmp.([]introspection.InputValue) rctx.Result = res - arr1 := graphql.Array{} + + arr1 := make(graphql.Array, len(res)) + var wg1 sync.WaitGroup + wg1.Add(len(res)) for idx1 := range res { - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], - } - ctx := graphql.WithResolverContext(ctx, rctx) - arr1 = append(arr1, func() graphql.Marshaler { + go func(idx1 int) { + defer wg1.Done() + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + arr1[idx1] = func() graphql.Marshaler { - return ec.___InputValue(ctx, field.Selections, &res[idx1]) - }()) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) + }() + }(idx1) } + wg1.Wait() return arr1 } From 7d6f8ed4b4d5e64eb98dca853aa58fef9aee8784 Mon Sep 17 00:00:00 2001 From: Cody Ley-Han Date: Fri, 24 Aug 2018 14:28:40 -0700 Subject: [PATCH 06/15] fixes case where embeded structs would cause no field to be found --- codegen/util.go | 4 ++-- codegen/util_test.go | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/codegen/util.go b/codegen/util.go index a1681911013..1849f100bb1 100644 --- a/codegen/util.go +++ b/codegen/util.go @@ -134,7 +134,7 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) { if field.Anonymous() { if named, ok := field.Type().(*types.Struct); ok { f, err := findField(named, name, structTag) - if err != nil { + if err != nil && !strings.HasPrefix(err.Error(), "no field named") { return nil, err } if f != nil && foundField == nil { @@ -144,7 +144,7 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) { if named, ok := field.Type().Underlying().(*types.Struct); ok { f, err := findField(named, name, structTag) - if err != nil { + if err != nil && !strings.HasPrefix(err.Error(), "no field named") { return nil, err } if f != nil && foundField == nil { diff --git a/codegen/util_test.go b/codegen/util_test.go index 0c6935cc3d9..aedfda04694 100644 --- a/codegen/util_test.go +++ b/codegen/util_test.go @@ -38,6 +38,10 @@ type Amb struct { Bar string ` + "`" + `gqlgen:"foo"` + "`" + ` Foo int ` + "`" + `gqlgen:"foo"` + "`" + ` } +type Embed struct { + Std + Test string +} ` scope, err := parseScope(input, "test") require.NoError(t, err) @@ -46,6 +50,7 @@ type Amb struct { anon := scope.Lookup("Anon").Type().Underlying().(*types.Struct) tags := scope.Lookup("Tags").Type().Underlying().(*types.Struct) amb := scope.Lookup("Amb").Type().Underlying().(*types.Struct) + embed := scope.Lookup("Embed").Type().Underlying().(*types.Struct) tests := []struct { Name string @@ -61,6 +66,7 @@ type Amb struct { {"Picks field with tag over field name when passed a tag", tags, "foo", "gqlgen", "Bar", false}, {"Errors when ambigious", amb, "foo", "gqlgen", "", true}, {"Finds a field that is in embedded struct", anon, "bar", "", "Bar", false}, + {"Finds field that is not in embedded struct", embed, "test", "", "Test", false}, } for _, tt := range tests { From 6ae82383f52b2a6c479f982d04215ddfda28f806 Mon Sep 17 00:00:00 2001 From: Andri Oskarsson Date: Sun, 26 Aug 2018 10:19:00 +0200 Subject: [PATCH 07/15] trying to get description with generated models --- codegen/enum.go | 4 ++-- codegen/enum_build.go | 5 +++-- codegen/model.go | 5 +++-- codegen/models_build.go | 1 + codegen/templates/data.go | 2 +- codegen/templates/models.gotpl | 9 +++++++-- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/codegen/enum.go b/codegen/enum.go index e62fd2b175a..7804971c02a 100644 --- a/codegen/enum.go +++ b/codegen/enum.go @@ -2,8 +2,8 @@ package codegen type Enum struct { *NamedType - - Values []EnumValue + Description string + Values []EnumValue } type EnumValue struct { diff --git a/codegen/enum_build.go b/codegen/enum_build.go index d5e82c24764..55cb957bdc3 100644 --- a/codegen/enum_build.go +++ b/codegen/enum_build.go @@ -23,8 +23,9 @@ func (cfg *Config) buildEnums(types NamedTypes) []Enum { } enum := Enum{ - NamedType: namedType, - Values: values, + NamedType: namedType, + Values: values, + Description: typ.Description, } enum.GoType = templates.ToCamel(enum.GQLType) enums = append(enums, enum) diff --git a/codegen/model.go b/codegen/model.go index 7b46a318c2b..5ba50337e27 100644 --- a/codegen/model.go +++ b/codegen/model.go @@ -2,8 +2,8 @@ package codegen type Model struct { *NamedType - - Fields []ModelField + Description string + Fields []ModelField } type ModelField struct { @@ -12,4 +12,5 @@ type ModelField struct { GoFieldName string GoFKName string GoFKType string + Description string } diff --git a/codegen/models_build.go b/codegen/models_build.go index 6b6ddb6fb61..b57eafff5c3 100644 --- a/codegen/models_build.go +++ b/codegen/models_build.go @@ -41,6 +41,7 @@ func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program, imports * default: continue } + model.Description = typ.Description // It's this or change both obj2Model and buildObject models = append(models, model) } diff --git a/codegen/templates/data.go b/codegen/templates/data.go index 3625fe4247b..3eda08b9f5b 100644 --- a/codegen/templates/data.go +++ b/codegen/templates/data.go @@ -6,7 +6,7 @@ var data = map[string]string{ "generated.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.\nfunc NewExecutableSchema(cfg Config) graphql.ExecutableSchema {\n\treturn &executableSchema{\n\t\tresolvers: cfg.Resolvers,\n\t\tdirectives: cfg.Directives,\n\t}\n}\n\ntype Config struct {\n\tResolvers ResolverRoot\n\tDirectives DirectiveRoot\n}\n\ntype ResolverRoot interface {\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers -}}\n\t\t{{$object.GQLType}}() {{$object.GQLType}}Resolver\n\t{{ end }}\n{{- end }}\n}\n\ntype DirectiveRoot struct {\n{{ range $directive := .Directives }}\n\t{{ $directive.Declaration }}\n{{ end }}\n}\n\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers }}\n\t\ttype {{$object.GQLType}}Resolver interface {\n\t\t{{ range $field := $object.Fields -}}\n\t\t\t{{ $field.ShortResolverDeclaration }}\n\t\t{{ end }}\n\t\t}\n\t{{- end }}\n{{- end }}\n\ntype executableSchema struct {\n\tresolvers ResolverRoot\n\tdirectives DirectiveRoot\n}\n\nfunc (e *executableSchema) Schema() *ast.Schema {\n\treturn parsedSchema\n}\n\nfunc (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {\n\t{{- if .QueryRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.QueryRoot.GQLType}}(ctx, op.SelectionSet)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"queries are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {\n\t{{- if .MutationRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.MutationRoot.GQLType}}(ctx, op.SelectionSet)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"mutations are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response {\n\t{{- if .SubscriptionRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tnext := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.SelectionSet)\n\t\tif ec.Errors != nil {\n\t\t\treturn graphql.OneShot(&graphql.Response{Data: []byte(\"null\"), Errors: ec.Errors})\n\t\t}\n\n\t\tvar buf bytes.Buffer\n\t\treturn func() *graphql.Response {\n\t\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\t\tbuf.Reset()\n\t\t\t\tdata := next()\n\n\t\t\t\tif data == nil {\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\tdata.MarshalGQL(&buf)\n\t\t\t\treturn buf.Bytes()\n\t\t\t})\n\n\t\t\treturn &graphql.Response{\n\t\t\t\tData: buf,\n\t\t\t\tErrors: ec.Errors,\n\t\t\t}\n\t\t}\n\t{{- else }}\n\t\treturn graphql.OneShot(graphql.ErrorResponse(ctx, \"subscriptions are not supported\"))\n\t{{- end }}\n}\n\ntype executionContext struct {\n\t*graphql.RequestContext\n\t*executableSchema\n}\n\n{{- range $object := .Objects }}\n\t{{ template \"object.gotpl\" $object }}\n\n\t{{- range $field := $object.Fields }}\n\t\t{{ template \"field.gotpl\" $field }}\n\t{{ end }}\n{{- end}}\n\n{{- range $interface := .Interfaces }}\n\t{{ template \"interface.gotpl\" $interface }}\n{{- end }}\n\n{{- range $input := .Inputs }}\n\t{{ template \"input.gotpl\" $input }}\n{{- end }}\n\nfunc (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tec.Error(ctx, ec.Recover(ctx, r))\n\t\t\tret = nil\n\t\t}\n\t}()\n\t{{- if .Directives }}\n\trctx := graphql.GetResolverContext(ctx)\n\tfor _, d := range rctx.Field.Definition.Directives {\n\t\tswitch d.Name {\n\t\t{{- range $directive := .Directives }}\n\t\tcase \"{{$directive.Name}}\":\n\t\t\tif ec.directives.{{$directive.Name|ucFirst}} != nil {\n\t\t\t\t{{- if $directive.Args }}\n\t\t\t\t\trawArgs := d.ArgumentMap(ec.Variables)\n\t\t\t\t\t{{ template \"args.gotpl\" $directive.Args }}\n\t\t\t\t{{- end }}\n\t\t\t\tn := next\n\t\t\t\tnext = func(ctx context.Context) (interface{}, error) {\n\t\t\t\t\treturn ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})\n\t\t\t\t}\n\t\t\t}\n\t\t{{- end }}\n\t\t}\n\t}\n\t{{- end }}\n\tres, err := ec.ResolverMiddleware(ctx, next)\n\tif err != nil {\n\t\tec.Error(ctx, err)\n\t\treturn nil\n\t}\n\treturn res\n}\n\nfunc (ec *executionContext) introspectSchema() *introspection.Schema {\n\treturn introspection.WrapSchema(parsedSchema)\n}\n\nfunc (ec *executionContext) introspectType(name string) *introspection.Type {\n\treturn introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name])\n}\n\nvar parsedSchema = gqlparser.MustLoadSchema(\n\t&ast.Source{Name: {{.SchemaFilename|quote}}, Input: {{.SchemaRaw|rawQuote}}},\n)\n", "input.gotpl": "\t{{- if .IsMarshaled }}\n\tfunc Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) {\n\t\tvar it {{.FullName}}\n\t\tvar asMap = v.(map[string]interface{})\n\t\t{{ range $field := .Fields}}\n\t\t\t{{- if $field.Default}}\n\t\t\t\tif _, present := asMap[{{$field.GQLName|quote}}] ; !present {\n\t\t\t\t\tasMap[{{$field.GQLName|quote}}] = {{ $field.Default | dump }}\n\t\t\t\t}\n\t\t\t{{- end}}\n\t\t{{- end }}\n\n\t\tfor k, v := range asMap {\n\t\t\tswitch k {\n\t\t\t{{- range $field := .Fields }}\n\t\t\tcase {{$field.GQLName|quote}}:\n\t\t\t\tvar err error\n\t\t\t\t{{ $field.Unmarshal (print \"it.\" $field.GoFieldName) \"v\" }}\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn it, err\n\t\t\t\t}\n\t\t\t{{- end }}\n\t\t\t}\n\t\t}\n\n\t\treturn it, nil\n\t}\n\t{{- end }}\n", "interface.gotpl": "{{- $interface := . }}\n\nfunc (ec *executionContext) _{{$interface.GQLType}}(ctx context.Context, sel ast.SelectionSet, obj *{{$interface.FullName}}) graphql.Marshaler {\n\tswitch obj := (*obj).(type) {\n\tcase nil:\n\t\treturn graphql.Null\n\t{{- range $implementor := $interface.Implementors }}\n\t\t{{- if $implementor.ValueReceiver }}\n\t\t\tcase {{$implementor.FullName}}:\n\t\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, &obj)\n\t\t{{- end}}\n\t\tcase *{{$implementor.FullName}}:\n\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, obj)\n\t{{- end }}\n\tdefault:\n\t\tpanic(fmt.Errorf(\"unexpected type %T\", obj))\n\t}\n}\n", - "models.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n{{ range $model := .Models }}\n\t{{- if .IsInterface }}\n\t\ttype {{.GoType}} interface {}\n\t{{- else }}\n\t\ttype {{.GoType}} struct {\n\t\t\t{{- range $field := .Fields }}\n\t\t\t\t{{- if $field.GoFieldName }}\n\t\t\t\t\t{{ $field.GoFieldName }} {{$field.Signature}} `json:\"{{$field.GQLName}}\"`\n\t\t\t\t{{- else }}\n\t\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t}\n\t{{- end }}\n{{- end}}\n\n{{ range $enum := .Enums }}\n\ttype {{.GoType}} string\n\tconst (\n\t{{ range $value := .Values -}}\n\t\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\t\t{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}\n\t{{- end }}\n\t)\n\n\tfunc (e {{.GoType}}) IsValid() bool {\n\t\tswitch e {\n\t\tcase {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tfunc (e {{.GoType}}) String() string {\n\t\treturn string(e)\n\t}\n\n\tfunc (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {\n\t\tstr, ok := v.(string)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"enums must be strings\")\n\t\t}\n\n\t\t*e = {{.GoType}}(str)\n\t\tif !e.IsValid() {\n\t\t\treturn fmt.Errorf(\"%s is not a valid {{.GQLType}}\", str)\n\t\t}\n\t\treturn nil\n\t}\n\n\tfunc (e {{.GoType}}) MarshalGQL(w io.Writer) {\n\t\tfmt.Fprint(w, strconv.Quote(e.String()))\n\t}\n\n{{- end }}\n", + "models.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n// Dirt\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n{{ range $model := .Models }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\n\t{{- if .IsInterface }}\n\t\ttype {{.GoType}} interface {}\n\t{{- else }}\n\t\ttype {{.GoType}} struct {\n\n\t\t\t{{- range $field := .Fields }}\n\t\t\t\t{{- if $field.GoFieldName }}\n\t\t\t\t\t{{ $field.GoFieldName }} {{$field.Signature}} `json:\"{{$field.GQLName}}\"`\n\t\t\t\t{{- else }}\n\t\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t}\n\t{{- end }}\n{{- end}}\n\n{{ range $enum := .Enums }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\ttype {{.GoType}} string\n\tconst (\n\t{{ range $value := .Values}}\n\t\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\t\t{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}\n\t{{ end }}\n\t)\n\n\tfunc (e {{.GoType}}) IsValid() bool {\n\t\tswitch e {\n\t\tcase {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tfunc (e {{.GoType}}) String() string {\n\t\treturn string(e)\n\t}\n\n\tfunc (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {\n\t\tstr, ok := v.(string)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"enums must be strings\")\n\t\t}\n\n\t\t*e = {{.GoType}}(str)\n\t\tif !e.IsValid() {\n\t\t\treturn fmt.Errorf(\"%s is not a valid {{.GQLType}}\", str)\n\t\t}\n\t\treturn nil\n\t}\n\n\tfunc (e {{.GoType}}) MarshalGQL(w io.Writer) {\n\t\tfmt.Fprint(w, strconv.Quote(e.String()))\n\t}\n\n{{- end }}\n", "object.gotpl": "{{ $object := . }}\n\nvar {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}}\n\n// nolint: gocyclo, errcheck, gas, goconst\n{{- if .Stream }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet) func() graphql.Marshaler {\n\tfields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)\n\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\tObject: {{$object.GQLType|quote}},\n\t})\n\tif len(fields) != 1 {\n\t\tec.Errorf(ctx, \"must subscribe to exactly one stream\")\n\t\treturn nil\n\t}\n\n\tswitch fields[0].Name {\n\t{{- range $field := $object.Fields }}\n\tcase \"{{$field.GQLName}}\":\n\t\treturn ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, fields[0])\n\t{{- end }}\n\tdefault:\n\t\tpanic(\"unknown field \" + strconv.Quote(fields[0].Name))\n\t}\n}\n{{- else }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet{{if not $object.Root}}, obj *{{$object.FullName}} {{end}}) graphql.Marshaler {\n\tfields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)\n\t{{if $object.Root}}\n\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\t\tObject: {{$object.GQLType|quote}},\n\t\t})\n\t{{end}}\n\n\t{{if $object.IsConcurrent}} var wg sync.WaitGroup {{end}}\n\tout := graphql.NewOrderedMap(len(fields))\n\tinvalid := false\n\tfor i, field := range fields {\n\t\tout.Keys[i] = field.Alias\n\n\t\tswitch field.Name {\n\t\tcase \"__typename\":\n\t\t\tout.Values[i] = graphql.MarshalString({{$object.GQLType|quote}})\n\t\t{{- range $field := $object.Fields }}\n\t\tcase \"{{$field.GQLName}}\":\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\twg.Add(1)\n\t\t\t\tgo func(i int, field graphql.CollectedField) {\n\t\t\t{{- end }}\n\t\t\t\tout.Values[i] = ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, field{{if not $object.Root}}, obj{{end}})\n\t\t\t\t{{- if $field.ASTType.NonNull }}\n\t\t\t\t\tif out.Values[i] == graphql.Null {\n\t\t\t\t\t\tinvalid = true\n\t\t\t\t\t}\n\t\t\t\t{{- end }}\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\t\twg.Done()\n\t\t\t\t}(i, field)\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t\tdefault:\n\t\t\tpanic(\"unknown field \" + strconv.Quote(field.Name))\n\t\t}\n\t}\n\t{{if $object.IsConcurrent}} wg.Wait() {{end}}\n\tif invalid { return graphql.Null }\n\treturn out\n}\n{{- end }}\n", "resolver.gotpl": "//go:generate gorunpkg github.com/99designs/gqlgen\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\ntype {{.ResolverType}} struct {}\n\n{{ range $object := .Objects -}}\n\t{{- if $object.HasResolvers -}}\n\t\tfunc (r *{{$.ResolverType}}) {{$object.GQLType}}() {{ $object.ResolverInterface.FullName }} {\n\t\t\treturn &{{lcFirst $object.GQLType}}Resolver{r}\n\t\t}\n\t{{ end -}}\n{{ end }}\n\n{{ range $object := .Objects -}}\n\t{{- if $object.HasResolvers -}}\n\t\ttype {{lcFirst $object.GQLType}}Resolver struct { *Resolver }\n\n\t\t{{ range $field := $object.Fields -}}\n\t\t\t{{- if $field.IsResolver -}}\n\t\t\tfunc (r *{{lcFirst $object.GQLType}}Resolver) {{ $field.ShortResolverDeclaration }} {\n\t\t\t\tpanic(\"not implemented\")\n\t\t\t}\n\t\t\t{{ end -}}\n\t\t{{ end -}}\n\t{{ end -}}\n{{ end }}\n", "server.gotpl": "package main\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\nconst defaultPort = \"8080\"\n\nfunc main() {\n\tport := os.Getenv(\"PORT\")\n\tif port == \"\" {\n\t\tport = defaultPort\n\t}\n\n\thttp.Handle(\"/\", handler.Playground(\"GraphQL playground\", \"/query\"))\n\thttp.Handle(\"/query\", handler.GraphQL({{.ExecPackageName}}.NewExecutableSchema({{.ExecPackageName}}.Config{Resolvers: &{{.ResolverPackageName}}.Resolver{}})))\n\n\tlog.Printf(\"connect to http://localhost:%s/ for GraphQL playground\", port)\n\tlog.Fatal(http.ListenAndServe(\":\" + port, nil))\n}\n", diff --git a/codegen/templates/models.gotpl b/codegen/templates/models.gotpl index c9599333565..6f149429d22 100644 --- a/codegen/templates/models.gotpl +++ b/codegen/templates/models.gotpl @@ -1,4 +1,5 @@ // Code generated by github.com/99designs/gqlgen, DO NOT EDIT. +// Dirt package {{ .PackageName }} @@ -9,10 +10,13 @@ import ( ) {{ range $model := .Models }} + {{with .Description}} {{.|prefixLines "// "}} {{end}} + {{- if .IsInterface }} type {{.GoType}} interface {} {{- else }} type {{.GoType}} struct { + {{- range $field := .Fields }} {{- if $field.GoFieldName }} {{ $field.GoFieldName }} {{$field.Signature}} `json:"{{$field.GQLName}}"` @@ -25,12 +29,13 @@ import ( {{- end}} {{ range $enum := .Enums }} + {{with .Description}} {{.|prefixLines "// "}} {{end}} type {{.GoType}} string const ( - {{ range $value := .Values -}} + {{ range $value := .Values}} {{with .Description}} {{.|prefixLines "// "}} {{end}} {{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}} - {{- end }} + {{ end }} ) func (e {{.GoType}}) IsValid() bool { From 076f9eac313b2d2460f4f735a8e59283a2862950 Mon Sep 17 00:00:00 2001 From: Andri Oskarsson Date: Sun, 26 Aug 2018 10:22:05 +0200 Subject: [PATCH 08/15] removed dirt --- codegen/templates/models.gotpl | 1 - 1 file changed, 1 deletion(-) diff --git a/codegen/templates/models.gotpl b/codegen/templates/models.gotpl index 6f149429d22..51c3339b17d 100644 --- a/codegen/templates/models.gotpl +++ b/codegen/templates/models.gotpl @@ -1,5 +1,4 @@ // Code generated by github.com/99designs/gqlgen, DO NOT EDIT. -// Dirt package {{ .PackageName }} From fce4c722a818665a1b0277693a5123b6f166f4a8 Mon Sep 17 00:00:00 2001 From: vvakame Date: Mon, 27 Aug 2018 13:01:36 +0900 Subject: [PATCH 09/15] address comment --- codegen/object.go | 45 ++- codegen/testserver/generated.go | 421 +++++++++++++++------- example/chat/generated.go | 354 ++++++++++++------ example/config/generated.go | 354 ++++++++++++------ example/dataloader/generated.go | 523 +++++++++++++++++++-------- example/scalars/generated.go | 354 ++++++++++++------ example/selection/generated.go | 410 +++++++++++++-------- example/starwars/generated.go | 620 +++++++++++++++++++++----------- example/todo/generated.go | 354 ++++++++++++------ integration/generated.go | 382 +++++++++++++------- 10 files changed, 2599 insertions(+), 1218 deletions(-) diff --git a/codegen/object.go b/codegen/object.go index fc0669b4ad1..041c99c7481 100644 --- a/codegen/object.go +++ b/codegen/object.go @@ -212,7 +212,6 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ } var arr = "arr" + strconv.Itoa(depth) var index = "idx" + strconv.Itoa(depth) - var wg = "wg" + strconv.Itoa(depth) var usePtr bool if len(remainingMods) == 1 && !isPtr { usePtr = true @@ -220,29 +219,47 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ return tpl(` {{.arr}} := make(graphql.Array, len({{.val}})) - var {{.wg}} sync.WaitGroup - {{.wg}}.Add(len({{.val}})) + {{ if and .top (not .isScalar) }} var wg sync.WaitGroup {{ end }} + {{ if not .isScalar }} + isLen1 := len({{.val}}) == 1 + if !isLen1 { + wg.Add(len({{.val}})) + } + {{ end }} for {{.index}} := range {{.val}} { - go func({{.index}} int) { - defer {{.wg}}.Done() - {{- if not .isScalar }} - rctx := &graphql.ResolverContext{ - Index: &{{.index}}, - Result: {{ if .usePtr }}&{{end}}{{.val}}[{{.index}}], + {{- if not .isScalar }} + {{.index}} := {{.index}} + rctx := &graphql.ResolverContext{ + Index: &{{.index}}, + Result: {{ if .usePtr }}&{{end}}{{.val}}[{{.index}}], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func({{.index}} int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) - {{- end}} + {{.arr}}[{{.index}}] = func() graphql.Marshaler { + {{ .next }} + }() + } + if isLen1 { + f({{.index}}) + } else { + go f({{.index}}) + } + {{ else }} {{.arr}}[{{.index}}] = func() graphql.Marshaler { {{ .next }} }() - }({{.index}}) + {{- end}} } - {{.wg}}.Wait() + {{ if and .top (not .isScalar) }} wg.Wait() {{ end }} return {{.arr}}`, map[string]interface{}{ "val": val, "arr": arr, "index": index, - "wg": wg, + "top": depth == 1, + "arrayLen": len(val), "isScalar": f.IsScalar, "usePtr": usePtr, "next": f.doWriteJson(val+"["+index+"]", remainingMods[1:], astType.Elem, false, depth+1), diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 9c423de3274..e300041d2bf 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -874,29 +874,44 @@ func (ec *executionContext) _Query_nestedOutputs(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { arr2 := make(graphql.Array, len(res[idx1])) - var wg2 sync.WaitGroup - wg2.Add(len(res[idx1])) + + isLen1 := len(res[idx1]) == 1 + if !isLen1 { + wg.Add(len(res[idx1])) + } + for idx2 := range res[idx1] { - go func(idx2 int) { - defer wg2.Done() - rctx := &graphql.ResolverContext{ - Index: &idx2, - Result: res[idx1][idx2], + idx2 := idx2 + rctx := &graphql.ResolverContext{ + Index: &idx2, + Result: res[idx1][idx2], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx2 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr2[idx2] = func() graphql.Marshaler { if res[idx1][idx2] == nil { @@ -905,14 +920,26 @@ func (ec *executionContext) _Query_nestedOutputs(ctx context.Context, field grap return ec._OuterObject(ctx, field.Selections, res[idx1][idx2]) }() - }(idx2) + } + if isLen1 { + f(idx2) + } else { + go f(idx2) + } + } - wg2.Wait() + return arr2 }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -973,16 +1000,24 @@ func (ec *executionContext) _Query_shapes(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { if res[idx1] == nil { @@ -991,9 +1026,15 @@ func (ec *executionContext) _Query_shapes(ctx context.Context, field graphql.Col return ec._Shape(ctx, field.Selections, res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1569,17 +1610,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -1604,23 +1641,37 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1856,23 +1907,37 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2145,23 +2210,37 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2264,23 +2343,37 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Directive(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2424,23 +2517,37 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Field(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2462,23 +2569,37 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2500,23 +2621,37 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2550,23 +2685,37 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2588,23 +2737,37 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } diff --git a/example/chat/generated.go b/example/chat/generated.go index 2c840e3fe6f..588d5f884b6 100644 --- a/example/chat/generated.go +++ b/example/chat/generated.go @@ -198,23 +198,37 @@ func (ec *executionContext) _Chatroom_messages(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Message(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -735,17 +749,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -770,23 +780,37 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1022,23 +1046,37 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1311,23 +1349,37 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1430,23 +1482,37 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Directive(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1590,23 +1656,37 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Field(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1628,23 +1708,37 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1666,23 +1760,37 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1716,23 +1824,37 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1754,23 +1876,37 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } diff --git a/example/config/generated.go b/example/config/generated.go index 60bd6d0387f..5f6f51403c8 100644 --- a/example/config/generated.go +++ b/example/config/generated.go @@ -228,23 +228,37 @@ func (ec *executionContext) _Query_todos(ctx context.Context, field graphql.Coll rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Todo(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -657,17 +671,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -692,23 +702,37 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -944,23 +968,37 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1233,23 +1271,37 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1352,23 +1404,37 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Directive(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1512,23 +1578,37 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Field(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1550,23 +1630,37 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1588,23 +1682,37 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1638,23 +1746,37 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1676,23 +1798,37 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } diff --git a/example/dataloader/generated.go b/example/dataloader/generated.go index c3718bd5770..8ad2fc63ed1 100644 --- a/example/dataloader/generated.go +++ b/example/dataloader/generated.go @@ -327,23 +327,37 @@ func (ec *executionContext) _Customer_orders(ctx context.Context, field graphql. rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Order(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -530,23 +544,37 @@ func (ec *executionContext) _Order_items(ctx context.Context, field graphql.Coll rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Item(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -620,23 +648,37 @@ func (ec *executionContext) _Query_customers(ctx context.Context, field graphql. rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Customer(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -681,23 +723,37 @@ func (ec *executionContext) _Query_torture1d(ctx context.Context, field graphql. rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Customer(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -753,41 +809,68 @@ func (ec *executionContext) _Query_torture2d(ctx context.Context, field graphql. rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { arr2 := make(graphql.Array, len(res[idx1])) - var wg2 sync.WaitGroup - wg2.Add(len(res[idx1])) + + isLen1 := len(res[idx1]) == 1 + if !isLen1 { + wg.Add(len(res[idx1])) + } + for idx2 := range res[idx1] { - go func(idx2 int) { - defer wg2.Done() - rctx := &graphql.ResolverContext{ - Index: &idx2, - Result: &res[idx1][idx2], + idx2 := idx2 + rctx := &graphql.ResolverContext{ + Index: &idx2, + Result: &res[idx1][idx2], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx2 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr2[idx2] = func() graphql.Marshaler { return ec._Customer(ctx, field.Selections, &res[idx1][idx2]) }() - }(idx2) + } + if isLen1 { + f(idx2) + } else { + go f(idx2) + } + } - wg2.Wait() + return arr2 }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -955,17 +1038,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -990,23 +1069,37 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1242,23 +1335,37 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1531,23 +1638,37 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1650,23 +1771,37 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Directive(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1810,23 +1945,37 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Field(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1848,23 +1997,37 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1886,23 +2049,37 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1936,23 +2113,37 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1974,23 +2165,37 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } diff --git a/example/scalars/generated.go b/example/scalars/generated.go index 9d60f27e8c0..b44bc68e07c 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -279,23 +279,37 @@ func (ec *executionContext) _Query_search(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._User(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -696,17 +710,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -731,23 +741,37 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -983,23 +1007,37 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1272,23 +1310,37 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1391,23 +1443,37 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Directive(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1551,23 +1617,37 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Field(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1589,23 +1669,37 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1627,23 +1721,37 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1677,23 +1785,37 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1715,23 +1837,37 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } diff --git a/example/selection/generated.go b/example/selection/generated.go index 6d127531080..f49b0a6e02b 100644 --- a/example/selection/generated.go +++ b/example/selection/generated.go @@ -178,17 +178,13 @@ func (ec *executionContext) _Like_selection(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -210,17 +206,13 @@ func (ec *executionContext) _Like_collected(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -325,17 +317,13 @@ func (ec *executionContext) _Post_selection(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -357,17 +345,13 @@ func (ec *executionContext) _Post_collected(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -429,23 +413,37 @@ func (ec *executionContext) _Query_events(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Event(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -613,17 +611,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -648,23 +642,37 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -900,23 +908,37 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1189,23 +1211,37 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1308,23 +1344,37 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Directive(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1468,23 +1518,37 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Field(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1506,23 +1570,37 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1544,23 +1622,37 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1594,23 +1686,37 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1632,23 +1738,37 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } diff --git a/example/starwars/generated.go b/example/starwars/generated.go index c38bc0db7f0..9213b5753f0 100644 --- a/example/starwars/generated.go +++ b/example/starwars/generated.go @@ -240,23 +240,37 @@ func (ec *executionContext) _Droid_friends(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Character(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -336,17 +350,13 @@ func (ec *executionContext) _Droid_appearsIn(ctx context.Context, field graphql. rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return res[idx1] - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return res[idx1] + }() } - wg1.Wait() + return arr1 } @@ -457,23 +467,37 @@ func (ec *executionContext) _FriendsConnection_edges(ctx context.Context, field rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._FriendsEdge(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -495,23 +519,37 @@ func (ec *executionContext) _FriendsConnection_friends(ctx context.Context, fiel rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Character(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -796,23 +834,37 @@ func (ec *executionContext) _Human_friends(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Character(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -892,17 +944,13 @@ func (ec *executionContext) _Human_appearsIn(ctx context.Context, field graphql. rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return res[idx1] - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return res[idx1] + }() } - wg1.Wait() + return arr1 } @@ -924,23 +972,37 @@ func (ec *executionContext) _Human_starships(ctx context.Context, field graphql. rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Starship(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1289,23 +1351,37 @@ func (ec *executionContext) _Query_reviews(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Review(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1342,23 +1418,37 @@ func (ec *executionContext) _Query_search(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._SearchResult(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1809,30 +1899,22 @@ func (ec *executionContext) _Starship_history(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { + arr1[idx1] = func() graphql.Marshaler { - arr2 := make(graphql.Array, len(res[idx1])) - var wg2 sync.WaitGroup - wg2.Add(len(res[idx1])) - for idx2 := range res[idx1] { - go func(idx2 int) { - defer wg2.Done() - arr2[idx2] = func() graphql.Marshaler { - return graphql.MarshalInt(res[idx1][idx2]) - }() - }(idx2) - } - wg2.Wait() - return arr2 - }() - }(idx1) + arr2 := make(graphql.Array, len(res[idx1])) + + for idx2 := range res[idx1] { + arr2[idx2] = func() graphql.Marshaler { + return graphql.MarshalInt(res[idx1][idx2]) + }() + } + + return arr2 + }() } - wg1.Wait() + return arr1 } @@ -1940,17 +2022,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -1975,23 +2053,37 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2227,23 +2319,37 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2516,23 +2622,37 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2635,23 +2755,37 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Directive(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2795,23 +2929,37 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Field(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2833,23 +2981,37 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2871,23 +3033,37 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2921,23 +3097,37 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -2959,23 +3149,37 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } diff --git a/example/todo/generated.go b/example/todo/generated.go index ebe2a66a04a..49485cb0e4e 100644 --- a/example/todo/generated.go +++ b/example/todo/generated.go @@ -348,23 +348,37 @@ func (ec *executionContext) _MyQuery_todos(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec._Todo(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -638,17 +652,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -673,23 +683,37 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -925,23 +949,37 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1214,23 +1252,37 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1333,23 +1385,37 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Directive(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1493,23 +1559,37 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Field(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1531,23 +1611,37 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1569,23 +1663,37 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1619,23 +1727,37 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1657,23 +1779,37 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } diff --git a/integration/generated.go b/integration/generated.go index 21f86a3cf4b..bf648ad6f61 100644 --- a/integration/generated.go +++ b/integration/generated.go @@ -205,17 +205,13 @@ func (ec *executionContext) _Element_mismatched(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalBoolean(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalBoolean(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -310,16 +306,24 @@ func (ec *executionContext) _Query_path(ctx context.Context, field graphql.Colle rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { if res[idx1] == nil { @@ -328,9 +332,15 @@ func (ec *executionContext) _Query_path(ctx context.Context, field graphql.Colle return ec._Element(ctx, field.Selections, res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -591,17 +601,13 @@ func (ec *executionContext) _User_likes(ctx context.Context, field graphql.Colle rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -760,17 +766,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - arr1[idx1] = func() graphql.Marshaler { - return graphql.MarshalString(res[idx1]) - }() - }(idx1) + arr1[idx1] = func() graphql.Marshaler { + return graphql.MarshalString(res[idx1]) + }() } - wg1.Wait() + return arr1 } @@ -795,23 +797,37 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1047,23 +1063,37 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1336,23 +1366,37 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1455,23 +1499,37 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Directive(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1615,23 +1673,37 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Field(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1653,23 +1725,37 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1691,23 +1777,37 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___Type(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1741,23 +1841,37 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } @@ -1779,23 +1893,37 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Result = res arr1 := make(graphql.Array, len(res)) - var wg1 sync.WaitGroup - wg1.Add(len(res)) + var wg sync.WaitGroup + + isLen1 := len(res) == 1 + if !isLen1 { + wg.Add(len(res)) + } + for idx1 := range res { - go func(idx1 int) { - defer wg1.Done() - rctx := &graphql.ResolverContext{ - Index: &idx1, - Result: &res[idx1], + idx1 := idx1 + rctx := &graphql.ResolverContext{ + Index: &idx1, + Result: &res[idx1], + } + ctx := graphql.WithResolverContext(ctx, rctx) + f := func(idx1 int) { + if !isLen1 { + defer wg.Done() } - ctx := graphql.WithResolverContext(ctx, rctx) arr1[idx1] = func() graphql.Marshaler { return ec.___InputValue(ctx, field.Selections, &res[idx1]) }() - }(idx1) + } + if isLen1 { + f(idx1) + } else { + go f(idx1) + } + } - wg1.Wait() + wg.Wait() return arr1 } From d63449b91ae1b95c1faa0dbd1100928e2d1b8641 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Mon, 27 Aug 2018 14:03:18 +1000 Subject: [PATCH 10/15] Remove support for the old json typemap --- cmd/gen.go | 40 ---------------------------------------- cmd/init.go | 3 --- cmd/root.go | 2 -- 3 files changed, 45 deletions(-) diff --git a/cmd/gen.go b/cmd/gen.go index 6a529ac5b5e..a941eda8693 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -8,7 +8,6 @@ import ( "github.com/99designs/gqlgen/codegen" "github.com/pkg/errors" "github.com/spf13/cobra" - "gopkg.in/yaml.v2" ) func init() { @@ -39,7 +38,6 @@ var genCmd = &cobra.Command{ } // overwrite by commandline options - var emitYamlGuidance bool if schemaFilename != "" { config.SchemaFilename = schemaFilename } @@ -55,10 +53,6 @@ var genCmd = &cobra.Command{ if modelPackageName != "" { config.Model.Package = modelPackageName } - if typemap != "" { - config.Models = loadModelMap() - emitYamlGuidance = true - } schemaRaw, err := ioutil.ReadFile(config.SchemaFilename) if err != nil { @@ -72,17 +66,6 @@ var genCmd = &cobra.Command{ os.Exit(1) } - if emitYamlGuidance { - var b []byte - b, err = yaml.Marshal(config) - if err != nil { - fmt.Fprintln(os.Stderr, "unable to marshal yaml: "+err.Error()) - os.Exit(1) - } - - fmt.Fprintf(os.Stderr, "DEPRECATION WARNING: we are moving away from the json typemap, instead create a gqlgen.yml with the following content:\n\n%s\n", string(b)) - } - err = codegen.Generate(*config) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) @@ -90,26 +73,3 @@ var genCmd = &cobra.Command{ } }, } - -func loadModelMap() codegen.TypeMap { - var goTypes map[string]string - b, err := ioutil.ReadFile(typemap) - if err != nil { - fmt.Fprintln(os.Stderr, "unable to open typemap: "+err.Error()) - return nil - } - - if err = yaml.Unmarshal(b, &goTypes); err != nil { - fmt.Fprintln(os.Stderr, "unable to parse typemap: "+err.Error()) - os.Exit(1) - } - - typeMap := make(codegen.TypeMap) - for typeName, entityPath := range goTypes { - typeMap[typeName] = codegen.TypeMapEntry{ - Model: entityPath, - } - } - - return typeMap -} diff --git a/cmd/init.go b/cmd/init.go index 4b7792f6c2f..f8b31945cfd 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -141,9 +141,6 @@ func initConfig() *codegen.Config { if modelPackageName != "" { config.Model.Package = modelPackageName } - if typemap != "" { - config.Models = loadModelMap() - } var buf bytes.Buffer buf.WriteString(strings.TrimSpace(configComment)) diff --git a/cmd/root.go b/cmd/root.go index 8598acd315c..1e9894cedbf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,7 +16,6 @@ var verbose bool var output string var models string var schemaFilename string -var typemap string var packageName string var modelPackageName string var serverFilename string @@ -29,7 +28,6 @@ func init() { rootCmd.PersistentFlags().StringVar(&output, "out", "", "the file to write to") rootCmd.PersistentFlags().StringVar(&models, "models", "", "the file to write the models to") rootCmd.PersistentFlags().StringVar(&schemaFilename, "schema", "", "the graphql schema to generate types from") - rootCmd.PersistentFlags().StringVar(&typemap, "typemap", "", "a json map going from graphql to golang types") rootCmd.PersistentFlags().StringVar(&packageName, "package", "", "the package name") rootCmd.PersistentFlags().StringVar(&modelPackageName, "modelpackage", "", "the package name to use for models") } From bb78d2faee1270626c670d21699bbde7e682cd93 Mon Sep 17 00:00:00 2001 From: Andri Oskarsson Date: Mon, 27 Aug 2018 08:35:47 +0200 Subject: [PATCH 11/15] go generate ./.. --- codegen/templates/data.go | 2 +- codegen/testserver/models-gen.go | 4 ++++ example/config/models_gen.go | 1 + example/dataloader/models_gen.go | 1 + example/selection/models_gen.go | 2 ++ example/starwars/models_gen.go | 13 ++++++++++--- example/todo/models_gen.go | 2 ++ integration/models-go/generated.go | 12 +++++++++--- 8 files changed, 30 insertions(+), 7 deletions(-) diff --git a/codegen/templates/data.go b/codegen/templates/data.go index 3eda08b9f5b..ad7db22aa00 100644 --- a/codegen/templates/data.go +++ b/codegen/templates/data.go @@ -6,7 +6,7 @@ var data = map[string]string{ "generated.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.\nfunc NewExecutableSchema(cfg Config) graphql.ExecutableSchema {\n\treturn &executableSchema{\n\t\tresolvers: cfg.Resolvers,\n\t\tdirectives: cfg.Directives,\n\t}\n}\n\ntype Config struct {\n\tResolvers ResolverRoot\n\tDirectives DirectiveRoot\n}\n\ntype ResolverRoot interface {\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers -}}\n\t\t{{$object.GQLType}}() {{$object.GQLType}}Resolver\n\t{{ end }}\n{{- end }}\n}\n\ntype DirectiveRoot struct {\n{{ range $directive := .Directives }}\n\t{{ $directive.Declaration }}\n{{ end }}\n}\n\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers }}\n\t\ttype {{$object.GQLType}}Resolver interface {\n\t\t{{ range $field := $object.Fields -}}\n\t\t\t{{ $field.ShortResolverDeclaration }}\n\t\t{{ end }}\n\t\t}\n\t{{- end }}\n{{- end }}\n\ntype executableSchema struct {\n\tresolvers ResolverRoot\n\tdirectives DirectiveRoot\n}\n\nfunc (e *executableSchema) Schema() *ast.Schema {\n\treturn parsedSchema\n}\n\nfunc (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {\n\t{{- if .QueryRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.QueryRoot.GQLType}}(ctx, op.SelectionSet)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"queries are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {\n\t{{- if .MutationRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.MutationRoot.GQLType}}(ctx, op.SelectionSet)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"mutations are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response {\n\t{{- if .SubscriptionRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tnext := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.SelectionSet)\n\t\tif ec.Errors != nil {\n\t\t\treturn graphql.OneShot(&graphql.Response{Data: []byte(\"null\"), Errors: ec.Errors})\n\t\t}\n\n\t\tvar buf bytes.Buffer\n\t\treturn func() *graphql.Response {\n\t\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\t\tbuf.Reset()\n\t\t\t\tdata := next()\n\n\t\t\t\tif data == nil {\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\tdata.MarshalGQL(&buf)\n\t\t\t\treturn buf.Bytes()\n\t\t\t})\n\n\t\t\treturn &graphql.Response{\n\t\t\t\tData: buf,\n\t\t\t\tErrors: ec.Errors,\n\t\t\t}\n\t\t}\n\t{{- else }}\n\t\treturn graphql.OneShot(graphql.ErrorResponse(ctx, \"subscriptions are not supported\"))\n\t{{- end }}\n}\n\ntype executionContext struct {\n\t*graphql.RequestContext\n\t*executableSchema\n}\n\n{{- range $object := .Objects }}\n\t{{ template \"object.gotpl\" $object }}\n\n\t{{- range $field := $object.Fields }}\n\t\t{{ template \"field.gotpl\" $field }}\n\t{{ end }}\n{{- end}}\n\n{{- range $interface := .Interfaces }}\n\t{{ template \"interface.gotpl\" $interface }}\n{{- end }}\n\n{{- range $input := .Inputs }}\n\t{{ template \"input.gotpl\" $input }}\n{{- end }}\n\nfunc (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tec.Error(ctx, ec.Recover(ctx, r))\n\t\t\tret = nil\n\t\t}\n\t}()\n\t{{- if .Directives }}\n\trctx := graphql.GetResolverContext(ctx)\n\tfor _, d := range rctx.Field.Definition.Directives {\n\t\tswitch d.Name {\n\t\t{{- range $directive := .Directives }}\n\t\tcase \"{{$directive.Name}}\":\n\t\t\tif ec.directives.{{$directive.Name|ucFirst}} != nil {\n\t\t\t\t{{- if $directive.Args }}\n\t\t\t\t\trawArgs := d.ArgumentMap(ec.Variables)\n\t\t\t\t\t{{ template \"args.gotpl\" $directive.Args }}\n\t\t\t\t{{- end }}\n\t\t\t\tn := next\n\t\t\t\tnext = func(ctx context.Context) (interface{}, error) {\n\t\t\t\t\treturn ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})\n\t\t\t\t}\n\t\t\t}\n\t\t{{- end }}\n\t\t}\n\t}\n\t{{- end }}\n\tres, err := ec.ResolverMiddleware(ctx, next)\n\tif err != nil {\n\t\tec.Error(ctx, err)\n\t\treturn nil\n\t}\n\treturn res\n}\n\nfunc (ec *executionContext) introspectSchema() *introspection.Schema {\n\treturn introspection.WrapSchema(parsedSchema)\n}\n\nfunc (ec *executionContext) introspectType(name string) *introspection.Type {\n\treturn introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name])\n}\n\nvar parsedSchema = gqlparser.MustLoadSchema(\n\t&ast.Source{Name: {{.SchemaFilename|quote}}, Input: {{.SchemaRaw|rawQuote}}},\n)\n", "input.gotpl": "\t{{- if .IsMarshaled }}\n\tfunc Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) {\n\t\tvar it {{.FullName}}\n\t\tvar asMap = v.(map[string]interface{})\n\t\t{{ range $field := .Fields}}\n\t\t\t{{- if $field.Default}}\n\t\t\t\tif _, present := asMap[{{$field.GQLName|quote}}] ; !present {\n\t\t\t\t\tasMap[{{$field.GQLName|quote}}] = {{ $field.Default | dump }}\n\t\t\t\t}\n\t\t\t{{- end}}\n\t\t{{- end }}\n\n\t\tfor k, v := range asMap {\n\t\t\tswitch k {\n\t\t\t{{- range $field := .Fields }}\n\t\t\tcase {{$field.GQLName|quote}}:\n\t\t\t\tvar err error\n\t\t\t\t{{ $field.Unmarshal (print \"it.\" $field.GoFieldName) \"v\" }}\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn it, err\n\t\t\t\t}\n\t\t\t{{- end }}\n\t\t\t}\n\t\t}\n\n\t\treturn it, nil\n\t}\n\t{{- end }}\n", "interface.gotpl": "{{- $interface := . }}\n\nfunc (ec *executionContext) _{{$interface.GQLType}}(ctx context.Context, sel ast.SelectionSet, obj *{{$interface.FullName}}) graphql.Marshaler {\n\tswitch obj := (*obj).(type) {\n\tcase nil:\n\t\treturn graphql.Null\n\t{{- range $implementor := $interface.Implementors }}\n\t\t{{- if $implementor.ValueReceiver }}\n\t\t\tcase {{$implementor.FullName}}:\n\t\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, &obj)\n\t\t{{- end}}\n\t\tcase *{{$implementor.FullName}}:\n\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, obj)\n\t{{- end }}\n\tdefault:\n\t\tpanic(fmt.Errorf(\"unexpected type %T\", obj))\n\t}\n}\n", - "models.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n// Dirt\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n{{ range $model := .Models }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\n\t{{- if .IsInterface }}\n\t\ttype {{.GoType}} interface {}\n\t{{- else }}\n\t\ttype {{.GoType}} struct {\n\n\t\t\t{{- range $field := .Fields }}\n\t\t\t\t{{- if $field.GoFieldName }}\n\t\t\t\t\t{{ $field.GoFieldName }} {{$field.Signature}} `json:\"{{$field.GQLName}}\"`\n\t\t\t\t{{- else }}\n\t\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t}\n\t{{- end }}\n{{- end}}\n\n{{ range $enum := .Enums }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\ttype {{.GoType}} string\n\tconst (\n\t{{ range $value := .Values}}\n\t\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\t\t{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}\n\t{{ end }}\n\t)\n\n\tfunc (e {{.GoType}}) IsValid() bool {\n\t\tswitch e {\n\t\tcase {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tfunc (e {{.GoType}}) String() string {\n\t\treturn string(e)\n\t}\n\n\tfunc (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {\n\t\tstr, ok := v.(string)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"enums must be strings\")\n\t\t}\n\n\t\t*e = {{.GoType}}(str)\n\t\tif !e.IsValid() {\n\t\t\treturn fmt.Errorf(\"%s is not a valid {{.GQLType}}\", str)\n\t\t}\n\t\treturn nil\n\t}\n\n\tfunc (e {{.GoType}}) MarshalGQL(w io.Writer) {\n\t\tfmt.Fprint(w, strconv.Quote(e.String()))\n\t}\n\n{{- end }}\n", + "models.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n{{ range $model := .Models }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\n\t{{- if .IsInterface }}\n\t\ttype {{.GoType}} interface {}\n\t{{- else }}\n\t\ttype {{.GoType}} struct {\n\n\t\t\t{{- range $field := .Fields }}\n\t\t\t\t{{- if $field.GoFieldName }}\n\t\t\t\t\t{{ $field.GoFieldName }} {{$field.Signature}} `json:\"{{$field.GQLName}}\"`\n\t\t\t\t{{- else }}\n\t\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t}\n\t{{- end }}\n{{- end}}\n\n{{ range $enum := .Enums }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\ttype {{.GoType}} string\n\tconst (\n\t{{ range $value := .Values}}\n\t\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\t\t{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}\n\t{{ end }}\n\t)\n\n\tfunc (e {{.GoType}}) IsValid() bool {\n\t\tswitch e {\n\t\tcase {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tfunc (e {{.GoType}}) String() string {\n\t\treturn string(e)\n\t}\n\n\tfunc (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {\n\t\tstr, ok := v.(string)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"enums must be strings\")\n\t\t}\n\n\t\t*e = {{.GoType}}(str)\n\t\tif !e.IsValid() {\n\t\t\treturn fmt.Errorf(\"%s is not a valid {{.GQLType}}\", str)\n\t\t}\n\t\treturn nil\n\t}\n\n\tfunc (e {{.GoType}}) MarshalGQL(w io.Writer) {\n\t\tfmt.Fprint(w, strconv.Quote(e.String()))\n\t}\n\n{{- end }}\n", "object.gotpl": "{{ $object := . }}\n\nvar {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}}\n\n// nolint: gocyclo, errcheck, gas, goconst\n{{- if .Stream }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet) func() graphql.Marshaler {\n\tfields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)\n\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\tObject: {{$object.GQLType|quote}},\n\t})\n\tif len(fields) != 1 {\n\t\tec.Errorf(ctx, \"must subscribe to exactly one stream\")\n\t\treturn nil\n\t}\n\n\tswitch fields[0].Name {\n\t{{- range $field := $object.Fields }}\n\tcase \"{{$field.GQLName}}\":\n\t\treturn ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, fields[0])\n\t{{- end }}\n\tdefault:\n\t\tpanic(\"unknown field \" + strconv.Quote(fields[0].Name))\n\t}\n}\n{{- else }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet{{if not $object.Root}}, obj *{{$object.FullName}} {{end}}) graphql.Marshaler {\n\tfields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)\n\t{{if $object.Root}}\n\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\t\tObject: {{$object.GQLType|quote}},\n\t\t})\n\t{{end}}\n\n\t{{if $object.IsConcurrent}} var wg sync.WaitGroup {{end}}\n\tout := graphql.NewOrderedMap(len(fields))\n\tinvalid := false\n\tfor i, field := range fields {\n\t\tout.Keys[i] = field.Alias\n\n\t\tswitch field.Name {\n\t\tcase \"__typename\":\n\t\t\tout.Values[i] = graphql.MarshalString({{$object.GQLType|quote}})\n\t\t{{- range $field := $object.Fields }}\n\t\tcase \"{{$field.GQLName}}\":\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\twg.Add(1)\n\t\t\t\tgo func(i int, field graphql.CollectedField) {\n\t\t\t{{- end }}\n\t\t\t\tout.Values[i] = ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, field{{if not $object.Root}}, obj{{end}})\n\t\t\t\t{{- if $field.ASTType.NonNull }}\n\t\t\t\t\tif out.Values[i] == graphql.Null {\n\t\t\t\t\t\tinvalid = true\n\t\t\t\t\t}\n\t\t\t\t{{- end }}\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\t\twg.Done()\n\t\t\t\t}(i, field)\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t\tdefault:\n\t\t\tpanic(\"unknown field \" + strconv.Quote(field.Name))\n\t\t}\n\t}\n\t{{if $object.IsConcurrent}} wg.Wait() {{end}}\n\tif invalid { return graphql.Null }\n\treturn out\n}\n{{- end }}\n", "resolver.gotpl": "//go:generate gorunpkg github.com/99designs/gqlgen\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\ntype {{.ResolverType}} struct {}\n\n{{ range $object := .Objects -}}\n\t{{- if $object.HasResolvers -}}\n\t\tfunc (r *{{$.ResolverType}}) {{$object.GQLType}}() {{ $object.ResolverInterface.FullName }} {\n\t\t\treturn &{{lcFirst $object.GQLType}}Resolver{r}\n\t\t}\n\t{{ end -}}\n{{ end }}\n\n{{ range $object := .Objects -}}\n\t{{- if $object.HasResolvers -}}\n\t\ttype {{lcFirst $object.GQLType}}Resolver struct { *Resolver }\n\n\t\t{{ range $field := $object.Fields -}}\n\t\t\t{{- if $field.IsResolver -}}\n\t\t\tfunc (r *{{lcFirst $object.GQLType}}Resolver) {{ $field.ShortResolverDeclaration }} {\n\t\t\t\tpanic(\"not implemented\")\n\t\t\t}\n\t\t\t{{ end -}}\n\t\t{{ end -}}\n\t{{ end -}}\n{{ end }}\n", "server.gotpl": "package main\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\nconst defaultPort = \"8080\"\n\nfunc main() {\n\tport := os.Getenv(\"PORT\")\n\tif port == \"\" {\n\t\tport = defaultPort\n\t}\n\n\thttp.Handle(\"/\", handler.Playground(\"GraphQL playground\", \"/query\"))\n\thttp.Handle(\"/query\", handler.GraphQL({{.ExecPackageName}}.NewExecutableSchema({{.ExecPackageName}}.Config{Resolvers: &{{.ResolverPackageName}}.Resolver{}})))\n\n\tlog.Printf(\"connect to http://localhost:%s/ for GraphQL playground\", port)\n\tlog.Fatal(http.ListenAndServe(\":\" + port, nil))\n}\n", diff --git a/codegen/testserver/models-gen.go b/codegen/testserver/models-gen.go index 312aeb63283..6195a275be1 100644 --- a/codegen/testserver/models-gen.go +++ b/codegen/testserver/models-gen.go @@ -5,9 +5,11 @@ package testserver type InnerInput struct { ID int `json:"id"` } + type InnerObject struct { ID int `json:"id"` } + type Keywords struct { Break string `json:"break"` Default string `json:"default"` @@ -35,9 +37,11 @@ type Keywords struct { Return string `json:"return"` Var string `json:"var"` } + type OuterInput struct { Inner InnerInput `json:"inner"` } + type OuterObject struct { Inner InnerObject `json:"inner"` } diff --git a/example/config/models_gen.go b/example/config/models_gen.go index 220b67695bc..056757da9f4 100644 --- a/example/config/models_gen.go +++ b/example/config/models_gen.go @@ -6,6 +6,7 @@ type NewTodo struct { Text string `json:"text"` UserID string `json:"userId"` } + type Todo struct { ID string `json:"id"` DatabaseID int `json:"databaseId"` diff --git a/example/dataloader/models_gen.go b/example/dataloader/models_gen.go index 4b9e8309da6..fb141c173fb 100644 --- a/example/dataloader/models_gen.go +++ b/example/dataloader/models_gen.go @@ -7,6 +7,7 @@ type Address struct { Street string `json:"street"` Country string `json:"country"` } + type Item struct { Name string `json:"name"` } diff --git a/example/selection/models_gen.go b/example/selection/models_gen.go index 6f68039dc68..ec05898826d 100644 --- a/example/selection/models_gen.go +++ b/example/selection/models_gen.go @@ -7,12 +7,14 @@ import ( ) type Event interface{} + type Like struct { Reaction string `json:"reaction"` Sent time.Time `json:"sent"` Selection []string `json:"selection"` Collected []string `json:"collected"` } + type Post struct { Message string `json:"message"` Sent time.Time `json:"sent"` diff --git a/example/starwars/models_gen.go b/example/starwars/models_gen.go index 36da5b0f51c..6f51606e30a 100644 --- a/example/starwars/models_gen.go +++ b/example/starwars/models_gen.go @@ -9,16 +9,20 @@ import ( ) type Character interface{} + type FriendsEdge struct { Cursor string `json:"cursor"` Node Character `json:"node"` } + type PageInfo struct { StartCursor string `json:"startCursor"` EndCursor string `json:"endCursor"` HasNextPage bool `json:"hasNextPage"` } + type SearchResult interface{} + type Starship struct { ID string `json:"id"` Name string `json:"name"` @@ -30,8 +34,10 @@ type Episode string const ( EpisodeNewhope Episode = "NEWHOPE" - EpisodeEmpire Episode = "EMPIRE" - EpisodeJedi Episode = "JEDI" + + EpisodeEmpire Episode = "EMPIRE" + + EpisodeJedi Episode = "JEDI" ) func (e Episode) IsValid() bool { @@ -67,7 +73,8 @@ type LengthUnit string const ( LengthUnitMeter LengthUnit = "METER" - LengthUnitFoot LengthUnit = "FOOT" + + LengthUnitFoot LengthUnit = "FOOT" ) func (e LengthUnit) IsValid() bool { diff --git a/example/todo/models_gen.go b/example/todo/models_gen.go index bcb243b53cb..5b768435605 100644 --- a/example/todo/models_gen.go +++ b/example/todo/models_gen.go @@ -8,6 +8,7 @@ import ( strconv "strconv" ) +// Passed to createTodo to create a new todo type TodoInput struct { Text string `json:"text"` Done *bool `json:"done"` @@ -17,6 +18,7 @@ type Role string const ( RoleAdmin Role = "ADMIN" + RoleOwner Role = "OWNER" ) diff --git a/integration/models-go/generated.go b/integration/models-go/generated.go index 1e0216bfffa..c4dbc28ac4d 100644 --- a/integration/models-go/generated.go +++ b/integration/models-go/generated.go @@ -17,11 +17,16 @@ type DateFilter struct { type DateFilterOp string const ( - DateFilterOpEq DateFilterOp = "EQ" + DateFilterOpEq DateFilterOp = "EQ" + DateFilterOpNeq DateFilterOp = "NEQ" - DateFilterOpGt DateFilterOp = "GT" + + DateFilterOpGt DateFilterOp = "GT" + DateFilterOpGte DateFilterOp = "GTE" - DateFilterOpLt DateFilterOp = "LT" + + DateFilterOpLt DateFilterOp = "LT" + DateFilterOpLte DateFilterOp = "LTE" ) @@ -58,6 +63,7 @@ type ErrorType string const ( ErrorTypeCustom ErrorType = "CUSTOM" + ErrorTypeNormal ErrorType = "NORMAL" ) From a0158a4edd009fbfd6f67a1ec63d1b69c56b719b Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Mon, 27 Aug 2018 17:16:15 +1000 Subject: [PATCH 12/15] Drop old cli flags --- Gopkg.lock | 48 ++++++++++++---------------------- cmd/gen.go | 38 +++++++-------------------- cmd/init.go | 58 +++++++++++++---------------------------- cmd/root.go | 62 ++++++++++++++++---------------------------- example/todo/todo.go | 2 +- 5 files changed, 67 insertions(+), 141 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index a10062414a5..7399021ad65 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -26,7 +26,7 @@ version = "v3.3.2" [[projects]] - digest = "1:f3df613325a793ffb3d0ce7644a3bb6f62db45ac744dafe20172fe999c61cdbf" + digest = "1:78907d832e27dbfc6e3fdfc52bd2e5e2e05c1d0e3789d4825b824489fbeab233" name = "github.com/gogo/protobuf" packages = [ "io", @@ -71,14 +71,6 @@ pruneopts = "UT" revision = "0fb14efe8c47ae851c0034ed7a448854d3d34cf3" -[[projects]] - digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be" - name = "github.com/inconshreveable/mousetrap" - packages = ["."] - pruneopts = "UT" - revision = "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75" - version = "v1.0" - [[projects]] branch = "master" digest = "1:ab9cfaf00fc5db5fd9d8e5f33da52e62bcc977d1976503dcc2a1492f391bd9ed" @@ -107,7 +99,7 @@ version = "v1.0.0" [[projects]] - digest = "1:450b7623b185031f3a456801155c8320209f75d0d4c4e633c6b1e59d44d6e392" + digest = "1:27af6024faa3c28426a698b8c653be0fd908bc96e25b7d76f2192eb342427db6" name = "github.com/opentracing/opentracing-go" packages = [ ".", @@ -151,23 +143,7 @@ revision = "ffb13db8def02f545acc58bd288ec6057c2bbfb9" [[projects]] - digest = "1:645cabccbb4fa8aab25a956cbcbdf6a6845ca736b2c64e197ca7cbb9d210b939" - name = "github.com/spf13/cobra" - packages = ["."] - pruneopts = "UT" - revision = "ef82de70bb3f60c65fb8eebacbb2d122ef517385" - version = "v0.0.3" - -[[projects]] - digest = "1:9424f440bba8f7508b69414634aef3b2b3a877e522d8a4624692412805407bb7" - name = "github.com/spf13/pflag" - packages = ["."] - pruneopts = "UT" - revision = "583c0c0531f06d5278b7d917446061adc344b5cd" - version = "v1.0.1" - -[[projects]] - digest = "1:7e8d267900c7fa7f35129a2a37596e38ed0f11ca746d6d9ba727980ee138f9f6" + digest = "1:73697231b93fb74a73ebd8384b68b9a60c57ea6b13c56d2425414566a72c8e6d" name = "github.com/stretchr/testify" packages = [ "assert", @@ -177,6 +153,14 @@ revision = "12b6f73e6084dad08a7c6e575284b177ecafbc71" version = "v1.2.1" +[[projects]] + digest = "1:b24d38b282bacf9791408a080f606370efa3d364e4b5fd9ba0f7b87786d3b679" + name = "github.com/urfave/cli" + packages = ["."] + pruneopts = "UT" + revision = "cfb38830724cc34fedffe9a2a29fb54fa9169cd1" + version = "v1.20.0" + [[projects]] branch = "master" digest = "1:3684f335059f8441260c3e949c0b99de97ea1057ecc493fdbd72ff3c27863277" @@ -187,7 +171,7 @@ [[projects]] branch = "master" - digest = "1:c6b203aeee848da05afba3a2dfaa47bb45668b1511f238e0cf5178e50c9607a2" + digest = "1:3e2c3d0b8cb780a09467884bebc032c831ae273d1d993b8f55fa61084e37278c" name = "github.com/vektah/gqlparser" packages = [ ".", @@ -211,7 +195,7 @@ [[projects]] branch = "master" - digest = "1:3cbc05413b8aac22b1f6d4350ed696b5a83a8515a4136db8f1ec3a0aee3d76e1" + digest = "1:77fe642412bfed48743e2b75163e3ab5c430cfe22dd488788647b89b28794635" name = "golang.org/x/tools" packages = [ "go/ast/astutil", @@ -232,7 +216,7 @@ [[projects]] branch = "master" - digest = "1:741ebea9214cc226789d3003baeca9b169e04b5b336fb1a3b2c16e75bd296bb5" + digest = "1:7ddb3a7b35cc853fe0db36a1b2473bdff03f28add7d28e4725e692603111266e" name = "sourcegraph.com/sourcegraph/appdash" packages = [ ".", @@ -248,7 +232,7 @@ [[projects]] branch = "master" - digest = "1:8e0a2957fe342f22d70a543c3fcdf390f7627419c3d82d87ab4fd715a9ef5716" + digest = "1:be108b48d79c3b3c345811a57a47ee87fdbe895beb4bb56239da71d4943e5be7" name = "sourcegraph.com/sourcegraph/appdash-data" packages = ["."] pruneopts = "UT" @@ -267,9 +251,9 @@ "github.com/opentracing/opentracing-go/ext", "github.com/opentracing/opentracing-go/log", "github.com/pkg/errors", - "github.com/spf13/cobra", "github.com/stretchr/testify/assert", "github.com/stretchr/testify/require", + "github.com/urfave/cli", "github.com/vektah/dataloaden", "github.com/vektah/gqlparser", "github.com/vektah/gqlparser/ast", diff --git a/cmd/gen.go b/cmd/gen.go index a941eda8693..0999b9de922 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -7,21 +7,20 @@ import ( "github.com/99designs/gqlgen/codegen" "github.com/pkg/errors" - "github.com/spf13/cobra" + "github.com/urfave/cli" ) -func init() { - rootCmd.AddCommand(genCmd) -} - -var genCmd = &cobra.Command{ - Use: "gen", - Short: "Generate models & resolvers .go", - Long: "", - Run: func(cmd *cobra.Command, args []string) { +var genCmd = cli.Command{ + Name: "generate", + Usage: "generate a graphql server based on schema", + Flags: []cli.Flag{ + cli.BoolFlag{Name: "verbose, v", Usage: "show logs"}, + cli.StringFlag{Name: "config, c", Usage: "the config filename"}, + }, + Action: func(ctx *cli.Context) { var config *codegen.Config var err error - if configFilename != "" { + if configFilename := ctx.String("config"); configFilename != "" { config, err = codegen.LoadConfig(configFilename) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) @@ -37,23 +36,6 @@ var genCmd = &cobra.Command{ } } - // overwrite by commandline options - if schemaFilename != "" { - config.SchemaFilename = schemaFilename - } - if models != "" { - config.Model.Filename = models - } - if output != "" { - config.Exec.Filename = output - } - if packageName != "" { - config.Exec.Package = packageName - } - if modelPackageName != "" { - config.Model.Package = modelPackageName - } - schemaRaw, err := ioutil.ReadFile(config.SchemaFilename) if err != nil { fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error()) diff --git a/cmd/init.go b/cmd/init.go index f8b31945cfd..2452510a852 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -9,14 +9,10 @@ import ( "github.com/99designs/gqlgen/codegen" "github.com/pkg/errors" - "github.com/spf13/cobra" + "github.com/urfave/cli" "gopkg.in/yaml.v2" ) -func init() { - rootCmd.AddCommand(initCmd) -} - var configComment = ` # .gqlgen.yml example # @@ -55,19 +51,24 @@ type Mutation { } ` -var initCmd = &cobra.Command{ - Use: "init", - Short: "Generate gqlgen skeleton", - Long: "", - Run: func(cmd *cobra.Command, args []string) { - initSchema() - config := initConfig() +var initCmd = cli.Command{ + Name: "init", + Usage: "create a new gqlgen project", + Flags: []cli.Flag{ + cli.BoolFlag{Name: "verbose, v", Usage: "show logs"}, + cli.StringFlag{Name: "config, c", Usage: "the config filename"}, + cli.StringFlag{Name: "server", Usage: "where to write the server stub to", Value: "server/server.go"}, + cli.StringFlag{Name: "schema", Usage: "where to write the schema stub to", Value: "schema.graphql"}, + }, + Action: func(ctx *cli.Context) { + initSchema(ctx.String("schema")) + config := initConfig(ctx) - GenerateGraphServer(config) + GenerateGraphServer(config, ctx.String("server")) }, } -func GenerateGraphServer(config *codegen.Config) { +func GenerateGraphServer(config *codegen.Config, serverFilename string) { schemaRaw, err := ioutil.ReadFile(config.SchemaFilename) if err != nil { fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error()) @@ -80,10 +81,6 @@ func GenerateGraphServer(config *codegen.Config) { os.Exit(1) } - if serverFilename == "" { - serverFilename = "server/server.go" - } - if err := codegen.Generate(*config); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) @@ -97,9 +94,10 @@ func GenerateGraphServer(config *codegen.Config) { fmt.Fprintf(os.Stdout, "Exec \"go run ./%s\" to start GraphQL server\n", serverFilename) } -func initConfig() *codegen.Config { +func initConfig(ctx *cli.Context) *codegen.Config { var config *codegen.Config var err error + configFilename := ctx.String("config") if configFilename != "" { config, err = codegen.LoadConfig(configFilename) } else { @@ -126,22 +124,6 @@ func initConfig() *codegen.Config { Type: "Resolver", } - if schemaFilename != "" { - config.SchemaFilename = schemaFilename - } - if models != "" { - config.Model.Filename = models - } - if output != "" { - config.Exec.Filename = output - } - if packageName != "" { - config.Exec.Package = packageName - } - if modelPackageName != "" { - config.Model.Package = modelPackageName - } - var buf bytes.Buffer buf.WriteString(strings.TrimSpace(configComment)) buf.WriteString("\n\n") @@ -164,11 +146,7 @@ func initConfig() *codegen.Config { return config } -func initSchema() { - if schemaFilename == "" { - schemaFilename = "schema.graphql" - } - +func initSchema(schemaFilename string) { _, err := os.Stat(schemaFilename) if !os.IsNotExist(err) { return diff --git a/cmd/root.go b/cmd/root.go index 1e9894cedbf..394f370b492 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,59 +7,41 @@ import ( "os" "github.com/99designs/gqlgen/internal/gopath" - "github.com/spf13/cobra" + "github.com/urfave/cli" ) -var configFilename string -var verbose bool - -var output string -var models string -var schemaFilename string -var packageName string -var modelPackageName string -var serverFilename string - -func init() { - rootCmd.PersistentFlags().StringVarP(&configFilename, "config", "c", "", "the file to configuration to") - rootCmd.PersistentFlags().StringVarP(&serverFilename, "server", "s", "", "the file to write server to") - rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "show logs") - - rootCmd.PersistentFlags().StringVar(&output, "out", "", "the file to write to") - rootCmd.PersistentFlags().StringVar(&models, "models", "", "the file to write the models to") - rootCmd.PersistentFlags().StringVar(&schemaFilename, "schema", "", "the graphql schema to generate types from") - rootCmd.PersistentFlags().StringVar(&packageName, "package", "", "the package name") - rootCmd.PersistentFlags().StringVar(&modelPackageName, "modelpackage", "", "the package name to use for models") -} - -var rootCmd = &cobra.Command{ - Use: "gqlgen", - Short: "go generate based graphql server library", - Long: `This is a library for quickly creating strictly typed graphql servers in golang. - See https://gqlgen.com/ for a getting started guide.`, - PersistentPreRun: func(cmd *cobra.Command, args []string) { +func Execute() { + app := cli.NewApp() + app.Name = "gqlgen" + app.Usage = genCmd.Usage + app.Description = "This is a library for quickly creating strictly typed graphql servers in golang. See https://gqlgen.com/ for a getting started guide." + app.HideVersion = true + app.Flags = genCmd.Flags + app.Before = func(context *cli.Context) error { pwd, err := os.Getwd() if err != nil { - fmt.Fprintf(os.Stderr, "unable to determine current workding dir: %s\n", err.Error()) - os.Exit(1) + return fmt.Errorf("unable to determine current workding dir: %s\n", err.Error()) } if !gopath.Contains(pwd) { - fmt.Fprintf(os.Stderr, "gqlgen must be run from inside your $GOPATH\n") - os.Exit(1) + return fmt.Errorf("gqlgen must be run from inside your $GOPATH\n") } - if verbose { + if context.Bool("verbose") { log.SetFlags(0) } else { log.SetOutput(ioutil.Discard) } - }, - Run: genCmd.Run, // delegate to gen subcommand -} + return nil + } -func Execute() { - if err := rootCmd.Execute(); err != nil { - fmt.Println(err) + app.Action = genCmd.Action + app.Commands = []cli.Command{ + genCmd, + initCmd, + } + + if err := app.Run(os.Args); err != nil { + fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } } diff --git a/example/todo/todo.go b/example/todo/todo.go index dc51b3e8f69..05846810e2b 100644 --- a/example/todo/todo.go +++ b/example/todo/todo.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen --out generated.go +//go:generate gorunpkg github.com/99designs/gqlgen package todo From 08ab33bedf493581c932313d6cc14e7b5722faf0 Mon Sep 17 00:00:00 2001 From: Andri Oskarsson Date: Mon, 27 Aug 2018 22:13:50 +0200 Subject: [PATCH 13/15] starting to look better --- codegen/templates/data.go | 2 +- codegen/templates/models.gotpl | 12 ++++++------ example/starwars/models_gen.go | 9 +++------ example/todo/models_gen.go | 1 - integration/models-go/generated.go | 12 +++--------- 5 files changed, 13 insertions(+), 23 deletions(-) diff --git a/codegen/templates/data.go b/codegen/templates/data.go index ad7db22aa00..b130b9c6f1d 100644 --- a/codegen/templates/data.go +++ b/codegen/templates/data.go @@ -6,7 +6,7 @@ var data = map[string]string{ "generated.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.\nfunc NewExecutableSchema(cfg Config) graphql.ExecutableSchema {\n\treturn &executableSchema{\n\t\tresolvers: cfg.Resolvers,\n\t\tdirectives: cfg.Directives,\n\t}\n}\n\ntype Config struct {\n\tResolvers ResolverRoot\n\tDirectives DirectiveRoot\n}\n\ntype ResolverRoot interface {\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers -}}\n\t\t{{$object.GQLType}}() {{$object.GQLType}}Resolver\n\t{{ end }}\n{{- end }}\n}\n\ntype DirectiveRoot struct {\n{{ range $directive := .Directives }}\n\t{{ $directive.Declaration }}\n{{ end }}\n}\n\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers }}\n\t\ttype {{$object.GQLType}}Resolver interface {\n\t\t{{ range $field := $object.Fields -}}\n\t\t\t{{ $field.ShortResolverDeclaration }}\n\t\t{{ end }}\n\t\t}\n\t{{- end }}\n{{- end }}\n\ntype executableSchema struct {\n\tresolvers ResolverRoot\n\tdirectives DirectiveRoot\n}\n\nfunc (e *executableSchema) Schema() *ast.Schema {\n\treturn parsedSchema\n}\n\nfunc (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {\n\t{{- if .QueryRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.QueryRoot.GQLType}}(ctx, op.SelectionSet)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"queries are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {\n\t{{- if .MutationRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.MutationRoot.GQLType}}(ctx, op.SelectionSet)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"mutations are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response {\n\t{{- if .SubscriptionRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tnext := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.SelectionSet)\n\t\tif ec.Errors != nil {\n\t\t\treturn graphql.OneShot(&graphql.Response{Data: []byte(\"null\"), Errors: ec.Errors})\n\t\t}\n\n\t\tvar buf bytes.Buffer\n\t\treturn func() *graphql.Response {\n\t\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\t\tbuf.Reset()\n\t\t\t\tdata := next()\n\n\t\t\t\tif data == nil {\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\tdata.MarshalGQL(&buf)\n\t\t\t\treturn buf.Bytes()\n\t\t\t})\n\n\t\t\treturn &graphql.Response{\n\t\t\t\tData: buf,\n\t\t\t\tErrors: ec.Errors,\n\t\t\t}\n\t\t}\n\t{{- else }}\n\t\treturn graphql.OneShot(graphql.ErrorResponse(ctx, \"subscriptions are not supported\"))\n\t{{- end }}\n}\n\ntype executionContext struct {\n\t*graphql.RequestContext\n\t*executableSchema\n}\n\n{{- range $object := .Objects }}\n\t{{ template \"object.gotpl\" $object }}\n\n\t{{- range $field := $object.Fields }}\n\t\t{{ template \"field.gotpl\" $field }}\n\t{{ end }}\n{{- end}}\n\n{{- range $interface := .Interfaces }}\n\t{{ template \"interface.gotpl\" $interface }}\n{{- end }}\n\n{{- range $input := .Inputs }}\n\t{{ template \"input.gotpl\" $input }}\n{{- end }}\n\nfunc (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tec.Error(ctx, ec.Recover(ctx, r))\n\t\t\tret = nil\n\t\t}\n\t}()\n\t{{- if .Directives }}\n\trctx := graphql.GetResolverContext(ctx)\n\tfor _, d := range rctx.Field.Definition.Directives {\n\t\tswitch d.Name {\n\t\t{{- range $directive := .Directives }}\n\t\tcase \"{{$directive.Name}}\":\n\t\t\tif ec.directives.{{$directive.Name|ucFirst}} != nil {\n\t\t\t\t{{- if $directive.Args }}\n\t\t\t\t\trawArgs := d.ArgumentMap(ec.Variables)\n\t\t\t\t\t{{ template \"args.gotpl\" $directive.Args }}\n\t\t\t\t{{- end }}\n\t\t\t\tn := next\n\t\t\t\tnext = func(ctx context.Context) (interface{}, error) {\n\t\t\t\t\treturn ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})\n\t\t\t\t}\n\t\t\t}\n\t\t{{- end }}\n\t\t}\n\t}\n\t{{- end }}\n\tres, err := ec.ResolverMiddleware(ctx, next)\n\tif err != nil {\n\t\tec.Error(ctx, err)\n\t\treturn nil\n\t}\n\treturn res\n}\n\nfunc (ec *executionContext) introspectSchema() *introspection.Schema {\n\treturn introspection.WrapSchema(parsedSchema)\n}\n\nfunc (ec *executionContext) introspectType(name string) *introspection.Type {\n\treturn introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name])\n}\n\nvar parsedSchema = gqlparser.MustLoadSchema(\n\t&ast.Source{Name: {{.SchemaFilename|quote}}, Input: {{.SchemaRaw|rawQuote}}},\n)\n", "input.gotpl": "\t{{- if .IsMarshaled }}\n\tfunc Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) {\n\t\tvar it {{.FullName}}\n\t\tvar asMap = v.(map[string]interface{})\n\t\t{{ range $field := .Fields}}\n\t\t\t{{- if $field.Default}}\n\t\t\t\tif _, present := asMap[{{$field.GQLName|quote}}] ; !present {\n\t\t\t\t\tasMap[{{$field.GQLName|quote}}] = {{ $field.Default | dump }}\n\t\t\t\t}\n\t\t\t{{- end}}\n\t\t{{- end }}\n\n\t\tfor k, v := range asMap {\n\t\t\tswitch k {\n\t\t\t{{- range $field := .Fields }}\n\t\t\tcase {{$field.GQLName|quote}}:\n\t\t\t\tvar err error\n\t\t\t\t{{ $field.Unmarshal (print \"it.\" $field.GoFieldName) \"v\" }}\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn it, err\n\t\t\t\t}\n\t\t\t{{- end }}\n\t\t\t}\n\t\t}\n\n\t\treturn it, nil\n\t}\n\t{{- end }}\n", "interface.gotpl": "{{- $interface := . }}\n\nfunc (ec *executionContext) _{{$interface.GQLType}}(ctx context.Context, sel ast.SelectionSet, obj *{{$interface.FullName}}) graphql.Marshaler {\n\tswitch obj := (*obj).(type) {\n\tcase nil:\n\t\treturn graphql.Null\n\t{{- range $implementor := $interface.Implementors }}\n\t\t{{- if $implementor.ValueReceiver }}\n\t\t\tcase {{$implementor.FullName}}:\n\t\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, &obj)\n\t\t{{- end}}\n\t\tcase *{{$implementor.FullName}}:\n\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, obj)\n\t{{- end }}\n\tdefault:\n\t\tpanic(fmt.Errorf(\"unexpected type %T\", obj))\n\t}\n}\n", - "models.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n{{ range $model := .Models }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\n\t{{- if .IsInterface }}\n\t\ttype {{.GoType}} interface {}\n\t{{- else }}\n\t\ttype {{.GoType}} struct {\n\n\t\t\t{{- range $field := .Fields }}\n\t\t\t\t{{- if $field.GoFieldName }}\n\t\t\t\t\t{{ $field.GoFieldName }} {{$field.Signature}} `json:\"{{$field.GQLName}}\"`\n\t\t\t\t{{- else }}\n\t\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t}\n\t{{- end }}\n{{- end}}\n\n{{ range $enum := .Enums }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\ttype {{.GoType}} string\n\tconst (\n\t{{ range $value := .Values}}\n\t\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\t\t{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}\n\t{{ end }}\n\t)\n\n\tfunc (e {{.GoType}}) IsValid() bool {\n\t\tswitch e {\n\t\tcase {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tfunc (e {{.GoType}}) String() string {\n\t\treturn string(e)\n\t}\n\n\tfunc (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {\n\t\tstr, ok := v.(string)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"enums must be strings\")\n\t\t}\n\n\t\t*e = {{.GoType}}(str)\n\t\tif !e.IsValid() {\n\t\t\treturn fmt.Errorf(\"%s is not a valid {{.GQLType}}\", str)\n\t\t}\n\t\treturn nil\n\t}\n\n\tfunc (e {{.GoType}}) MarshalGQL(w io.Writer) {\n\t\tfmt.Fprint(w, strconv.Quote(e.String()))\n\t}\n\n{{- end }}\n", + "models.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n{{ range $model := .Models }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\t{{- if .IsInterface }}\n\t\ttype {{.GoType}} interface {}\n\t{{- else }}\n\t\ttype {{.GoType}} struct {\n\t\t\t{{- range $field := .Fields }}\n\t\t\t\t{{- if $field.GoFieldName }}\n\t\t\t\t\t{{ $field.GoFieldName }} {{$field.Signature}} `json:\"{{$field.GQLName}}\"`\n\t\t\t\t{{- else }}\n\t\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t}\n\t{{- end }}\n{{- end}}\n\n{{ range $enum := .Enums }}\n\t{{with .Description}}{{.|prefixLines \"// \"}} {{end}}\n\ttype {{.GoType}} string\n\tconst (\n\t{{- range $value := .Values}}\n\t\t{{- with .Description}}\n\t\t\t{{.|prefixLines \"// \"}}\n\t\t{{- end}}\n\t\t{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}\n\t{{- end }}\n\t)\n\n\tfunc (e {{.GoType}}) IsValid() bool {\n\t\tswitch e {\n\t\tcase {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tfunc (e {{.GoType}}) String() string {\n\t\treturn string(e)\n\t}\n\n\tfunc (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {\n\t\tstr, ok := v.(string)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"enums must be strings\")\n\t\t}\n\n\t\t*e = {{.GoType}}(str)\n\t\tif !e.IsValid() {\n\t\t\treturn fmt.Errorf(\"%s is not a valid {{.GQLType}}\", str)\n\t\t}\n\t\treturn nil\n\t}\n\n\tfunc (e {{.GoType}}) MarshalGQL(w io.Writer) {\n\t\tfmt.Fprint(w, strconv.Quote(e.String()))\n\t}\n\n{{- end }}\n", "object.gotpl": "{{ $object := . }}\n\nvar {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}}\n\n// nolint: gocyclo, errcheck, gas, goconst\n{{- if .Stream }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet) func() graphql.Marshaler {\n\tfields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)\n\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\tObject: {{$object.GQLType|quote}},\n\t})\n\tif len(fields) != 1 {\n\t\tec.Errorf(ctx, \"must subscribe to exactly one stream\")\n\t\treturn nil\n\t}\n\n\tswitch fields[0].Name {\n\t{{- range $field := $object.Fields }}\n\tcase \"{{$field.GQLName}}\":\n\t\treturn ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, fields[0])\n\t{{- end }}\n\tdefault:\n\t\tpanic(\"unknown field \" + strconv.Quote(fields[0].Name))\n\t}\n}\n{{- else }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet{{if not $object.Root}}, obj *{{$object.FullName}} {{end}}) graphql.Marshaler {\n\tfields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)\n\t{{if $object.Root}}\n\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\t\tObject: {{$object.GQLType|quote}},\n\t\t})\n\t{{end}}\n\n\t{{if $object.IsConcurrent}} var wg sync.WaitGroup {{end}}\n\tout := graphql.NewOrderedMap(len(fields))\n\tinvalid := false\n\tfor i, field := range fields {\n\t\tout.Keys[i] = field.Alias\n\n\t\tswitch field.Name {\n\t\tcase \"__typename\":\n\t\t\tout.Values[i] = graphql.MarshalString({{$object.GQLType|quote}})\n\t\t{{- range $field := $object.Fields }}\n\t\tcase \"{{$field.GQLName}}\":\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\twg.Add(1)\n\t\t\t\tgo func(i int, field graphql.CollectedField) {\n\t\t\t{{- end }}\n\t\t\t\tout.Values[i] = ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, field{{if not $object.Root}}, obj{{end}})\n\t\t\t\t{{- if $field.ASTType.NonNull }}\n\t\t\t\t\tif out.Values[i] == graphql.Null {\n\t\t\t\t\t\tinvalid = true\n\t\t\t\t\t}\n\t\t\t\t{{- end }}\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\t\twg.Done()\n\t\t\t\t}(i, field)\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t\tdefault:\n\t\t\tpanic(\"unknown field \" + strconv.Quote(field.Name))\n\t\t}\n\t}\n\t{{if $object.IsConcurrent}} wg.Wait() {{end}}\n\tif invalid { return graphql.Null }\n\treturn out\n}\n{{- end }}\n", "resolver.gotpl": "//go:generate gorunpkg github.com/99designs/gqlgen\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\ntype {{.ResolverType}} struct {}\n\n{{ range $object := .Objects -}}\n\t{{- if $object.HasResolvers -}}\n\t\tfunc (r *{{$.ResolverType}}) {{$object.GQLType}}() {{ $object.ResolverInterface.FullName }} {\n\t\t\treturn &{{lcFirst $object.GQLType}}Resolver{r}\n\t\t}\n\t{{ end -}}\n{{ end }}\n\n{{ range $object := .Objects -}}\n\t{{- if $object.HasResolvers -}}\n\t\ttype {{lcFirst $object.GQLType}}Resolver struct { *Resolver }\n\n\t\t{{ range $field := $object.Fields -}}\n\t\t\t{{- if $field.IsResolver -}}\n\t\t\tfunc (r *{{lcFirst $object.GQLType}}Resolver) {{ $field.ShortResolverDeclaration }} {\n\t\t\t\tpanic(\"not implemented\")\n\t\t\t}\n\t\t\t{{ end -}}\n\t\t{{ end -}}\n\t{{ end -}}\n{{ end }}\n", "server.gotpl": "package main\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\nconst defaultPort = \"8080\"\n\nfunc main() {\n\tport := os.Getenv(\"PORT\")\n\tif port == \"\" {\n\t\tport = defaultPort\n\t}\n\n\thttp.Handle(\"/\", handler.Playground(\"GraphQL playground\", \"/query\"))\n\thttp.Handle(\"/query\", handler.GraphQL({{.ExecPackageName}}.NewExecutableSchema({{.ExecPackageName}}.Config{Resolvers: &{{.ResolverPackageName}}.Resolver{}})))\n\n\tlog.Printf(\"connect to http://localhost:%s/ for GraphQL playground\", port)\n\tlog.Fatal(http.ListenAndServe(\":\" + port, nil))\n}\n", diff --git a/codegen/templates/models.gotpl b/codegen/templates/models.gotpl index 51c3339b17d..d17b6f98f15 100644 --- a/codegen/templates/models.gotpl +++ b/codegen/templates/models.gotpl @@ -10,12 +10,10 @@ import ( {{ range $model := .Models }} {{with .Description}} {{.|prefixLines "// "}} {{end}} - {{- if .IsInterface }} type {{.GoType}} interface {} {{- else }} type {{.GoType}} struct { - {{- range $field := .Fields }} {{- if $field.GoFieldName }} {{ $field.GoFieldName }} {{$field.Signature}} `json:"{{$field.GQLName}}"` @@ -28,13 +26,15 @@ import ( {{- end}} {{ range $enum := .Enums }} - {{with .Description}} {{.|prefixLines "// "}} {{end}} + {{with .Description}}{{.|prefixLines "// "}} {{end}} type {{.GoType}} string const ( - {{ range $value := .Values}} - {{with .Description}} {{.|prefixLines "// "}} {{end}} + {{- range $value := .Values}} + {{- with .Description}} + {{.|prefixLines "// "}} + {{- end}} {{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}} - {{ end }} + {{- end }} ) func (e {{.GoType}}) IsValid() bool { diff --git a/example/starwars/models_gen.go b/example/starwars/models_gen.go index 6f51606e30a..b34acb08797 100644 --- a/example/starwars/models_gen.go +++ b/example/starwars/models_gen.go @@ -34,10 +34,8 @@ type Episode string const ( EpisodeNewhope Episode = "NEWHOPE" - - EpisodeEmpire Episode = "EMPIRE" - - EpisodeJedi Episode = "JEDI" + EpisodeEmpire Episode = "EMPIRE" + EpisodeJedi Episode = "JEDI" ) func (e Episode) IsValid() bool { @@ -73,8 +71,7 @@ type LengthUnit string const ( LengthUnitMeter LengthUnit = "METER" - - LengthUnitFoot LengthUnit = "FOOT" + LengthUnitFoot LengthUnit = "FOOT" ) func (e LengthUnit) IsValid() bool { diff --git a/example/todo/models_gen.go b/example/todo/models_gen.go index 5b768435605..8a9bb884f06 100644 --- a/example/todo/models_gen.go +++ b/example/todo/models_gen.go @@ -18,7 +18,6 @@ type Role string const ( RoleAdmin Role = "ADMIN" - RoleOwner Role = "OWNER" ) diff --git a/integration/models-go/generated.go b/integration/models-go/generated.go index c4dbc28ac4d..1e0216bfffa 100644 --- a/integration/models-go/generated.go +++ b/integration/models-go/generated.go @@ -17,16 +17,11 @@ type DateFilter struct { type DateFilterOp string const ( - DateFilterOpEq DateFilterOp = "EQ" - + DateFilterOpEq DateFilterOp = "EQ" DateFilterOpNeq DateFilterOp = "NEQ" - - DateFilterOpGt DateFilterOp = "GT" - + DateFilterOpGt DateFilterOp = "GT" DateFilterOpGte DateFilterOp = "GTE" - - DateFilterOpLt DateFilterOp = "LT" - + DateFilterOpLt DateFilterOp = "LT" DateFilterOpLte DateFilterOp = "LTE" ) @@ -63,7 +58,6 @@ type ErrorType string const ( ErrorTypeCustom ErrorType = "CUSTOM" - ErrorTypeNormal ErrorType = "NORMAL" ) From 7c0400454230b3bb01d49ba310b7047b064cdc65 Mon Sep 17 00:00:00 2001 From: Andri Oskarsson Date: Mon, 27 Aug 2018 22:29:41 +0200 Subject: [PATCH 14/15] now with field values --- codegen/object.go | 2 +- codegen/object_build.go | 1 + codegen/templates/data.go | 2 +- codegen/templates/models.gotpl | 3 +++ 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/codegen/object.go b/codegen/object.go index 93d1952102f..0bad499f261 100644 --- a/codegen/object.go +++ b/codegen/object.go @@ -32,7 +32,7 @@ type Object struct { type Field struct { *Type - + Description string // Description of a field GQLName string // The name of the field in graphql GoFieldType GoFieldType // The field type in go, if any GoReceiverName string // The name of method & var receiver in go, if any diff --git a/codegen/object_build.go b/codegen/object_build.go index 686b2bfe2a4..8573ad2402d 100644 --- a/codegen/object_build.go +++ b/codegen/object_build.go @@ -115,6 +115,7 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I GoReceiverName: "ec", GoFieldName: "introspectSchema", Object: obj, + Description: field.Description, }) continue } diff --git a/codegen/templates/data.go b/codegen/templates/data.go index b130b9c6f1d..6f70f82ae49 100644 --- a/codegen/templates/data.go +++ b/codegen/templates/data.go @@ -6,7 +6,7 @@ var data = map[string]string{ "generated.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.\nfunc NewExecutableSchema(cfg Config) graphql.ExecutableSchema {\n\treturn &executableSchema{\n\t\tresolvers: cfg.Resolvers,\n\t\tdirectives: cfg.Directives,\n\t}\n}\n\ntype Config struct {\n\tResolvers ResolverRoot\n\tDirectives DirectiveRoot\n}\n\ntype ResolverRoot interface {\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers -}}\n\t\t{{$object.GQLType}}() {{$object.GQLType}}Resolver\n\t{{ end }}\n{{- end }}\n}\n\ntype DirectiveRoot struct {\n{{ range $directive := .Directives }}\n\t{{ $directive.Declaration }}\n{{ end }}\n}\n\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers }}\n\t\ttype {{$object.GQLType}}Resolver interface {\n\t\t{{ range $field := $object.Fields -}}\n\t\t\t{{ $field.ShortResolverDeclaration }}\n\t\t{{ end }}\n\t\t}\n\t{{- end }}\n{{- end }}\n\ntype executableSchema struct {\n\tresolvers ResolverRoot\n\tdirectives DirectiveRoot\n}\n\nfunc (e *executableSchema) Schema() *ast.Schema {\n\treturn parsedSchema\n}\n\nfunc (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {\n\t{{- if .QueryRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.QueryRoot.GQLType}}(ctx, op.SelectionSet)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"queries are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {\n\t{{- if .MutationRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.MutationRoot.GQLType}}(ctx, op.SelectionSet)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"mutations are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response {\n\t{{- if .SubscriptionRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e}\n\n\t\tnext := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.SelectionSet)\n\t\tif ec.Errors != nil {\n\t\t\treturn graphql.OneShot(&graphql.Response{Data: []byte(\"null\"), Errors: ec.Errors})\n\t\t}\n\n\t\tvar buf bytes.Buffer\n\t\treturn func() *graphql.Response {\n\t\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\t\tbuf.Reset()\n\t\t\t\tdata := next()\n\n\t\t\t\tif data == nil {\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\tdata.MarshalGQL(&buf)\n\t\t\t\treturn buf.Bytes()\n\t\t\t})\n\n\t\t\treturn &graphql.Response{\n\t\t\t\tData: buf,\n\t\t\t\tErrors: ec.Errors,\n\t\t\t}\n\t\t}\n\t{{- else }}\n\t\treturn graphql.OneShot(graphql.ErrorResponse(ctx, \"subscriptions are not supported\"))\n\t{{- end }}\n}\n\ntype executionContext struct {\n\t*graphql.RequestContext\n\t*executableSchema\n}\n\n{{- range $object := .Objects }}\n\t{{ template \"object.gotpl\" $object }}\n\n\t{{- range $field := $object.Fields }}\n\t\t{{ template \"field.gotpl\" $field }}\n\t{{ end }}\n{{- end}}\n\n{{- range $interface := .Interfaces }}\n\t{{ template \"interface.gotpl\" $interface }}\n{{- end }}\n\n{{- range $input := .Inputs }}\n\t{{ template \"input.gotpl\" $input }}\n{{- end }}\n\nfunc (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tec.Error(ctx, ec.Recover(ctx, r))\n\t\t\tret = nil\n\t\t}\n\t}()\n\t{{- if .Directives }}\n\trctx := graphql.GetResolverContext(ctx)\n\tfor _, d := range rctx.Field.Definition.Directives {\n\t\tswitch d.Name {\n\t\t{{- range $directive := .Directives }}\n\t\tcase \"{{$directive.Name}}\":\n\t\t\tif ec.directives.{{$directive.Name|ucFirst}} != nil {\n\t\t\t\t{{- if $directive.Args }}\n\t\t\t\t\trawArgs := d.ArgumentMap(ec.Variables)\n\t\t\t\t\t{{ template \"args.gotpl\" $directive.Args }}\n\t\t\t\t{{- end }}\n\t\t\t\tn := next\n\t\t\t\tnext = func(ctx context.Context) (interface{}, error) {\n\t\t\t\t\treturn ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})\n\t\t\t\t}\n\t\t\t}\n\t\t{{- end }}\n\t\t}\n\t}\n\t{{- end }}\n\tres, err := ec.ResolverMiddleware(ctx, next)\n\tif err != nil {\n\t\tec.Error(ctx, err)\n\t\treturn nil\n\t}\n\treturn res\n}\n\nfunc (ec *executionContext) introspectSchema() *introspection.Schema {\n\treturn introspection.WrapSchema(parsedSchema)\n}\n\nfunc (ec *executionContext) introspectType(name string) *introspection.Type {\n\treturn introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name])\n}\n\nvar parsedSchema = gqlparser.MustLoadSchema(\n\t&ast.Source{Name: {{.SchemaFilename|quote}}, Input: {{.SchemaRaw|rawQuote}}},\n)\n", "input.gotpl": "\t{{- if .IsMarshaled }}\n\tfunc Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) {\n\t\tvar it {{.FullName}}\n\t\tvar asMap = v.(map[string]interface{})\n\t\t{{ range $field := .Fields}}\n\t\t\t{{- if $field.Default}}\n\t\t\t\tif _, present := asMap[{{$field.GQLName|quote}}] ; !present {\n\t\t\t\t\tasMap[{{$field.GQLName|quote}}] = {{ $field.Default | dump }}\n\t\t\t\t}\n\t\t\t{{- end}}\n\t\t{{- end }}\n\n\t\tfor k, v := range asMap {\n\t\t\tswitch k {\n\t\t\t{{- range $field := .Fields }}\n\t\t\tcase {{$field.GQLName|quote}}:\n\t\t\t\tvar err error\n\t\t\t\t{{ $field.Unmarshal (print \"it.\" $field.GoFieldName) \"v\" }}\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn it, err\n\t\t\t\t}\n\t\t\t{{- end }}\n\t\t\t}\n\t\t}\n\n\t\treturn it, nil\n\t}\n\t{{- end }}\n", "interface.gotpl": "{{- $interface := . }}\n\nfunc (ec *executionContext) _{{$interface.GQLType}}(ctx context.Context, sel ast.SelectionSet, obj *{{$interface.FullName}}) graphql.Marshaler {\n\tswitch obj := (*obj).(type) {\n\tcase nil:\n\t\treturn graphql.Null\n\t{{- range $implementor := $interface.Implementors }}\n\t\t{{- if $implementor.ValueReceiver }}\n\t\t\tcase {{$implementor.FullName}}:\n\t\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, &obj)\n\t\t{{- end}}\n\t\tcase *{{$implementor.FullName}}:\n\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, obj)\n\t{{- end }}\n\tdefault:\n\t\tpanic(fmt.Errorf(\"unexpected type %T\", obj))\n\t}\n}\n", - "models.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n{{ range $model := .Models }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\t{{- if .IsInterface }}\n\t\ttype {{.GoType}} interface {}\n\t{{- else }}\n\t\ttype {{.GoType}} struct {\n\t\t\t{{- range $field := .Fields }}\n\t\t\t\t{{- if $field.GoFieldName }}\n\t\t\t\t\t{{ $field.GoFieldName }} {{$field.Signature}} `json:\"{{$field.GQLName}}\"`\n\t\t\t\t{{- else }}\n\t\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t}\n\t{{- end }}\n{{- end}}\n\n{{ range $enum := .Enums }}\n\t{{with .Description}}{{.|prefixLines \"// \"}} {{end}}\n\ttype {{.GoType}} string\n\tconst (\n\t{{- range $value := .Values}}\n\t\t{{- with .Description}}\n\t\t\t{{.|prefixLines \"// \"}}\n\t\t{{- end}}\n\t\t{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}\n\t{{- end }}\n\t)\n\n\tfunc (e {{.GoType}}) IsValid() bool {\n\t\tswitch e {\n\t\tcase {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tfunc (e {{.GoType}}) String() string {\n\t\treturn string(e)\n\t}\n\n\tfunc (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {\n\t\tstr, ok := v.(string)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"enums must be strings\")\n\t\t}\n\n\t\t*e = {{.GoType}}(str)\n\t\tif !e.IsValid() {\n\t\t\treturn fmt.Errorf(\"%s is not a valid {{.GQLType}}\", str)\n\t\t}\n\t\treturn nil\n\t}\n\n\tfunc (e {{.GoType}}) MarshalGQL(w io.Writer) {\n\t\tfmt.Fprint(w, strconv.Quote(e.String()))\n\t}\n\n{{- end }}\n", + "models.gotpl": "// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n{{ range $model := .Models }}\n\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\t{{- if .IsInterface }}\n\t\ttype {{.GoType}} interface {}\n\t{{- else }}\n\t\ttype {{.GoType}} struct {\n\t\t\t{{- range $field := .Fields }}\n\t\t\t\t{{- with .Description}}\n\t\t\t\t\t{{.|prefixLines \"// \"}}\n\t\t\t\t{{- end}}\n\t\t\t\t{{- if $field.GoFieldName }}\n\t\t\t\t\t{{ $field.GoFieldName }} {{$field.Signature}} `json:\"{{$field.GQLName}}\"`\n\t\t\t\t{{- else }}\n\t\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t}\n\t{{- end }}\n{{- end}}\n\n{{ range $enum := .Enums }}\n\t{{with .Description}}{{.|prefixLines \"// \"}} {{end}}\n\ttype {{.GoType}} string\n\tconst (\n\t{{- range $value := .Values}}\n\t\t{{- with .Description}}\n\t\t\t{{.|prefixLines \"// \"}}\n\t\t{{- end}}\n\t\t{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}\n\t{{- end }}\n\t)\n\n\tfunc (e {{.GoType}}) IsValid() bool {\n\t\tswitch e {\n\t\tcase {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tfunc (e {{.GoType}}) String() string {\n\t\treturn string(e)\n\t}\n\n\tfunc (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {\n\t\tstr, ok := v.(string)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"enums must be strings\")\n\t\t}\n\n\t\t*e = {{.GoType}}(str)\n\t\tif !e.IsValid() {\n\t\t\treturn fmt.Errorf(\"%s is not a valid {{.GQLType}}\", str)\n\t\t}\n\t\treturn nil\n\t}\n\n\tfunc (e {{.GoType}}) MarshalGQL(w io.Writer) {\n\t\tfmt.Fprint(w, strconv.Quote(e.String()))\n\t}\n\n{{- end }}\n", "object.gotpl": "{{ $object := . }}\n\nvar {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}}\n\n// nolint: gocyclo, errcheck, gas, goconst\n{{- if .Stream }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet) func() graphql.Marshaler {\n\tfields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)\n\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\tObject: {{$object.GQLType|quote}},\n\t})\n\tif len(fields) != 1 {\n\t\tec.Errorf(ctx, \"must subscribe to exactly one stream\")\n\t\treturn nil\n\t}\n\n\tswitch fields[0].Name {\n\t{{- range $field := $object.Fields }}\n\tcase \"{{$field.GQLName}}\":\n\t\treturn ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, fields[0])\n\t{{- end }}\n\tdefault:\n\t\tpanic(\"unknown field \" + strconv.Quote(fields[0].Name))\n\t}\n}\n{{- else }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet{{if not $object.Root}}, obj *{{$object.FullName}} {{end}}) graphql.Marshaler {\n\tfields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)\n\t{{if $object.Root}}\n\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\t\tObject: {{$object.GQLType|quote}},\n\t\t})\n\t{{end}}\n\n\t{{if $object.IsConcurrent}} var wg sync.WaitGroup {{end}}\n\tout := graphql.NewOrderedMap(len(fields))\n\tinvalid := false\n\tfor i, field := range fields {\n\t\tout.Keys[i] = field.Alias\n\n\t\tswitch field.Name {\n\t\tcase \"__typename\":\n\t\t\tout.Values[i] = graphql.MarshalString({{$object.GQLType|quote}})\n\t\t{{- range $field := $object.Fields }}\n\t\tcase \"{{$field.GQLName}}\":\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\twg.Add(1)\n\t\t\t\tgo func(i int, field graphql.CollectedField) {\n\t\t\t{{- end }}\n\t\t\t\tout.Values[i] = ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, field{{if not $object.Root}}, obj{{end}})\n\t\t\t\t{{- if $field.ASTType.NonNull }}\n\t\t\t\t\tif out.Values[i] == graphql.Null {\n\t\t\t\t\t\tinvalid = true\n\t\t\t\t\t}\n\t\t\t\t{{- end }}\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\t\twg.Done()\n\t\t\t\t}(i, field)\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t\tdefault:\n\t\t\tpanic(\"unknown field \" + strconv.Quote(field.Name))\n\t\t}\n\t}\n\t{{if $object.IsConcurrent}} wg.Wait() {{end}}\n\tif invalid { return graphql.Null }\n\treturn out\n}\n{{- end }}\n", "resolver.gotpl": "//go:generate gorunpkg github.com/99designs/gqlgen\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\ntype {{.ResolverType}} struct {}\n\n{{ range $object := .Objects -}}\n\t{{- if $object.HasResolvers -}}\n\t\tfunc (r *{{$.ResolverType}}) {{$object.GQLType}}() {{ $object.ResolverInterface.FullName }} {\n\t\t\treturn &{{lcFirst $object.GQLType}}Resolver{r}\n\t\t}\n\t{{ end -}}\n{{ end }}\n\n{{ range $object := .Objects -}}\n\t{{- if $object.HasResolvers -}}\n\t\ttype {{lcFirst $object.GQLType}}Resolver struct { *Resolver }\n\n\t\t{{ range $field := $object.Fields -}}\n\t\t\t{{- if $field.IsResolver -}}\n\t\t\tfunc (r *{{lcFirst $object.GQLType}}Resolver) {{ $field.ShortResolverDeclaration }} {\n\t\t\t\tpanic(\"not implemented\")\n\t\t\t}\n\t\t\t{{ end -}}\n\t\t{{ end -}}\n\t{{ end -}}\n{{ end }}\n", "server.gotpl": "package main\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\nconst defaultPort = \"8080\"\n\nfunc main() {\n\tport := os.Getenv(\"PORT\")\n\tif port == \"\" {\n\t\tport = defaultPort\n\t}\n\n\thttp.Handle(\"/\", handler.Playground(\"GraphQL playground\", \"/query\"))\n\thttp.Handle(\"/query\", handler.GraphQL({{.ExecPackageName}}.NewExecutableSchema({{.ExecPackageName}}.Config{Resolvers: &{{.ResolverPackageName}}.Resolver{}})))\n\n\tlog.Printf(\"connect to http://localhost:%s/ for GraphQL playground\", port)\n\tlog.Fatal(http.ListenAndServe(\":\" + port, nil))\n}\n", diff --git a/codegen/templates/models.gotpl b/codegen/templates/models.gotpl index d17b6f98f15..7427d71d7a4 100644 --- a/codegen/templates/models.gotpl +++ b/codegen/templates/models.gotpl @@ -15,6 +15,9 @@ import ( {{- else }} type {{.GoType}} struct { {{- range $field := .Fields }} + {{- with .Description}} + {{.|prefixLines "// "}} + {{- end}} {{- if $field.GoFieldName }} {{ $field.GoFieldName }} {{$field.Signature}} `json:"{{$field.GQLName}}"` {{- else }} From 82a28b5735f59524aa3f513f9c893a4c48b6d104 Mon Sep 17 00:00:00 2001 From: Evan Shaw Date: Tue, 28 Aug 2018 21:22:59 +1200 Subject: [PATCH 15/15] Only allow query operations on GET requests This mitigates the risk of CSRF attacks. Closes #317. --- handler/graphql.go | 5 +++++ handler/graphql_test.go | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/handler/graphql.go b/handler/graphql.go index ccb3b38748f..87c3e66bb25 100644 --- a/handler/graphql.go +++ b/handler/graphql.go @@ -206,6 +206,11 @@ func GraphQL(exec graphql.ExecutableSchema, options ...Option) http.HandlerFunc return } + if op.Operation != ast.Query && r.Method == http.MethodGet { + sendErrorf(w, http.StatusUnprocessableEntity, "GET requests only allow query operations") + return + } + vars, err := validator.VariableValues(exec.Schema(), op, reqParams.Variables) if err != nil { sendError(w, http.StatusUnprocessableEntity, err) diff --git a/handler/graphql_test.go b/handler/graphql_test.go index 4ace5d41314..685c9b994a8 100644 --- a/handler/graphql_test.go +++ b/handler/graphql_test.go @@ -75,6 +75,12 @@ func TestHandlerGET(t *testing.T) { assert.Equal(t, http.StatusUnprocessableEntity, resp.Code) assert.Equal(t, `{"data":null,"errors":[{"message":"Unexpected !","locations":[{"line":1,"column":1}]}]}`, resp.Body.String()) }) + + t.Run("no mutations", func(t *testing.T) { + resp := doRequest(h, "GET", "/graphql?query=mutation{me{name}}", "") + assert.Equal(t, http.StatusUnprocessableEntity, resp.Code) + assert.Equal(t, `{"data":null,"errors":[{"message":"GET requests only allow query operations"}]}`, resp.Body.String()) + }) } func TestHandlerOptions(t *testing.T) {