diff --git a/codegen/build.go b/codegen/build.go index fb915185c78..1ae60f8054a 100644 --- a/codegen/build.go +++ b/codegen/build.go @@ -21,6 +21,7 @@ type Build struct { SubscriptionRoot *Object SchemaRaw string SchemaFilename string + Directives []*Directive } type ModelBuild struct { @@ -84,6 +85,7 @@ func (cfg *Config) bind() (*Build, error) { Imports: imports.finalize(), SchemaRaw: cfg.SchemaStr, SchemaFilename: cfg.SchemaFilename, + Directives: cfg.buildDirectives(), } if cfg.schema.Query != nil { diff --git a/codegen/directive.go b/codegen/directive.go new file mode 100644 index 00000000000..aeeda1dcb63 --- /dev/null +++ b/codegen/directive.go @@ -0,0 +1,5 @@ +package codegen + +type Directive struct { + Name string +} diff --git a/codegen/directive_build.go b/codegen/directive_build.go new file mode 100644 index 00000000000..1d47d381432 --- /dev/null +++ b/codegen/directive_build.go @@ -0,0 +1,13 @@ +package codegen + +func (cfg *Config) buildDirectives() (directives []*Directive) { + for name := range cfg.schema.Directives { + if name == "skip" || name == "include" || name == "deprecated" { + continue + } + directives = append(directives, &Directive{ + Name: name, + }) + } + return directives +} diff --git a/codegen/templates/data.go b/codegen/templates/data.go index 52e4f880083..460c347659e 100644 --- a/codegen/templates/data.go +++ b/codegen/templates/data.go @@ -2,8 +2,8 @@ package templates var data = map[string]string{ "args.gotpl": "\t{{- if . }}args := map[string]interface{}{} {{end}}\n\t{{- range $i, $arg := . }}\n\t\tvar arg{{$i}} {{$arg.Signature }}\n\t\tif tmp, ok := field.Args[{{$arg.GQLName|quote}}]; ok {\n\t\t\tvar err error\n\t\t\t{{$arg.Unmarshal (print \"arg\" $i) \"tmp\" }}\n\t\t\tif err != nil {\n\t\t\t\tec.Error(ctx, err)\n\t\t\t\t{{- if $arg.Object.Stream }}\n\t\t\t\t\treturn nil\n\t\t\t\t{{- else }}\n\t\t\t\t\treturn graphql.Null\n\t\t\t\t{{- end }}\n\t\t\t}\n\t\t} {{ if $arg.Default }} else {\n\t\t\tvar tmp interface{} = {{ $arg.Default | dump }}\n\t\t\tvar err error\n\t\t\t{{$arg.Unmarshal (print \"arg\" $i) \"tmp\" }}\n\t\t\tif err != nil {\n\t\t\t\tec.Error(ctx, err)\n\t\t\t\t{{- if $arg.Object.Stream }}\n\t\t\t\t\treturn nil\n\t\t\t\t{{- else }}\n\t\t\t\t\treturn graphql.Null\n\t\t\t\t{{- end }}\n\t\t\t}\n\t\t}\n\t\t{{end }}\n\t\targs[{{$arg.GQLName|quote}}] = arg{{$i}}\n\t{{- end -}}\n", - "field.gotpl": "{{ $field := . }}\n{{ $object := $field.Object }}\n\n{{- if $object.Stream }}\n\tfunc (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField) func() graphql.Marshaler {\n\t\t{{- template \"args.gotpl\" $field.Args }}\n\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{Field: field})\n\t\tresults, err := ec.resolvers.{{ $field.ShortInvocation }}\n\t\tif err != nil {\n\t\t\tec.Error(ctx, err)\n\t\t\treturn nil\n\t\t}\n\t\treturn func() graphql.Marshaler {\n\t\t\tres, ok := <-results\n\t\t\tif !ok {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tvar out graphql.OrderedMap\n\t\t\tout.Add(field.Alias, func() graphql.Marshaler { {{ $field.WriteJson }} }())\n\t\t\treturn &out\n\t\t}\n\t}\n{{ else }}\n\tfunc (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField, {{if not $object.Root}}obj *{{$object.FullName}}{{end}}) graphql.Marshaler {\n\t\t{{- template \"args.gotpl\" $field.Args }}\n\n\t\t{{- if $field.IsConcurrent }}\n\t\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\t\t\tObject: {{$object.GQLType|quote}},\n\t\t\t\tArgs: {{if $field.Args }}args{{else}}nil{{end}},\n\t\t\t\tField: field,\n\t\t\t})\n\t\t\treturn graphql.Defer(func() (ret graphql.Marshaler) {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\t\tuserErr := ec.Recover(ctx, r)\n\t\t\t\t\t\tec.Error(ctx, userErr)\n\t\t\t\t\t\tret = graphql.Null\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t{{ else }}\n\t\t\trctx := graphql.GetResolverContext(ctx)\n\t\t\trctx.Object = {{$object.GQLType|quote}}\n\t\t\trctx.Args = {{if $field.Args }}args{{else}}nil{{end}}\n\t\t\trctx.Field = field\n\t\t\trctx.PushField(field.Alias)\n\t\t\tdefer rctx.Pop()\n\t\t{{- end }}\n\t\t\tresTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {\n\t\t\t\t{{- if $field.IsResolver }}\n\t\t\t\t\treturn ec.resolvers.{{ $field.ShortInvocation }}\n\t\t\t\t{{- else if $field.GoVarName }}\n\t\t\t\t\treturn obj.{{$field.GoVarName}}, nil\n\t\t\t\t{{- else if $field.GoMethodName }}\n\t\t\t\t\t{{- if $field.NoErr }}\n\t\t\t\t\t\treturn {{$field.GoMethodName}}({{ $field.CallArgs }}), nil\n\t\t\t\t\t{{- else }}\n\t\t\t\t\t\treturn {{$field.GoMethodName}}({{ $field.CallArgs }})\n\t\t\t\t\t{{- end }}\n\t\t\t\t{{- end }}\n\t\t\t})\n\t\t\tif err != nil {\n\t\t\t\tec.Error(ctx, err)\n\t\t\t\treturn graphql.Null\n\t\t\t}\n\t\t\tif resTmp == nil {\n\t\t\t\treturn graphql.Null\n\t\t\t}\n\t\t\tres := resTmp.({{$field.Signature}})\n\t\t\t{{ $field.WriteJson }}\n\t\t{{- if $field.IsConcurrent }}\n\t\t\t})\n\t\t{{- end }}\n\t}\n{{ end }}\n", - "generated.gotpl": "// Code generated by github.com/vektah/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(resolvers ResolverRoot) graphql.ExecutableSchema {\n\treturn &executableSchema{resolvers: resolvers}\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\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}\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.resolvers}\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.resolvers}\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.resolvers}\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\n\tresolvers ResolverRoot\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) 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", + "field.gotpl": "{{ $field := . }}\n{{ $object := $field.Object }}\n\n{{- if $object.Stream }}\n\tfunc (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField) func() graphql.Marshaler {\n\t\t{{- template \"args.gotpl\" $field.Args }}\n\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{Field: field})\n\t\tresults, err := ec.resolvers.{{ $field.ShortInvocation }}\n\t\tif err != nil {\n\t\t\tec.Error(ctx, err)\n\t\t\treturn nil\n\t\t}\n\t\treturn func() graphql.Marshaler {\n\t\t\tres, ok := <-results\n\t\t\tif !ok {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tvar out graphql.OrderedMap\n\t\t\tout.Add(field.Alias, func() graphql.Marshaler { {{ $field.WriteJson }} }())\n\t\t\treturn &out\n\t\t}\n\t}\n{{ else }}\n\tfunc (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField, {{if not $object.Root}}obj *{{$object.FullName}}{{end}}) graphql.Marshaler {\n\t\t{{- template \"args.gotpl\" $field.Args }}\n\n\t\t{{- if $field.IsConcurrent }}\n\t\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\t\t\tObject: {{$object.GQLType|quote}},\n\t\t\t\tArgs: {{if $field.Args }}args{{else}}nil{{end}},\n\t\t\t\tField: field,\n\t\t\t})\n\t\t\treturn graphql.Defer(func() (ret graphql.Marshaler) {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\t\tuserErr := ec.Recover(ctx, r)\n\t\t\t\t\t\tec.Error(ctx, userErr)\n\t\t\t\t\t\tret = graphql.Null\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t{{ else }}\n\t\t\trctx := graphql.GetResolverContext(ctx)\n\t\t\trctx.Object = {{$object.GQLType|quote}}\n\t\t\trctx.Args = {{if $field.Args }}args{{else}}nil{{end}}\n\t\t\trctx.Field = field\n\t\t\trctx.PushField(field.Alias)\n\t\t\tdefer rctx.Pop()\n\t\t{{- end }}\n\t\t\tresTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) {\n\t\t\t\t{{- if $field.IsResolver }}\n\t\t\t\t\treturn ec.resolvers.{{ $field.ShortInvocation }}\n\t\t\t\t{{- else if $field.GoVarName }}\n\t\t\t\t\treturn obj.{{$field.GoVarName}}, nil\n\t\t\t\t{{- else if $field.GoMethodName }}\n\t\t\t\t\t{{- if $field.NoErr }}\n\t\t\t\t\t\treturn {{$field.GoMethodName}}({{ $field.CallArgs }}), nil\n\t\t\t\t\t{{- else }}\n\t\t\t\t\t\treturn {{$field.GoMethodName}}({{ $field.CallArgs }})\n\t\t\t\t\t{{- end }}\n\t\t\t\t{{- end }}\n\t\t\t})\n\t\t\tif resTmp == nil {\n\t\t\t\treturn graphql.Null\n\t\t\t}\n\t\t\tres := resTmp.({{$field.Signature}})\n\t\t\t{{ $field.WriteJson }}\n\t\t{{- if $field.IsConcurrent }}\n\t\t\t})\n\t\t{{- end }}\n\t}\n{{ end }}\n", + "generated.gotpl": "// Code generated by github.com/vektah/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.Name|ucFirst}} graphql.FieldMiddleware\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, next graphql.Resolver) interface{} {\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\tn := next\n\t\t\t\tnext = func(ctx context.Context) (interface{}, error) {\n\t\t\t\t\treturn ec.directives.{{$directive.Name|ucFirst}}(ctx, n)\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.GoVarName) \"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/vektah/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.GoVarName }}\n\t\t\t\t\t{{ $field.GoVarName }} {{$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", diff --git a/codegen/templates/field.gotpl b/codegen/templates/field.gotpl index b9c3e6c9e29..428a1df44fb 100644 --- a/codegen/templates/field.gotpl +++ b/codegen/templates/field.gotpl @@ -46,7 +46,7 @@ rctx.PushField(field.Alias) defer rctx.Pop() {{- end }} - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { {{- if $field.IsResolver }} return ec.resolvers.{{ $field.ShortInvocation }} {{- else if $field.GoVarName }} @@ -59,10 +59,6 @@ {{- end }} {{- end }} }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } diff --git a/codegen/templates/generated.gotpl b/codegen/templates/generated.gotpl index 415e5d42e48..cdeaba69d5e 100644 --- a/codegen/templates/generated.gotpl +++ b/codegen/templates/generated.gotpl @@ -9,8 +9,16 @@ import ( ) // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. -func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { - return &executableSchema{resolvers: resolvers} +func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { + return &executableSchema{ + resolvers: cfg.Resolvers, + directives: cfg.Directives, + } +} + +type Config struct { + Resolvers ResolverRoot + Directives DirectiveRoot } type ResolverRoot interface { @@ -21,6 +29,12 @@ type ResolverRoot interface { {{- end }} } +type DirectiveRoot struct { +{{ range $directive := .Directives }} + {{$directive.Name|ucFirst}} graphql.FieldMiddleware +{{ end }} +} + {{- range $object := .Objects -}} {{ if $object.HasResolvers }} type {{$object.GQLType}}Resolver interface { @@ -32,7 +46,8 @@ type ResolverRoot interface { {{- end }} type executableSchema struct { - resolvers ResolverRoot + resolvers ResolverRoot + directives DirectiveRoot } func (e *executableSchema) Schema() *ast.Schema { @@ -41,7 +56,7 @@ func (e *executableSchema) Schema() *ast.Schema { func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { {{- if .QueryRoot }} - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._{{.QueryRoot.GQLType}}(ctx, op.SelectionSet) @@ -61,7 +76,7 @@ func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinitio func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { {{- if .MutationRoot }} - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._{{.MutationRoot.GQLType}}(ctx, op.SelectionSet) @@ -81,7 +96,7 @@ func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefini func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response { {{- if .SubscriptionRoot }} - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} next := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.SelectionSet) if ec.Errors != nil { @@ -113,8 +128,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe type executionContext struct { *graphql.RequestContext - - resolvers ResolverRoot + *executableSchema } {{- range $object := .Objects }} @@ -133,6 +147,31 @@ type executionContext struct { {{ template "input.gotpl" $input }} {{- end }} +func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} { + {{- if .Directives }} + rctx := graphql.GetResolverContext(ctx) + for _, d := range rctx.Field.Definition.Directives { + switch d.Name { + {{- range $directive := .Directives }} + case "{{$directive.Name}}": + if ec.directives.{{$directive.Name|ucFirst}} != nil { + n := next + next = func(ctx context.Context) (interface{}, error) { + return ec.directives.{{$directive.Name|ucFirst}}(ctx, n) + } + } + {{- end }} + } + } + {{- end }} + res, err := ec.ResolverMiddleware(ctx, next) + if err != nil { + ec.Error(ctx, err) + return nil + } + return res +} + func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) } diff --git a/example/chat/generated.go b/example/chat/generated.go index 8f68078a6fa..51711c680fa 100644 --- a/example/chat/generated.go +++ b/example/chat/generated.go @@ -15,8 +15,16 @@ import ( ) // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. -func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { - return &executableSchema{resolvers: resolvers} +func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { + return &executableSchema{ + resolvers: cfg.Resolvers, + directives: cfg.Directives, + } +} + +type Config struct { + Resolvers ResolverRoot + Directives DirectiveRoot } type ResolverRoot interface { @@ -24,6 +32,9 @@ type ResolverRoot interface { Query() QueryResolver Subscription() SubscriptionResolver } + +type DirectiveRoot struct { +} type MutationResolver interface { Post(ctx context.Context, text string, username string, roomName string) (Message, error) } @@ -35,7 +46,8 @@ type SubscriptionResolver interface { } type executableSchema struct { - resolvers ResolverRoot + resolvers ResolverRoot + directives DirectiveRoot } func (e *executableSchema) Schema() *ast.Schema { @@ -43,7 +55,7 @@ func (e *executableSchema) Schema() *ast.Schema { } func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._Query(ctx, op.SelectionSet) @@ -59,7 +71,7 @@ func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinitio } func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._Mutation(ctx, op.SelectionSet) @@ -75,7 +87,7 @@ func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefini } func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} next := ec._Subscription(ctx, op.SelectionSet) if ec.Errors != nil { @@ -104,8 +116,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe type executionContext struct { *graphql.RequestContext - - resolvers ResolverRoot + *executableSchema } var chatroomImplementors = []string{"Chatroom"} @@ -140,13 +151,9 @@ func (ec *executionContext) _Chatroom_name(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -161,13 +168,9 @@ func (ec *executionContext) _Chatroom_messages(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Messages, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -220,13 +223,9 @@ func (ec *executionContext) _Message_id(ctx context.Context, field graphql.Colle rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -241,13 +240,9 @@ func (ec *executionContext) _Message_text(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Text, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -262,13 +257,9 @@ func (ec *executionContext) _Message_createdBy(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.CreatedBy, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -283,13 +274,9 @@ func (ec *executionContext) _Message_createdAt(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.CreatedAt, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -362,13 +349,9 @@ func (ec *executionContext) _Mutation_post(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Mutation().Post(ctx, args["text"].(string), args["username"].(string), args["roomName"].(string)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -433,13 +416,9 @@ func (ec *executionContext) _Query_room(ctx context.Context, field graphql.Colle } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Room(ctx, args["name"].(string)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -469,13 +448,9 @@ func (ec *executionContext) _Query___type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectType(args["name"].(string)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -493,13 +468,9 @@ func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectSchema(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -596,13 +567,9 @@ func (ec *executionContext) ___Directive_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -617,13 +584,9 @@ func (ec *executionContext) ___Directive_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -638,13 +601,9 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Locations, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -668,13 +627,9 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -727,13 +682,9 @@ func (ec *executionContext) ___EnumValue_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -748,13 +699,9 @@ func (ec *executionContext) ___EnumValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -769,13 +716,9 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -790,13 +733,9 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -844,13 +783,9 @@ func (ec *executionContext) ___Field_name(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -865,13 +800,9 @@ func (ec *executionContext) ___Field_description(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -886,13 +817,9 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -916,13 +843,9 @@ func (ec *executionContext) ___Field_type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -940,13 +863,9 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -961,13 +880,9 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1011,13 +926,9 @@ func (ec *executionContext) ___InputValue_name(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1032,13 +943,9 @@ func (ec *executionContext) ___InputValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1053,13 +960,9 @@ func (ec *executionContext) ___InputValue_type(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1077,13 +980,9 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DefaultValue, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1129,13 +1028,9 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Types(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1159,13 +1054,9 @@ func (ec *executionContext) ___Schema_queryType(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.QueryType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1183,13 +1074,9 @@ func (ec *executionContext) ___Schema_mutationType(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.MutationType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1207,13 +1094,9 @@ func (ec *executionContext) ___Schema_subscriptionType(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.SubscriptionType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1231,13 +1114,9 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Directives(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1300,13 +1179,9 @@ func (ec *executionContext) ___Type_kind(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Kind(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1321,13 +1196,9 @@ func (ec *executionContext) ___Type_name(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1342,13 +1213,9 @@ func (ec *executionContext) ___Type_description(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1374,13 +1241,9 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Fields(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1404,13 +1267,9 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Interfaces(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1434,13 +1293,9 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.PossibleTypes(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1475,13 +1330,9 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.EnumValues(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1505,13 +1356,9 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.InputFields(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1535,13 +1382,9 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.OfType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1552,6 +1395,15 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co return ec.___Type(ctx, field.Selections, res) } +func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} { + res, err := ec.ResolverMiddleware(ctx, next) + if err != nil { + ec.Error(ctx, err) + return nil + } + return res +} + func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) } diff --git a/example/chat/resolvers.go b/example/chat/resolvers.go index f9f8ee3f083..8807cbd0722 100644 --- a/example/chat/resolvers.go +++ b/example/chat/resolvers.go @@ -26,9 +26,11 @@ func (r *resolver) Subscription() SubscriptionResolver { return &subscriptionResolver{r} } -func New() *resolver { - return &resolver{ - Rooms: map[string]*Chatroom{}, +func New() Config { + return Config{ + Resolvers: &resolver{ + Rooms: map[string]*Chatroom{}, + }, } } diff --git a/example/dataloader/dataloader_test.go b/example/dataloader/dataloader_test.go index 4948feacc48..f866f64af53 100644 --- a/example/dataloader/dataloader_test.go +++ b/example/dataloader/dataloader_test.go @@ -11,7 +11,7 @@ import ( ) func TestTodo(t *testing.T) { - srv := httptest.NewServer(LoaderMiddleware(handler.GraphQL(NewExecutableSchema(&Resolver{})))) + srv := httptest.NewServer(LoaderMiddleware(handler.GraphQL(NewExecutableSchema(Config{Resolvers: &Resolver{}})))) c := client.New(srv.URL) t.Run("create a new todo", func(t *testing.T) { diff --git a/example/dataloader/generated.go b/example/dataloader/generated.go index 3eae1a84170..21b4abdc7fe 100644 --- a/example/dataloader/generated.go +++ b/example/dataloader/generated.go @@ -15,8 +15,16 @@ import ( ) // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. -func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { - return &executableSchema{resolvers: resolvers} +func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { + return &executableSchema{ + resolvers: cfg.Resolvers, + directives: cfg.Directives, + } +} + +type Config struct { + Resolvers ResolverRoot + Directives DirectiveRoot } type ResolverRoot interface { @@ -24,6 +32,9 @@ type ResolverRoot interface { Order() OrderResolver Query() QueryResolver } + +type DirectiveRoot struct { +} type CustomerResolver interface { Address(ctx context.Context, obj *Customer) (*Address, error) Orders(ctx context.Context, obj *Customer) ([]Order, error) @@ -37,7 +48,8 @@ type QueryResolver interface { } type executableSchema struct { - resolvers ResolverRoot + resolvers ResolverRoot + directives DirectiveRoot } func (e *executableSchema) Schema() *ast.Schema { @@ -45,7 +57,7 @@ func (e *executableSchema) Schema() *ast.Schema { } func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._Query(ctx, op.SelectionSet) @@ -70,8 +82,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe type executionContext struct { *graphql.RequestContext - - resolvers ResolverRoot + *executableSchema } var addressImplementors = []string{"Address"} @@ -108,13 +119,9 @@ func (ec *executionContext) _Address_id(ctx context.Context, field graphql.Colle rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -129,13 +136,9 @@ func (ec *executionContext) _Address_street(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Street, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -150,13 +153,9 @@ func (ec *executionContext) _Address_country(ctx context.Context, field graphql. rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Country, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -200,13 +199,9 @@ func (ec *executionContext) _Customer_id(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -221,13 +216,9 @@ func (ec *executionContext) _Customer_name(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -250,13 +241,9 @@ func (ec *executionContext) _Customer_address(ctx context.Context, field graphql } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Customer().Address(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -283,13 +270,9 @@ func (ec *executionContext) _Customer_orders(ctx context.Context, field graphql. } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Customer().Orders(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -337,13 +320,9 @@ func (ec *executionContext) _Item_name(ctx context.Context, field graphql.Collec rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -387,13 +366,9 @@ func (ec *executionContext) _Order_id(ctx context.Context, field graphql.Collect rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -408,13 +383,9 @@ func (ec *executionContext) _Order_date(ctx context.Context, field graphql.Colle rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Date, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -429,13 +400,9 @@ func (ec *executionContext) _Order_amount(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Amount, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -458,13 +425,9 @@ func (ec *executionContext) _Order_items(ctx context.Context, field graphql.Coll } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Order().Items(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -530,13 +493,9 @@ func (ec *executionContext) _Query_customers(ctx context.Context, field graphql. } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Customers(ctx) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -598,13 +557,9 @@ func (ec *executionContext) _Query_torture(ctx context.Context, field graphql.Co } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Torture(ctx, args["customerIds"].([][]int)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -649,13 +604,9 @@ func (ec *executionContext) _Query___type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectType(args["name"].(string)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -673,13 +624,9 @@ func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectSchema(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -726,13 +673,9 @@ func (ec *executionContext) ___Directive_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -747,13 +690,9 @@ func (ec *executionContext) ___Directive_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -768,13 +707,9 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Locations, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -798,13 +733,9 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -857,13 +788,9 @@ func (ec *executionContext) ___EnumValue_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -878,13 +805,9 @@ func (ec *executionContext) ___EnumValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -899,13 +822,9 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -920,13 +839,9 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -974,13 +889,9 @@ func (ec *executionContext) ___Field_name(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -995,13 +906,9 @@ func (ec *executionContext) ___Field_description(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1016,13 +923,9 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1046,13 +949,9 @@ func (ec *executionContext) ___Field_type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1070,13 +969,9 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1091,13 +986,9 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1141,13 +1032,9 @@ func (ec *executionContext) ___InputValue_name(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1162,13 +1049,9 @@ func (ec *executionContext) ___InputValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1183,13 +1066,9 @@ func (ec *executionContext) ___InputValue_type(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1207,13 +1086,9 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DefaultValue, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1259,13 +1134,9 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Types(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1289,13 +1160,9 @@ func (ec *executionContext) ___Schema_queryType(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.QueryType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1313,13 +1180,9 @@ func (ec *executionContext) ___Schema_mutationType(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.MutationType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1337,13 +1200,9 @@ func (ec *executionContext) ___Schema_subscriptionType(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.SubscriptionType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1361,13 +1220,9 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Directives(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1430,13 +1285,9 @@ func (ec *executionContext) ___Type_kind(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Kind(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1451,13 +1302,9 @@ func (ec *executionContext) ___Type_name(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1472,13 +1319,9 @@ func (ec *executionContext) ___Type_description(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1504,13 +1347,9 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Fields(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1534,13 +1373,9 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Interfaces(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1564,13 +1399,9 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.PossibleTypes(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1605,13 +1436,9 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.EnumValues(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1635,13 +1462,9 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.InputFields(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1665,13 +1488,9 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.OfType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1682,6 +1501,15 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co return ec.___Type(ctx, field.Selections, res) } +func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} { + res, err := ec.ResolverMiddleware(ctx, next) + if err != nil { + ec.Error(ctx, err) + return nil + } + return res +} + func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) } diff --git a/example/dataloader/server/server.go b/example/dataloader/server/server.go index 5f1a501fa4a..3666d72696f 100644 --- a/example/dataloader/server/server.go +++ b/example/dataloader/server/server.go @@ -26,7 +26,7 @@ func main() { router.Handle("/", handler.Playground("Dataloader", "/query")) router.Handle("/query", handler.GraphQL( - dataloader.NewExecutableSchema(&dataloader.Resolver{}), + dataloader.NewExecutableSchema(dataloader.Config{Resolvers: &dataloader.Resolver{}}), handler.ResolverMiddleware(gqlopentracing.ResolverMiddleware()), handler.RequestMiddleware(gqlopentracing.RequestMiddleware()), )) diff --git a/example/scalars/generated.go b/example/scalars/generated.go index f29f6285934..2fc65759141 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -17,14 +17,25 @@ import ( ) // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. -func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { - return &executableSchema{resolvers: resolvers} +func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { + return &executableSchema{ + resolvers: cfg.Resolvers, + directives: cfg.Directives, + } +} + +type Config struct { + Resolvers ResolverRoot + Directives DirectiveRoot } type ResolverRoot interface { Query() QueryResolver User() UserResolver } + +type DirectiveRoot struct { +} type QueryResolver interface { User(ctx context.Context, id external.ObjectID) (*model.User, error) Search(ctx context.Context, input model.SearchArgs) ([]model.User, error) @@ -35,7 +46,8 @@ type UserResolver interface { } type executableSchema struct { - resolvers ResolverRoot + resolvers ResolverRoot + directives DirectiveRoot } func (e *executableSchema) Schema() *ast.Schema { @@ -43,7 +55,7 @@ func (e *executableSchema) Schema() *ast.Schema { } func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._Query(ctx, op.SelectionSet) @@ -68,8 +80,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe type executionContext struct { *graphql.RequestContext - - resolvers ResolverRoot + *executableSchema } var addressImplementors = []string{"Address"} @@ -104,13 +115,9 @@ func (ec *executionContext) _Address_id(ctx context.Context, field graphql.Colle rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -125,13 +132,9 @@ func (ec *executionContext) _Address_location(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Location, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -201,13 +204,9 @@ func (ec *executionContext) _Query_user(ctx context.Context, field graphql.Colle } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().User(ctx, args["id"].(external.ObjectID)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -254,13 +253,9 @@ func (ec *executionContext) _Query_search(ctx context.Context, field graphql.Col } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Search(ctx, args["input"].(model.SearchArgs)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -296,13 +291,9 @@ func (ec *executionContext) _Query___type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectType(args["name"].(string)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -320,13 +311,9 @@ func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectSchema(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -381,13 +368,9 @@ func (ec *executionContext) _User_id(ctx context.Context, field graphql.Collecte rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -402,13 +385,9 @@ func (ec *executionContext) _User_name(ctx context.Context, field graphql.Collec rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -423,13 +402,9 @@ func (ec *executionContext) _User_created(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Created, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -444,13 +419,9 @@ func (ec *executionContext) _User_isBanned(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsBanned, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -473,13 +444,9 @@ func (ec *executionContext) _User_primitiveResolver(ctx context.Context, field g } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.User().PrimitiveResolver(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -503,13 +470,9 @@ func (ec *executionContext) _User_customResolver(ctx context.Context, field grap } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.User().CustomResolver(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -525,13 +488,9 @@ func (ec *executionContext) _User_address(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Address, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -546,13 +505,9 @@ func (ec *executionContext) _User_tier(ctx context.Context, field graphql.Collec rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Tier, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -596,13 +551,9 @@ func (ec *executionContext) ___Directive_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -617,13 +568,9 @@ func (ec *executionContext) ___Directive_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -638,13 +585,9 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Locations, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -668,13 +611,9 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -727,13 +666,9 @@ func (ec *executionContext) ___EnumValue_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -748,13 +683,9 @@ func (ec *executionContext) ___EnumValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -769,13 +700,9 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -790,13 +717,9 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -844,13 +767,9 @@ func (ec *executionContext) ___Field_name(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -865,13 +784,9 @@ func (ec *executionContext) ___Field_description(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -886,13 +801,9 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -916,13 +827,9 @@ func (ec *executionContext) ___Field_type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -940,13 +847,9 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -961,13 +864,9 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1011,13 +910,9 @@ func (ec *executionContext) ___InputValue_name(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1032,13 +927,9 @@ func (ec *executionContext) ___InputValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1053,13 +944,9 @@ func (ec *executionContext) ___InputValue_type(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1077,13 +964,9 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DefaultValue, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1129,13 +1012,9 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Types(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1159,13 +1038,9 @@ func (ec *executionContext) ___Schema_queryType(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.QueryType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1183,13 +1058,9 @@ func (ec *executionContext) ___Schema_mutationType(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.MutationType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1207,13 +1078,9 @@ func (ec *executionContext) ___Schema_subscriptionType(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.SubscriptionType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1231,13 +1098,9 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Directives(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1300,13 +1163,9 @@ func (ec *executionContext) ___Type_kind(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Kind(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1321,13 +1180,9 @@ func (ec *executionContext) ___Type_name(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1342,13 +1197,9 @@ func (ec *executionContext) ___Type_description(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1374,13 +1225,9 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Fields(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1404,13 +1251,9 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Interfaces(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1434,13 +1277,9 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.PossibleTypes(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1475,13 +1314,9 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.EnumValues(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1505,13 +1340,9 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.InputFields(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1535,13 +1366,9 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.OfType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1595,6 +1422,15 @@ func UnmarshalSearchArgs(v interface{}) (model.SearchArgs, error) { return it, nil } +func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} { + res, err := ec.ResolverMiddleware(ctx, next) + if err != nil { + ec.Error(ctx, err) + return nil + } + return res +} + func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) } diff --git a/example/scalars/scalar_test.go b/example/scalars/scalar_test.go index 7e07e096858..0ec5b72f34a 100644 --- a/example/scalars/scalar_test.go +++ b/example/scalars/scalar_test.go @@ -22,7 +22,7 @@ type RawUser struct { } func TestScalars(t *testing.T) { - srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(&Resolver{}))) + srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: &Resolver{}}))) c := client.New(srv.URL) t.Run("marshaling", func(t *testing.T) { diff --git a/example/scalars/server/server.go b/example/scalars/server/server.go index 748213b97ec..91062734260 100644 --- a/example/scalars/server/server.go +++ b/example/scalars/server/server.go @@ -10,7 +10,7 @@ import ( func main() { http.Handle("/", handler.Playground("Starwars", "/query")) - http.Handle("/query", handler.GraphQL(scalars.NewExecutableSchema(&scalars.Resolver{}))) + http.Handle("/query", handler.GraphQL(scalars.NewExecutableSchema(scalars.Config{Resolvers: &scalars.Resolver{}}))) log.Fatal(http.ListenAndServe(":8084", nil)) } diff --git a/example/selection/generated.go b/example/selection/generated.go index 007d74a2ceb..db02424f8cb 100644 --- a/example/selection/generated.go +++ b/example/selection/generated.go @@ -16,19 +16,31 @@ import ( ) // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. -func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { - return &executableSchema{resolvers: resolvers} +func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { + return &executableSchema{ + resolvers: cfg.Resolvers, + directives: cfg.Directives, + } +} + +type Config struct { + Resolvers ResolverRoot + Directives DirectiveRoot } type ResolverRoot interface { Query() QueryResolver } + +type DirectiveRoot struct { +} type QueryResolver interface { Events(ctx context.Context) ([]Event, error) } type executableSchema struct { - resolvers ResolverRoot + resolvers ResolverRoot + directives DirectiveRoot } func (e *executableSchema) Schema() *ast.Schema { @@ -36,7 +48,7 @@ func (e *executableSchema) Schema() *ast.Schema { } func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._Query(ctx, op.SelectionSet) @@ -61,8 +73,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe type executionContext struct { *graphql.RequestContext - - resolvers ResolverRoot + *executableSchema } var likeImplementors = []string{"Like", "Event"} @@ -101,13 +112,9 @@ func (ec *executionContext) _Like_reaction(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Reaction, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -122,13 +129,9 @@ func (ec *executionContext) _Like_sent(ctx context.Context, field graphql.Collec rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Sent, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -143,13 +146,9 @@ func (ec *executionContext) _Like_selection(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Selection, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -173,13 +172,9 @@ func (ec *executionContext) _Like_collected(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Collected, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -232,13 +227,9 @@ func (ec *executionContext) _Post_message(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Message, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -253,13 +244,9 @@ func (ec *executionContext) _Post_sent(ctx context.Context, field graphql.Collec rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Sent, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -274,13 +261,9 @@ func (ec *executionContext) _Post_selection(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Selection, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -304,13 +287,9 @@ func (ec *executionContext) _Post_collected(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Collected, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -373,13 +352,9 @@ func (ec *executionContext) _Query_events(ctx context.Context, field graphql.Col } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Events(ctx) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -415,13 +390,9 @@ func (ec *executionContext) _Query___type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectType(args["name"].(string)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -439,13 +410,9 @@ func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectSchema(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -492,13 +459,9 @@ func (ec *executionContext) ___Directive_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -513,13 +476,9 @@ func (ec *executionContext) ___Directive_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -534,13 +493,9 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Locations, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -564,13 +519,9 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -623,13 +574,9 @@ func (ec *executionContext) ___EnumValue_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -644,13 +591,9 @@ func (ec *executionContext) ___EnumValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -665,13 +608,9 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -686,13 +625,9 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -740,13 +675,9 @@ func (ec *executionContext) ___Field_name(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -761,13 +692,9 @@ func (ec *executionContext) ___Field_description(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -782,13 +709,9 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -812,13 +735,9 @@ func (ec *executionContext) ___Field_type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -836,13 +755,9 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -857,13 +772,9 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -907,13 +818,9 @@ func (ec *executionContext) ___InputValue_name(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -928,13 +835,9 @@ func (ec *executionContext) ___InputValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -949,13 +852,9 @@ func (ec *executionContext) ___InputValue_type(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -973,13 +872,9 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DefaultValue, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1025,13 +920,9 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Types(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1055,13 +946,9 @@ func (ec *executionContext) ___Schema_queryType(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.QueryType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1079,13 +966,9 @@ func (ec *executionContext) ___Schema_mutationType(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.MutationType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1103,13 +986,9 @@ func (ec *executionContext) ___Schema_subscriptionType(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.SubscriptionType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1127,13 +1006,9 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Directives(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1196,13 +1071,9 @@ func (ec *executionContext) ___Type_kind(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Kind(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1217,13 +1088,9 @@ func (ec *executionContext) ___Type_name(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1238,13 +1105,9 @@ func (ec *executionContext) ___Type_description(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1270,13 +1133,9 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Fields(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1300,13 +1159,9 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Interfaces(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1330,13 +1185,9 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.PossibleTypes(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1371,13 +1222,9 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.EnumValues(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1401,13 +1248,9 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.InputFields(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1431,13 +1274,9 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.OfType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1465,6 +1304,15 @@ func (ec *executionContext) _Event(ctx context.Context, sel ast.SelectionSet, ob } } +func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} { + res, err := ec.ResolverMiddleware(ctx, next) + if err != nil { + ec.Error(ctx, err) + return nil + } + return res +} + func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) } diff --git a/example/selection/selection_test.go b/example/selection/selection_test.go index dc3a199549e..fd4b320ec36 100644 --- a/example/selection/selection_test.go +++ b/example/selection/selection_test.go @@ -11,7 +11,7 @@ import ( ) func TestSelection(t *testing.T) { - srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(&Resolver{}))) + srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: &Resolver{}}))) c := client.New(srv.URL) query := `{ diff --git a/example/selection/server/server.go b/example/selection/server/server.go index 84e291cb5db..993ea659f32 100644 --- a/example/selection/server/server.go +++ b/example/selection/server/server.go @@ -10,6 +10,6 @@ import ( func main() { http.Handle("/", handler.Playground("Selection Demo", "/query")) - http.Handle("/query", handler.GraphQL(selection.NewExecutableSchema(&selection.Resolver{}))) + http.Handle("/query", handler.GraphQL(selection.NewExecutableSchema(selection.Config{Resolvers: &selection.Resolver{}}))) log.Fatal(http.ListenAndServe(":8086", nil)) } diff --git a/example/starwars/generated.go b/example/starwars/generated.go index 8c1b64d1576..3cc8a734d60 100644 --- a/example/starwars/generated.go +++ b/example/starwars/generated.go @@ -16,8 +16,16 @@ import ( ) // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. -func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { - return &executableSchema{resolvers: resolvers} +func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { + return &executableSchema{ + resolvers: cfg.Resolvers, + directives: cfg.Directives, + } +} + +type Config struct { + Resolvers ResolverRoot + Directives DirectiveRoot } type ResolverRoot interface { @@ -28,6 +36,9 @@ type ResolverRoot interface { Query() QueryResolver Starship() StarshipResolver } + +type DirectiveRoot struct { +} type DroidResolver interface { Friends(ctx context.Context, obj *Droid) ([]Character, error) FriendsConnection(ctx context.Context, obj *Droid, first *int, after *string) (FriendsConnection, error) @@ -59,7 +70,8 @@ type StarshipResolver interface { } type executableSchema struct { - resolvers ResolverRoot + resolvers ResolverRoot + directives DirectiveRoot } func (e *executableSchema) Schema() *ast.Schema { @@ -67,7 +79,7 @@ func (e *executableSchema) Schema() *ast.Schema { } func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._Query(ctx, op.SelectionSet) @@ -83,7 +95,7 @@ func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinitio } func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._Mutation(ctx, op.SelectionSet) @@ -104,8 +116,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe type executionContext struct { *graphql.RequestContext - - resolvers ResolverRoot + *executableSchema } var droidImplementors = []string{"Droid", "Character"} @@ -148,13 +159,9 @@ func (ec *executionContext) _Droid_id(ctx context.Context, field graphql.Collect rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -169,13 +176,9 @@ func (ec *executionContext) _Droid_name(ctx context.Context, field graphql.Colle rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -198,13 +201,9 @@ func (ec *executionContext) _Droid_friends(ctx context.Context, field graphql.Co } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Droid().Friends(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -268,13 +267,9 @@ func (ec *executionContext) _Droid_friendsConnection(ctx context.Context, field } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Droid().FriendsConnection(ctx, obj, args["first"].(*int), args["after"].(*string)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -290,13 +285,9 @@ func (ec *executionContext) _Droid_appearsIn(ctx context.Context, field graphql. rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.AppearsIn, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -320,13 +311,9 @@ func (ec *executionContext) _Droid_primaryFunction(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.PrimaryFunction, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -370,13 +357,9 @@ func (ec *executionContext) _FriendsConnection_totalCount(ctx context.Context, f rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.TotalCount(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -399,13 +382,9 @@ func (ec *executionContext) _FriendsConnection_edges(ctx context.Context, field } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.FriendsConnection().Edges(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -438,13 +417,9 @@ func (ec *executionContext) _FriendsConnection_friends(ctx context.Context, fiel } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.FriendsConnection().Friends(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -469,13 +444,9 @@ func (ec *executionContext) _FriendsConnection_pageInfo(ctx context.Context, fie rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.PageInfo(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -515,13 +486,9 @@ func (ec *executionContext) _FriendsEdge_cursor(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Cursor, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -536,13 +503,9 @@ func (ec *executionContext) _FriendsEdge_node(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Node, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -594,13 +557,9 @@ func (ec *executionContext) _Human_id(ctx context.Context, field graphql.Collect rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -615,13 +574,9 @@ func (ec *executionContext) _Human_name(ctx context.Context, field graphql.Colle rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -656,13 +611,9 @@ func (ec *executionContext) _Human_height(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Height(args["unit"].(LengthUnit)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -677,13 +628,9 @@ func (ec *executionContext) _Human_mass(ctx context.Context, field graphql.Colle rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Mass, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -706,13 +653,9 @@ func (ec *executionContext) _Human_friends(ctx context.Context, field graphql.Co } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Human().Friends(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -776,13 +719,9 @@ func (ec *executionContext) _Human_friendsConnection(ctx context.Context, field } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Human().FriendsConnection(ctx, obj, args["first"].(*int), args["after"].(*string)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -798,13 +737,9 @@ func (ec *executionContext) _Human_appearsIn(ctx context.Context, field graphql. rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.AppearsIn, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -836,13 +771,9 @@ func (ec *executionContext) _Human_starships(ctx context.Context, field graphql. } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Human().Starships(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -915,13 +846,9 @@ func (ec *executionContext) _Mutation_createReview(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Mutation().CreateReview(ctx, args["episode"].(Episode), args["review"].(Review)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -966,13 +893,9 @@ func (ec *executionContext) _PageInfo_startCursor(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.StartCursor, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -987,13 +910,9 @@ func (ec *executionContext) _PageInfo_endCursor(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.EndCursor, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1008,13 +927,9 @@ func (ec *executionContext) _PageInfo_hasNextPage(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.HasNextPage, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1100,13 +1015,9 @@ func (ec *executionContext) _Query_hero(ctx context.Context, field graphql.Colle } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Hero(ctx, args["episode"].(Episode)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1156,13 +1067,9 @@ func (ec *executionContext) _Query_reviews(ctx context.Context, field graphql.Co } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Reviews(ctx, args["episode"].(Episode), args["since"].(*time.Time)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1206,13 +1113,9 @@ func (ec *executionContext) _Query_search(ctx context.Context, field graphql.Col } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Search(ctx, args["text"].(string)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1256,13 +1159,9 @@ func (ec *executionContext) _Query_character(ctx context.Context, field graphql. } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Character(ctx, args["id"].(string)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1297,13 +1196,9 @@ func (ec *executionContext) _Query_droid(ctx context.Context, field graphql.Coll } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Droid(ctx, args["id"].(string)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1341,13 +1236,9 @@ func (ec *executionContext) _Query_human(ctx context.Context, field graphql.Coll } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Human(ctx, args["id"].(string)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1385,13 +1276,9 @@ func (ec *executionContext) _Query_starship(ctx context.Context, field graphql.C } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Starship(ctx, args["id"].(string)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1421,13 +1308,9 @@ func (ec *executionContext) _Query___type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectType(args["name"].(string)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1445,13 +1328,9 @@ func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectSchema(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1496,13 +1375,9 @@ func (ec *executionContext) _Review_stars(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Stars, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1517,13 +1392,9 @@ func (ec *executionContext) _Review_commentary(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Commentary, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1541,13 +1412,9 @@ func (ec *executionContext) _Review_time(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Time, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1591,13 +1458,9 @@ func (ec *executionContext) _Starship_id(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1612,13 +1475,9 @@ func (ec *executionContext) _Starship_name(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1661,13 +1520,9 @@ func (ec *executionContext) _Starship_length(ctx context.Context, field graphql. } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Starship().Length(ctx, obj, args["unit"].(LengthUnit)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1683,13 +1538,9 @@ func (ec *executionContext) _Starship_history(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.History, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1751,13 +1602,9 @@ func (ec *executionContext) ___Directive_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1772,13 +1619,9 @@ func (ec *executionContext) ___Directive_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1793,13 +1636,9 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Locations, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1823,13 +1662,9 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1882,13 +1717,9 @@ func (ec *executionContext) ___EnumValue_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1903,13 +1734,9 @@ func (ec *executionContext) ___EnumValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1924,13 +1751,9 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1945,13 +1768,9 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1999,13 +1818,9 @@ func (ec *executionContext) ___Field_name(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2020,13 +1835,9 @@ func (ec *executionContext) ___Field_description(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2041,13 +1852,9 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2071,13 +1878,9 @@ func (ec *executionContext) ___Field_type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2095,13 +1898,9 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2116,13 +1915,9 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2166,13 +1961,9 @@ func (ec *executionContext) ___InputValue_name(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2187,13 +1978,9 @@ func (ec *executionContext) ___InputValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2208,13 +1995,9 @@ func (ec *executionContext) ___InputValue_type(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2232,13 +2015,9 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DefaultValue, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2284,13 +2063,9 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Types(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2314,13 +2089,9 @@ func (ec *executionContext) ___Schema_queryType(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.QueryType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2338,13 +2109,9 @@ func (ec *executionContext) ___Schema_mutationType(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.MutationType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2362,13 +2129,9 @@ func (ec *executionContext) ___Schema_subscriptionType(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.SubscriptionType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2386,13 +2149,9 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Directives(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2455,13 +2214,9 @@ func (ec *executionContext) ___Type_kind(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Kind(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2476,13 +2231,9 @@ func (ec *executionContext) ___Type_name(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2497,13 +2248,9 @@ func (ec *executionContext) ___Type_description(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2529,13 +2276,9 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Fields(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2559,13 +2302,9 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Interfaces(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2589,13 +2328,9 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.PossibleTypes(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2630,13 +2365,9 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.EnumValues(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2660,13 +2391,9 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.InputFields(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2690,13 +2417,9 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.OfType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -2780,6 +2503,15 @@ func UnmarshalReviewInput(v interface{}) (Review, error) { return it, nil } +func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} { + res, err := ec.ResolverMiddleware(ctx, next) + if err != nil { + ec.Error(ctx, err) + return nil + } + return res +} + func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) } diff --git a/example/starwars/resolvers.go b/example/starwars/resolvers.go index 8cf78469aa1..b591f5aa298 100644 --- a/example/starwars/resolvers.go +++ b/example/starwars/resolvers.go @@ -204,7 +204,7 @@ func (r *starshipResolver) Length(ctx context.Context, obj *Starship, unit Lengt } } -func NewResolver() *Resolver { +func NewResolver() Config { r := Resolver{} r.humans = map[string]Human{ "1000": { @@ -332,5 +332,7 @@ func NewResolver() *Resolver { r.reviews = map[Episode][]Review{} - return &r + return Config{ + Resolvers: &r, + } } diff --git a/example/todo/generated.go b/example/todo/generated.go index 775ce980e47..66f2e5acc55 100644 --- a/example/todo/generated.go +++ b/example/todo/generated.go @@ -14,26 +14,40 @@ import ( ) // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. -func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { - return &executableSchema{resolvers: resolvers} +func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { + return &executableSchema{ + resolvers: cfg.Resolvers, + directives: cfg.Directives, + } +} + +type Config struct { + Resolvers ResolverRoot + Directives DirectiveRoot } type ResolverRoot interface { MyMutation() MyMutationResolver MyQuery() MyQueryResolver } + +type DirectiveRoot struct { + IsAuthenticated graphql.FieldMiddleware +} type MyMutationResolver interface { CreateTodo(ctx context.Context, todo TodoInput) (Todo, error) UpdateTodo(ctx context.Context, id int, changes map[string]interface{}) (*Todo, error) } type MyQueryResolver interface { Todo(ctx context.Context, id int) (*Todo, error) + AuthenticatedTodo(ctx context.Context, id int) (*Todo, error) LastTodo(ctx context.Context) (*Todo, error) Todos(ctx context.Context) ([]Todo, error) } type executableSchema struct { - resolvers ResolverRoot + resolvers ResolverRoot + directives DirectiveRoot } func (e *executableSchema) Schema() *ast.Schema { @@ -41,7 +55,7 @@ func (e *executableSchema) Schema() *ast.Schema { } func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._MyQuery(ctx, op.SelectionSet) @@ -57,7 +71,7 @@ func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinitio } func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._MyMutation(ctx, op.SelectionSet) @@ -78,8 +92,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe type executionContext struct { *graphql.RequestContext - - resolvers ResolverRoot + *executableSchema } var myMutationImplementors = []string{"MyMutation"} @@ -129,13 +142,9 @@ func (ec *executionContext) _MyMutation_createTodo(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.MyMutation().CreateTodo(ctx, args["todo"].(TodoInput)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -171,13 +180,9 @@ func (ec *executionContext) _MyMutation_updateTodo(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.MyMutation().UpdateTodo(ctx, args["id"].(int), args["changes"].(map[string]interface{})) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -207,6 +212,8 @@ func (ec *executionContext) _MyQuery(ctx context.Context, sel ast.SelectionSet) out.Values[i] = graphql.MarshalString("MyQuery") case "todo": out.Values[i] = ec._MyQuery_todo(ctx, field) + case "authenticatedTodo": + out.Values[i] = ec._MyQuery_authenticatedTodo(ctx, field) case "lastTodo": out.Values[i] = ec._MyQuery_lastTodo(ctx, field) case "todos": @@ -249,13 +256,49 @@ func (ec *executionContext) _MyQuery_todo(ctx context.Context, field graphql.Col } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.MyQuery().Todo(ctx, args["id"].(int)) }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*Todo) + if res == nil { + return graphql.Null + } + return ec._Todo(ctx, field.Selections, res) + }) +} + +func (ec *executionContext) _MyQuery_authenticatedTodo(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { + args := map[string]interface{}{} + var arg0 int + if tmp, ok := field.Args["id"]; ok { + var err error + arg0, err = graphql.UnmarshalInt(tmp) if err != nil { ec.Error(ctx, err) return graphql.Null } + } + args["id"] = arg0 + ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ + Object: "MyQuery", + Args: args, + Field: field, + }) + return graphql.Defer(func() (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + userErr := ec.Recover(ctx, r) + ec.Error(ctx, userErr) + ret = graphql.Null + } + }() + + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return ec.resolvers.MyQuery().AuthenticatedTodo(ctx, args["id"].(int)) + }) if resTmp == nil { return graphql.Null } @@ -282,13 +325,9 @@ func (ec *executionContext) _MyQuery_lastTodo(ctx context.Context, field graphql } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.MyQuery().LastTodo(ctx) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -315,13 +354,9 @@ func (ec *executionContext) _MyQuery_todos(ctx context.Context, field graphql.Co } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.MyQuery().Todos(ctx) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -357,13 +392,9 @@ func (ec *executionContext) _MyQuery___type(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectType(args["name"].(string)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -381,13 +412,9 @@ func (ec *executionContext) _MyQuery___schema(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectSchema(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -432,13 +459,9 @@ func (ec *executionContext) _Todo_id(ctx context.Context, field graphql.Collecte rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.ID, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -453,13 +476,9 @@ func (ec *executionContext) _Todo_text(ctx context.Context, field graphql.Collec rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Text, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -474,13 +493,9 @@ func (ec *executionContext) _Todo_done(ctx context.Context, field graphql.Collec rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Done, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -524,13 +539,9 @@ func (ec *executionContext) ___Directive_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -545,13 +556,9 @@ func (ec *executionContext) ___Directive_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -566,13 +573,9 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Locations, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -596,13 +599,9 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -655,13 +654,9 @@ func (ec *executionContext) ___EnumValue_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -676,13 +671,9 @@ func (ec *executionContext) ___EnumValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -697,13 +688,9 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -718,13 +705,9 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -772,13 +755,9 @@ func (ec *executionContext) ___Field_name(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -793,13 +772,9 @@ func (ec *executionContext) ___Field_description(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -814,13 +789,9 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -844,13 +815,9 @@ func (ec *executionContext) ___Field_type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -868,13 +835,9 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -889,13 +852,9 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -939,13 +898,9 @@ func (ec *executionContext) ___InputValue_name(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -960,13 +915,9 @@ func (ec *executionContext) ___InputValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -981,13 +932,9 @@ func (ec *executionContext) ___InputValue_type(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1005,13 +952,9 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DefaultValue, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1057,13 +1000,9 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Types(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1087,13 +1026,9 @@ func (ec *executionContext) ___Schema_queryType(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.QueryType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1111,13 +1046,9 @@ func (ec *executionContext) ___Schema_mutationType(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.MutationType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1135,13 +1066,9 @@ func (ec *executionContext) ___Schema_subscriptionType(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.SubscriptionType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1159,13 +1086,9 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Directives(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1228,13 +1151,9 @@ func (ec *executionContext) ___Type_kind(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Kind(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1249,13 +1168,9 @@ func (ec *executionContext) ___Type_name(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1270,13 +1185,9 @@ func (ec *executionContext) ___Type_description(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1302,13 +1213,9 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Fields(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1332,13 +1239,9 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Interfaces(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1362,13 +1265,9 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.PossibleTypes(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1403,13 +1302,9 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.EnumValues(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1433,13 +1328,9 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.InputFields(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1463,13 +1354,9 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.OfType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1509,6 +1396,27 @@ func UnmarshalTodoInput(v interface{}) (TodoInput, error) { return it, nil } +func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} { + rctx := graphql.GetResolverContext(ctx) + for _, d := range rctx.Field.Definition.Directives { + switch d.Name { + case "isAuthenticated": + if ec.directives.IsAuthenticated != nil { + n := next + next = func(ctx context.Context) (interface{}, error) { + return ec.directives.IsAuthenticated(ctx, n) + } + } + } + } + res, err := ec.ResolverMiddleware(ctx, next) + if err != nil { + ec.Error(ctx, err) + return nil + } + return res +} + func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) } @@ -1525,6 +1433,7 @@ var parsedSchema = gqlparser.MustLoadSchema( type MyQuery { todo(id: Int!): Todo + authenticatedTodo(id: Int!): Todo @isAuthenticated lastTodo: Todo todos: [Todo!]! } @@ -1546,5 +1455,7 @@ input TodoInput { } scalar Map + +directive @isAuthenticated on FIELD_DEFINITION `}, ) diff --git a/example/todo/schema.graphql b/example/todo/schema.graphql index 8482d0df778..bdc631b016c 100644 --- a/example/todo/schema.graphql +++ b/example/todo/schema.graphql @@ -5,6 +5,7 @@ schema { type MyQuery { todo(id: Int!): Todo + authenticatedTodo(id: Int!): Todo @isAuthenticated lastTodo: Todo todos: [Todo!]! } @@ -26,3 +27,5 @@ input TodoInput { } scalar Map + +directive @isAuthenticated on FIELD_DEFINITION diff --git a/example/todo/todo.go b/example/todo/todo.go index 3f6f9dd226c..d9aa4ea7f7f 100644 --- a/example/todo/todo.go +++ b/example/todo/todo.go @@ -8,17 +8,30 @@ import ( "time" "github.com/mitchellh/mapstructure" + graphql "github.com/vektah/gqlgen/graphql" ) -func New() *resolvers { - return &resolvers{ - todos: []Todo{ - {ID: 1, Text: "A todo not to forget", Done: false}, - {ID: 2, Text: "This is the most important", Done: false}, - {ID: 3, Text: "Please do this or else", Done: false}, +func New() Config { + c := Config{ + Resolvers: &resolvers{ + todos: []Todo{ + {ID: 1, Text: "A todo not to forget", Done: false}, + {ID: 2, Text: "This is the most important", Done: false}, + {ID: 3, Text: "Please do this or else", Done: false}, + }, + lastID: 3, }, - lastID: 3, } + c.Directives.IsAuthenticated = func(ctx context.Context, next graphql.Resolver) (interface{}, error) { + rctx := graphql.GetResolverContext(ctx) + idVal := rctx.Field.Arguments.ForName("id").Value + id, _ := idVal.Value(make(map[string]interface{})) + if id.(int64) == 1 { + return nil, nil + } + return next(ctx) + } + return c } type resolvers struct { @@ -62,6 +75,10 @@ func (r *QueryResolver) Todos(ctx context.Context) ([]Todo, error) { return r.todos, nil } +func (r *QueryResolver) AuthenticatedTodo(ctx context.Context, id int) (*Todo, error) { + return r.Todo(ctx, id) +} + type MutationResolver resolvers func (r *MutationResolver) CreateTodo(ctx context.Context, todo TodoInput) (Todo, error) { diff --git a/example/todo/todo_test.go b/example/todo/todo_test.go index e5c4fdddfe3..ca244274ad4 100644 --- a/example/todo/todo_test.go +++ b/example/todo/todo_test.go @@ -127,6 +127,19 @@ func TestTodo(t *testing.T) { require.Equal(t, "Completed todo", resp.CreateTodo.Text) }) + + t.Run("isAuthenticated directive middleware", func(t *testing.T) { + var resp map[string]interface{} + c.MustPost(`{ authenticatedTodo(id: 1) { __typename } }`, &resp) + val, ok := resp["authenticatedTodo"] + require.True(t, ok) + require.Nil(t, val) + + c.MustPost(`{ authenticatedTodo(id: 2) { __typename } }`, &resp) + val, ok = resp["authenticatedTodo"] + require.True(t, ok) + require.NotNil(t, val) + }) } func TestSkipAndIncludeDirectives(t *testing.T) { diff --git a/graphql/context.go b/graphql/context.go index 4ffaf38f568..8fe5155e08b 100644 --- a/graphql/context.go +++ b/graphql/context.go @@ -10,7 +10,7 @@ import ( ) type Resolver func(ctx context.Context) (res interface{}, err error) -type ResolverMiddleware func(ctx context.Context, next Resolver) (res interface{}, err error) +type FieldMiddleware func(ctx context.Context, next Resolver) (res interface{}, err error) type RequestMiddleware func(ctx context.Context, next func(ctx context.Context) []byte) []byte type RequestContext struct { @@ -19,10 +19,11 @@ type RequestContext struct { Doc *ast.QueryDocument // ErrorPresenter will be used to generate the error // message from errors given to Error(). - ErrorPresenter ErrorPresenterFunc - Recover RecoverFunc - ResolverMiddleware ResolverMiddleware - RequestMiddleware RequestMiddleware + ErrorPresenter ErrorPresenterFunc + Recover RecoverFunc + ResolverMiddleware FieldMiddleware + DirectiveMiddleware FieldMiddleware + RequestMiddleware RequestMiddleware errorsMu sync.Mutex Errors gqlerror.List @@ -32,19 +33,24 @@ func DefaultResolverMiddleware(ctx context.Context, next Resolver) (res interfac return next(ctx) } +func DefaultDirectiveMiddleware(ctx context.Context, next Resolver) (res interface{}, err error) { + return next(ctx) +} + func DefaultRequestMiddleware(ctx context.Context, next func(ctx context.Context) []byte) []byte { return next(ctx) } func NewRequestContext(doc *ast.QueryDocument, query string, variables map[string]interface{}) *RequestContext { return &RequestContext{ - Doc: doc, - RawQuery: query, - Variables: variables, - ResolverMiddleware: DefaultResolverMiddleware, - RequestMiddleware: DefaultRequestMiddleware, - Recover: DefaultRecover, - ErrorPresenter: DefaultErrorPresenter, + Doc: doc, + RawQuery: query, + Variables: variables, + ResolverMiddleware: DefaultResolverMiddleware, + DirectiveMiddleware: DefaultDirectiveMiddleware, + RequestMiddleware: DefaultRequestMiddleware, + Recover: DefaultRecover, + ErrorPresenter: DefaultErrorPresenter, } } @@ -106,7 +112,7 @@ func WithResolverContext(ctx context.Context, rc *ResolverContext) context.Conte if parent != nil { rc.Path = append(rc.Path, parent.Path...) } - if rc.Field.Alias != "" { + if rc.Field.Field != nil && rc.Field.Alias != "" { rc.PushField(rc.Field.Alias) } return context.WithValue(ctx, resolver, rc) diff --git a/graphql/exec.go b/graphql/exec.go index 11fa9c95f33..5231abc2bd8 100644 --- a/graphql/exec.go +++ b/graphql/exec.go @@ -29,10 +29,7 @@ func collectFields(reqCtx *RequestContext, selSet ast.SelectionSet, satisfies [] continue } f := getOrCreateField(&groupedFields, sel.Alias, func() CollectedField { - f := CollectedField{ - Alias: sel.Alias, - Name: sel.Name, - } + f := CollectedField{Field: sel} if len(sel.Arguments) > 0 { f.Args = map[string]interface{}{} for _, arg := range sel.Arguments { @@ -96,8 +93,8 @@ func collectFields(reqCtx *RequestContext, selSet ast.SelectionSet, satisfies [] } type CollectedField struct { - Alias string - Name string + *ast.Field + Args map[string]interface{} Selections ast.SelectionSet } diff --git a/handler/graphql.go b/handler/graphql.go index 5e9d1c7ca82..43e953d6c92 100644 --- a/handler/graphql.go +++ b/handler/graphql.go @@ -24,7 +24,7 @@ type Config struct { upgrader websocket.Upgrader recover graphql.RecoverFunc errorPresenter graphql.ErrorPresenterFunc - resolverHook graphql.ResolverMiddleware + resolverHook graphql.FieldMiddleware requestHook graphql.RequestMiddleware } @@ -74,7 +74,7 @@ func ErrorPresenter(f graphql.ErrorPresenterFunc) Option { // ResolverMiddleware allows you to define a function that will be called around every resolver, // useful for tracing and logging. -func ResolverMiddleware(middleware graphql.ResolverMiddleware) Option { +func ResolverMiddleware(middleware graphql.FieldMiddleware) Option { return func(cfg *Config) { if cfg.resolverHook == nil { cfg.resolverHook = middleware diff --git a/opentracing/opentracing.go b/opentracing/opentracing.go index ddb98f7ae05..0ae4e2e7904 100644 --- a/opentracing/opentracing.go +++ b/opentracing/opentracing.go @@ -10,7 +10,7 @@ import ( "github.com/vektah/gqlgen/graphql" ) -func ResolverMiddleware() graphql.ResolverMiddleware { +func ResolverMiddleware() graphql.FieldMiddleware { return func(ctx context.Context, next graphql.Resolver) (interface{}, error) { rctx := graphql.GetResolverContext(ctx) span, ctx := opentracing.StartSpanFromContext(ctx, rctx.Object+"_"+rctx.Field.Name, diff --git a/test/generated.go b/test/generated.go index 40e6aaaa1eb..36268ffa585 100644 --- a/test/generated.go +++ b/test/generated.go @@ -16,8 +16,16 @@ import ( ) // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. -func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { - return &executableSchema{resolvers: resolvers} +func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { + return &executableSchema{ + resolvers: cfg.Resolvers, + directives: cfg.Directives, + } +} + +type Config struct { + Resolvers ResolverRoot + Directives DirectiveRoot } type ResolverRoot interface { @@ -25,6 +33,9 @@ type ResolverRoot interface { Query() QueryResolver User() UserResolver } + +type DirectiveRoot struct { +} type ElementResolver interface { Child(ctx context.Context, obj *models.Element) (models.Element, error) Error(ctx context.Context, obj *models.Element) (bool, error) @@ -41,7 +52,8 @@ type UserResolver interface { } type executableSchema struct { - resolvers ResolverRoot + resolvers ResolverRoot + directives DirectiveRoot } func (e *executableSchema) Schema() *ast.Schema { @@ -49,7 +61,7 @@ func (e *executableSchema) Schema() *ast.Schema { } func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { data := ec._Query(ctx, op.SelectionSet) @@ -74,8 +86,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe type executionContext struct { *graphql.RequestContext - - resolvers ResolverRoot + *executableSchema } var elementImplementors = []string{"Element"} @@ -120,13 +131,9 @@ func (ec *executionContext) _Element_child(ctx context.Context, field graphql.Co } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Element().Child(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -150,13 +157,9 @@ func (ec *executionContext) _Element_error(ctx context.Context, field graphql.Co } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Element().Error(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -180,13 +183,9 @@ func (ec *executionContext) _Element_mismatched(ctx context.Context, field graph } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Element().Mismatched(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -256,13 +255,9 @@ func (ec *executionContext) _Query_path(ctx context.Context, field graphql.Colle } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Path(ctx) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -309,13 +304,9 @@ func (ec *executionContext) _Query_date(ctx context.Context, field graphql.Colle } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Date(ctx, args["filter"].(models.DateFilter)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -339,13 +330,9 @@ func (ec *executionContext) _Query_viewer(ctx context.Context, field graphql.Col } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().Viewer(ctx) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -372,13 +359,9 @@ func (ec *executionContext) _Query_jsonEncoding(ctx context.Context, field graph } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.Query().JsonEncoding(ctx) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -405,13 +388,9 @@ func (ec *executionContext) _Query___type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectType(args["name"].(string)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -429,13 +408,9 @@ func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.introspectSchema(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -478,13 +453,9 @@ func (ec *executionContext) _User_name(ctx context.Context, field graphql.Collec rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -507,13 +478,9 @@ func (ec *executionContext) _User_likes(ctx context.Context, field graphql.Colle } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return ec.resolvers.User().Likes(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -561,13 +528,9 @@ func (ec *executionContext) _Viewer_user(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.User, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -614,13 +577,9 @@ func (ec *executionContext) ___Directive_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -635,13 +594,9 @@ func (ec *executionContext) ___Directive_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -656,13 +611,9 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Locations, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -686,13 +637,9 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -745,13 +692,9 @@ func (ec *executionContext) ___EnumValue_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -766,13 +709,9 @@ func (ec *executionContext) ___EnumValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -787,13 +726,9 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -808,13 +743,9 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -862,13 +793,9 @@ func (ec *executionContext) ___Field_name(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -883,13 +810,9 @@ func (ec *executionContext) ___Field_description(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -904,13 +827,9 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Args, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -934,13 +853,9 @@ func (ec *executionContext) ___Field_type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -958,13 +873,9 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.IsDeprecated, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -979,13 +890,9 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DeprecationReason, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1029,13 +936,9 @@ func (ec *executionContext) ___InputValue_name(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1050,13 +953,9 @@ func (ec *executionContext) ___InputValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1071,13 +970,9 @@ func (ec *executionContext) ___InputValue_type(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Type, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1095,13 +990,9 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.DefaultValue, nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1147,13 +1038,9 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Types(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1177,13 +1064,9 @@ func (ec *executionContext) ___Schema_queryType(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.QueryType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1201,13 +1084,9 @@ func (ec *executionContext) ___Schema_mutationType(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.MutationType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1225,13 +1104,9 @@ func (ec *executionContext) ___Schema_subscriptionType(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.SubscriptionType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1249,13 +1124,9 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Directives(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1318,13 +1189,9 @@ func (ec *executionContext) ___Type_kind(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Kind(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1339,13 +1206,9 @@ func (ec *executionContext) ___Type_name(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Name(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1360,13 +1223,9 @@ func (ec *executionContext) ___Type_description(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Description(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1392,13 +1251,9 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Fields(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1422,13 +1277,9 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.Interfaces(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1452,13 +1303,9 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.PossibleTypes(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1493,13 +1340,9 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.EnumValues(args["includeDeprecated"].(bool)), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1523,13 +1366,9 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.InputFields(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1553,13 +1392,9 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { return obj.OfType(), nil }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } @@ -1617,6 +1452,15 @@ func UnmarshalDateFilter(v interface{}) (models.DateFilter, error) { return it, nil } +func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} { + res, err := ec.ResolverMiddleware(ctx, next) + if err != nil { + ec.Error(ctx, err) + return nil + } + return res +} + func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) } diff --git a/test/resolvers_test.go b/test/resolvers_test.go index db27e9c8dab..1de3bab1c3a 100644 --- a/test/resolvers_test.go +++ b/test/resolvers_test.go @@ -22,7 +22,7 @@ import ( func TestCustomErrorPresenter(t *testing.T) { resolvers := &testResolver{} - srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(resolvers), + srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers}), handler.ErrorPresenter(func(i context.Context, e error) *gqlerror.Error { if _, ok := errors.Cause(e).(*specialErr); ok { return &gqlerror.Error{Message: "override special error message"} @@ -62,7 +62,7 @@ func TestCustomErrorPresenter(t *testing.T) { } func TestErrorPath(t *testing.T) { - srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(&testResolver{err: fmt.Errorf("boom")}))) + srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: &testResolver{err: fmt.Errorf("boom")}}))) c := client.New(srv.URL) var resp struct{} @@ -73,7 +73,7 @@ func TestErrorPath(t *testing.T) { func TestInputDefaults(t *testing.T) { called := false - srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(&testResolver{ + srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: &testResolver{ queryDate: func(ctx context.Context, filter models.DateFilter) (bool, error) { assert.Equal(t, "asdf", filter.Value) assert.Equal(t, "UTC", *filter.Timezone) @@ -82,7 +82,7 @@ func TestInputDefaults(t *testing.T) { return false, nil }, - }))) + }}))) c := client.New(srv.URL) var resp struct { @@ -96,7 +96,7 @@ func TestInputDefaults(t *testing.T) { } func TestJsonEncoding(t *testing.T) { - srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(&testResolver{}))) + srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: &testResolver{}}))) c := client.New(srv.URL) var resp struct {