Skip to content

Commit

Permalink
move directive to directives.gotpl
Browse files Browse the repository at this point in the history
  • Loading branch information
asamusev committed Jun 18, 2019
1 parent cfdbc39 commit 69d7e28
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 256 deletions.
34 changes: 14 additions & 20 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 13 additions & 5 deletions codegen/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,35 @@ 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
Args []*FieldArgument
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
}
}
Expand Down
84 changes: 84 additions & 0 deletions codegen/directives.gotpl
Original file line number Diff line number Diff line change
@@ -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 }}
138 changes: 12 additions & 126 deletions codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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 }}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 69d7e28

Please sign in to comment.