From 69d7e28241b9847a073f1a335a5cc12e2efddf05 Mon Sep 17 00:00:00 2001 From: asamusev Date: Tue, 18 Jun 2019 09:41:16 +0300 Subject: [PATCH] move directive to directives.gotpl --- codegen/data.go | 34 +++-- codegen/directive.go | 18 ++- codegen/directives.gotpl | 84 +++++++++++++ codegen/generated!.gotpl | 138 ++------------------- codegen/testserver/generated.go | 4 + example/chat/generated.go | 80 ++++++------ example/config/generated.go | 4 + example/dataloader/generated.go | 4 + example/fileupload/generated.go | 4 + example/scalars/generated.go | 4 + example/selection/generated.go | 4 + example/starwars/generated/exec.go | 4 + example/todo/generated.go | 136 ++++++++++---------- example/type-system-extension/generated.go | 4 + integration/generated.go | 4 + 15 files changed, 270 insertions(+), 256 deletions(-) create mode 100644 codegen/directives.gotpl diff --git a/codegen/data.go b/codegen/data.go index ca32707067a..12c85921064 100644 --- a/codegen/data.go +++ b/codegen/data.go @@ -12,18 +12,15 @@ import ( // Data is a unified model of the code to be generated. Plugins may modify this structure to do things like implement // resolvers or directives automatically (eg grpc, validation) type Data struct { - Config *config.Config - Schema *ast.Schema - SchemaStr map[string]string - Directives DirectiveList - QueryDirectives DirectiveList - MutationDirectives DirectiveList - SubscriptionDirectives DirectiveList - Objects Objects - Inputs Objects - Interfaces map[string]*Interface - ReferencedTypes map[string]*config.TypeReference - ComplexityRoots map[string]*Object + Config *config.Config + Schema *ast.Schema + SchemaStr map[string]string + Directives DirectiveList + Objects Objects + Inputs Objects + Interfaces map[string]*Interface + ReferencedTypes map[string]*config.TypeReference + ComplexityRoots map[string]*Object QueryRoot *Object MutationRoot *Object @@ -74,14 +71,11 @@ func BuildData(cfg *config.Config) (*Data, error) { } s := Data{ - Config: cfg, - Directives: dataDirectives, - QueryDirectives: locationDirectives(dataDirectives, ast.LocationQuery), - MutationDirectives: locationDirectives(dataDirectives, ast.LocationMutation), - SubscriptionDirectives: locationDirectives(dataDirectives, ast.LocationSubscription), - Schema: b.Schema, - SchemaStr: b.SchemaStr, - Interfaces: map[string]*Interface{}, + Config: cfg, + Directives: dataDirectives, + Schema: b.Schema, + SchemaStr: b.SchemaStr, + Interfaces: map[string]*Interface{}, } for _, schemaType := range b.Schema.Types { diff --git a/codegen/directive.go b/codegen/directive.go index dccf914d36b..5857717c520 100644 --- a/codegen/directive.go +++ b/codegen/directive.go @@ -12,6 +12,11 @@ import ( type DirectiveList map[string]*Directive +//LocationDirectives filter directives by location +func (dl DirectiveList) LocationDirectives(location string) DirectiveList { + return locationDirectives(dl, ast.DirectiveLocation(location)) +} + type Directive struct { *ast.DirectiveDefinition Name string @@ -19,20 +24,23 @@ type Directive struct { Builtin bool } -func (d *Directive) IsLocation(location ast.DirectiveLocation) bool { +//IsLocation check location directive +func (d *Directive) IsLocation(location ...ast.DirectiveLocation) bool { for _, l := range d.Locations { - if l == location { - return true + for _, a := range location { + if l == a { + return true + } } } return false } -func locationDirectives(directives DirectiveList, location ast.DirectiveLocation) map[string]*Directive { +func locationDirectives(directives DirectiveList, location ...ast.DirectiveLocation) map[string]*Directive { mDirectives := make(map[string]*Directive) for name, d := range directives { - if d.IsLocation(location) { + if d.IsLocation(location...) { mDirectives[name] = d } } diff --git a/codegen/directives.gotpl b/codegen/directives.gotpl new file mode 100644 index 00000000000..1db2177a8ec --- /dev/null +++ b/codegen/directives.gotpl @@ -0,0 +1,84 @@ + +{{define "queryDirectives"}} + for _, d := range obj.Directives { + switch d.Name { + {{- range $directive := . }} + case "{{$directive.Name}}": + {{- if $directive.Args }} + rawArgs := d.ArgumentMap(ec.Variables) + args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + {{- end }} + n := next + next = func(ctx context.Context) (interface{}, error) { + return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}}) + } + {{- end }} + } + } + tmp, err := next(ctx) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if data, ok := tmp.(graphql.Marshaler); ok { + return data + } + ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) + return graphql.Null +{{end}} + +{{ if .Directives.LocationDirectives "QUERY" }} +func (ec *executionContext) _queryMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) graphql.Marshaler { + {{ template "queryDirectives" .Directives.LocationDirectives "QUERY" }} +} +{{ end }} + +{{ if .Directives.LocationDirectives "MUTATION" }} +func (ec *executionContext) _mutationMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) graphql.Marshaler { + {{ template "queryDirectives" .Directives.LocationDirectives "MUTATION" }} +} +{{ end }} + +{{ if .Directives.LocationDirectives "SUBSCRIPTION" }} +func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) func() graphql.Marshaler { + for _, d := range obj.Directives { + switch d.Name { + {{- range $directive := .Directives.LocationDirectives "SUBSCRIPTION" }} + case "{{$directive.Name}}": + {{- if $directive.Args }} + rawArgs := d.ArgumentMap(ec.Variables) + args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs) + if err != nil { + ec.Error(ctx, err) + return func() graphql.Marshaler { + return graphql.Null + } + } + {{- end }} + n := next + next = func(ctx context.Context) (interface{}, error) { + return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}}) + } + {{- end }} + } + } + tmp, err := next(ctx) + if err != nil { + ec.Error(ctx, err) + return func() graphql.Marshaler { + return graphql.Null + } + } + if data, ok := tmp.(func() graphql.Marshaler); ok { + return data + } + ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) + return func() graphql.Marshaler { + return graphql.Null + } +} +{{ end }} diff --git a/codegen/generated!.gotpl b/codegen/generated!.gotpl index ce040ef18b0..dec7cbc8231 100644 --- a/codegen/generated!.gotpl +++ b/codegen/generated!.gotpl @@ -117,8 +117,10 @@ func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinitio ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { - {{ if .QueryDirectives -}} - data := ec._{{.QueryRoot.Name}}Middleware(ctx, op) + {{ if .Directives.LocationDirectives "QUERY" -}} + data := ec._queryMiddleware(ctx, op, func(ctx context.Context) (interface{}, error){ + return ec._{{.QueryRoot.Name}}(ctx, op.SelectionSet), nil + }) {{- else -}} data := ec._{{.QueryRoot.Name}}(ctx, op.SelectionSet) {{- end }} @@ -142,8 +144,10 @@ func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefini ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { - {{ if .MutationDirectives -}} - data := ec._{{.MutationRoot.Name}}Middleware(ctx, op) + {{ if .Directives.LocationDirectives "MUTATION" -}} + data := ec._mutationMiddleware(ctx, op, func(ctx context.Context) (interface{}, error){ + return ec._{{.MutationRoot.Name}}(ctx, op.SelectionSet), nil + }) {{- else -}} data := ec._{{.MutationRoot.Name}}(ctx, op.SelectionSet) {{- end }} @@ -166,8 +170,10 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe {{- if .SubscriptionRoot }} ec := executionContext{graphql.GetRequestContext(ctx), e} - {{ if .SubscriptionDirectives -}} - next := ec._{{.SubscriptionRoot.Name}}Middleware(ctx, op) + {{ if .Directives.LocationDirectives "SUBSCRIPTION" -}} + next := ec._subscriptionMiddleware(ctx, op, func(ctx context.Context) (interface{}, error){ + return ec._{{.SubscriptionRoot.Name}}(ctx, op.SelectionSet),nil + }) {{- else -}} next := ec._{{.SubscriptionRoot.Name}}(ctx, op.SelectionSet) {{- end }} @@ -208,126 +214,6 @@ type executionContext struct { *executableSchema } -{{ if and .QueryDirectives .QueryRoot }} -func (ec *executionContext) _{{.QueryRoot.Name}}Middleware(ctx context.Context, obj *ast.OperationDefinition) graphql.Marshaler { - - next := func(ctx context.Context) (interface{}, error){ - return ec._{{.QueryRoot.Name}}(ctx, obj.SelectionSet),nil - } - for _, d := range obj.Directives { - switch d.Name { - {{- range $directive := .QueryDirectives }} - case "{{$directive.Name}}": - {{- if $directive.Args }} - rawArgs := d.ArgumentMap(ec.Variables) - args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - {{- end }} - n := next - next = func(ctx context.Context) (interface{}, error) { - return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}}) - } - {{- end }} - } - } - tmp, err := next(ctx) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if data, ok := tmp.(graphql.Marshaler); ok { - return data - } - ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) - return graphql.Null -} -{{end}} - -{{ if and .SubscriptionDirectives .SubscriptionRoot }} -func (ec *executionContext) _{{.SubscriptionRoot.Name}}Middleware(ctx context.Context, obj *ast.OperationDefinition) func() graphql.Marshaler { - - next := func(ctx context.Context) (interface{}, error){ - return ec._{{.SubscriptionRoot.Name}}(ctx, obj.SelectionSet),nil - } - for _, d := range obj.Directives { - switch d.Name { - {{- range $directive := .SubscriptionDirectives }} - case "{{$directive.Name}}": - {{- if $directive.Args }} - rawArgs := d.ArgumentMap(ec.Variables) - args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs) - if err != nil { - ec.Error(ctx, err) - return func() graphql.Marshaler { - return graphql.Null - } - } - {{- end }} - n := next - next = func(ctx context.Context) (interface{}, error) { - return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}}) - } - {{- end }} - } - } - tmp, err := next(ctx) - if err != nil { - ec.Error(ctx, err) - return func() graphql.Marshaler { - return graphql.Null - } - } - if data, ok := tmp.(func() graphql.Marshaler); ok { - return data - } - ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) - return func() graphql.Marshaler { - return graphql.Null - } -} -{{end}} - -{{ if and .MutationDirectives .MutationRoot }} -func (ec *executionContext) _{{.MutationRoot.Name}}Middleware(ctx context.Context, obj *ast.OperationDefinition) graphql.Marshaler { - - next := func(ctx context.Context) (interface{}, error){ - return ec._{{.MutationRoot.Name}}(ctx, obj.SelectionSet),nil - } - for _, d := range obj.Directives { - switch d.Name { - {{- range $directive := .MutationDirectives }} - case "{{$directive.Name}}": - {{- if $directive.Args }} - rawArgs := d.ArgumentMap(ec.Variables) - args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - {{- end }} - n := next - next = func(ctx context.Context) (interface{}, error) { - return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}}) - } - {{- end }} - } - } - tmp, err := next(ctx) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if data, ok := tmp.(graphql.Marshaler); ok { - return data - } - ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) - return graphql.Null -} -{{end}} - func (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) { defer func() { if r := recover(); r != nil { diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index d7a074445a8..39dc204e0dd 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -2263,6 +2263,10 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _A_id(ctx context.Context, field graphql.CollectedField, obj *A) graphql.Marshaler { diff --git a/example/chat/generated.go b/example/chat/generated.go index 15ffd28ce48..9ac1ae7528e 100644 --- a/example/chat/generated.go +++ b/example/chat/generated.go @@ -214,7 +214,9 @@ 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} - next := ec._SubscriptionMiddleware(ctx, op) + next := ec._subscriptionMiddleware(ctx, op, func(ctx context.Context) (interface{}, error) { + return ec._Subscription(ctx, op.SelectionSet), nil + }) if ec.Errors != nil { return graphql.OneShot(&graphql.Response{Data: []byte("null"), Errors: ec.Errors}) } @@ -249,44 +251,6 @@ type executionContext struct { *executableSchema } -func (ec *executionContext) _SubscriptionMiddleware(ctx context.Context, obj *ast.OperationDefinition) func() graphql.Marshaler { - - next := func(ctx context.Context) (interface{}, error) { - return ec._Subscription(ctx, obj.SelectionSet), nil - } - for _, d := range obj.Directives { - switch d.Name { - case "user": - rawArgs := d.ArgumentMap(ec.Variables) - args, err := ec.dir_user_args(ctx, rawArgs) - if err != nil { - ec.Error(ctx, err) - return func() graphql.Marshaler { - return graphql.Null - } - } - n := next - next = func(ctx context.Context) (interface{}, error) { - return ec.directives.User(ctx, obj, n, args["username"].(string)) - } - } - } - tmp, err := next(ctx) - if err != nil { - ec.Error(ctx, err) - return func() graphql.Marshaler { - return graphql.Null - } - } - if data, ok := tmp.(func() graphql.Marshaler); ok { - return data - } - ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) - return func() graphql.Marshaler { - return graphql.Null - } -} - func (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) { defer func() { if r := recover(); r != nil { @@ -485,6 +449,44 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) func() graphql.Marshaler { + for _, d := range obj.Directives { + switch d.Name { + case "user": + rawArgs := d.ArgumentMap(ec.Variables) + args, err := ec.dir_user_args(ctx, rawArgs) + if err != nil { + ec.Error(ctx, err) + return func() graphql.Marshaler { + return graphql.Null + } + } + n := next + next = func(ctx context.Context) (interface{}, error) { + return ec.directives.User(ctx, obj, n, args["username"].(string)) + } + } + } + tmp, err := next(ctx) + if err != nil { + ec.Error(ctx, err) + return func() graphql.Marshaler { + return graphql.Null + } + } + if data, ok := tmp.(func() graphql.Marshaler); ok { + return data + } + ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) + return func() graphql.Marshaler { + return graphql.Null + } +} + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _Chatroom_name(ctx context.Context, field graphql.CollectedField, obj *Chatroom) graphql.Marshaler { diff --git a/example/config/generated.go b/example/config/generated.go index ccf93600be2..da6797bc36c 100644 --- a/example/config/generated.go +++ b/example/config/generated.go @@ -326,6 +326,10 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _Mutation_createTodo(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { diff --git a/example/dataloader/generated.go b/example/dataloader/generated.go index 2bf7166d442..6ac5e7da1d4 100644 --- a/example/dataloader/generated.go +++ b/example/dataloader/generated.go @@ -393,6 +393,10 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _Address_id(ctx context.Context, field graphql.CollectedField, obj *Address) graphql.Marshaler { diff --git a/example/fileupload/generated.go b/example/fileupload/generated.go index 54b77979cd2..a9c76071976 100644 --- a/example/fileupload/generated.go +++ b/example/fileupload/generated.go @@ -375,6 +375,10 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _File_id(ctx context.Context, field graphql.CollectedField, obj *model.File) graphql.Marshaler { diff --git a/example/scalars/generated.go b/example/scalars/generated.go index 62a3e059b13..a5bd8819019 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -364,6 +364,10 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _Address_id(ctx context.Context, field graphql.CollectedField, obj *model.Address) graphql.Marshaler { diff --git a/example/selection/generated.go b/example/selection/generated.go index 26c3ef43848..9aa5781b15f 100644 --- a/example/selection/generated.go +++ b/example/selection/generated.go @@ -282,6 +282,10 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _Like_reaction(ctx context.Context, field graphql.CollectedField, obj *Like) graphql.Marshaler { diff --git a/example/starwars/generated/exec.go b/example/starwars/generated/exec.go index b70c74cfb85..ff3f2add346 100644 --- a/example/starwars/generated/exec.go +++ b/example/starwars/generated/exec.go @@ -945,6 +945,10 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _Droid_id(ctx context.Context, field graphql.CollectedField, obj *models.Droid) graphql.Marshaler { diff --git a/example/todo/generated.go b/example/todo/generated.go index 29681516cbd..a5ec211b6b3 100644 --- a/example/todo/generated.go +++ b/example/todo/generated.go @@ -167,7 +167,9 @@ func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinitio ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { - data := ec._MyQueryMiddleware(ctx, op) + data := ec._queryMiddleware(ctx, op, func(ctx context.Context) (interface{}, error) { + return ec._MyQuery(ctx, op.SelectionSet), nil + }) var buf bytes.Buffer data.MarshalGQL(&buf) return buf.Bytes() @@ -184,7 +186,9 @@ func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefini ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { - data := ec._MyMutationMiddleware(ctx, op) + data := ec._mutationMiddleware(ctx, op, func(ctx context.Context) (interface{}, error) { + return ec._MyMutation(ctx, op.SelectionSet), nil + }) var buf bytes.Buffer data.MarshalGQL(&buf) return buf.Bytes() @@ -206,70 +210,6 @@ type executionContext struct { *executableSchema } -func (ec *executionContext) _MyQueryMiddleware(ctx context.Context, obj *ast.OperationDefinition) graphql.Marshaler { - - next := func(ctx context.Context) (interface{}, error) { - return ec._MyQuery(ctx, obj.SelectionSet), nil - } - for _, d := range obj.Directives { - switch d.Name { - case "user": - rawArgs := d.ArgumentMap(ec.Variables) - args, err := ec.dir_user_args(ctx, rawArgs) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - n := next - next = func(ctx context.Context) (interface{}, error) { - return ec.directives.User(ctx, obj, n, args["id"].(int)) - } - } - } - tmp, err := next(ctx) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if data, ok := tmp.(graphql.Marshaler); ok { - return data - } - ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) - return graphql.Null -} - -func (ec *executionContext) _MyMutationMiddleware(ctx context.Context, obj *ast.OperationDefinition) graphql.Marshaler { - - next := func(ctx context.Context) (interface{}, error) { - return ec._MyMutation(ctx, obj.SelectionSet), nil - } - for _, d := range obj.Directives { - switch d.Name { - case "user": - rawArgs := d.ArgumentMap(ec.Variables) - args, err := ec.dir_user_args(ctx, rawArgs) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - n := next - next = func(ctx context.Context) (interface{}, error) { - return ec.directives.User(ctx, obj, n, args["id"].(int)) - } - } - } - tmp, err := next(ctx) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if data, ok := tmp.(graphql.Marshaler); ok { - return data - } - ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) - return graphql.Null -} - func (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) { defer func() { if r := recover(); r != nil { @@ -500,6 +440,70 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +func (ec *executionContext) _queryMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) graphql.Marshaler { + + for _, d := range obj.Directives { + switch d.Name { + case "user": + rawArgs := d.ArgumentMap(ec.Variables) + args, err := ec.dir_user_args(ctx, rawArgs) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + n := next + next = func(ctx context.Context) (interface{}, error) { + return ec.directives.User(ctx, obj, n, args["id"].(int)) + } + } + } + tmp, err := next(ctx) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if data, ok := tmp.(graphql.Marshaler); ok { + return data + } + ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) + return graphql.Null + +} + +func (ec *executionContext) _mutationMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) graphql.Marshaler { + + for _, d := range obj.Directives { + switch d.Name { + case "user": + rawArgs := d.ArgumentMap(ec.Variables) + args, err := ec.dir_user_args(ctx, rawArgs) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + n := next + next = func(ctx context.Context) (interface{}, error) { + return ec.directives.User(ctx, obj, n, args["id"].(int)) + } + } + } + tmp, err := next(ctx) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if data, ok := tmp.(graphql.Marshaler); ok { + return data + } + ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp) + return graphql.Null + +} + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _MyMutation_createTodo(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { diff --git a/example/type-system-extension/generated.go b/example/type-system-extension/generated.go index dd98089c711..42853d3dabb 100644 --- a/example/type-system-extension/generated.go +++ b/example/type-system-extension/generated.go @@ -439,6 +439,10 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _MyMutation_createTodo(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { diff --git a/integration/generated.go b/integration/generated.go index 36c283c220d..69815c31825 100644 --- a/integration/generated.go +++ b/integration/generated.go @@ -442,6 +442,10 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // endregion ***************************** args.gotpl ***************************** +// region ************************** directives.gotpl ************************** + +// endregion ************************** directives.gotpl ************************** + // region **************************** field.gotpl ***************************** func (ec *executionContext) _Element_child(ctx context.Context, field graphql.CollectedField, obj *models.Element) graphql.Marshaler {