directives.gotpl

  1{{ define "implDirectives" }}{{ $in := .DirectiveObjName }}
  2	{{- range $i, $directive := .ImplDirectives -}}
  3		directive{{add $i 1}} := func(ctx context.Context) (interface{}, error) {
  4			{{- range $arg := $directive.Args }}
  5				{{- if notNil "Value" $arg }}
  6						{{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Value | dump }})
  7						if err != nil{
  8							return nil, err
  9						}
 10					{{- else if notNil "Default" $arg }}
 11						{{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Default | dump }})
 12						if err != nil{
 13							return nil, err
 14						}
 15					{{- end }}
 16			{{- end }}
 17			return ec.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs $in $i }})
 18		}
 19	{{- end -}}
 20{{ end }}
 21
 22{{define "queryDirectives"}}
 23	for _, d := range obj.Directives {
 24		switch d.Name {
 25		{{- range $directive := . }}
 26		case "{{$directive.Name}}":
 27			{{- if $directive.Args }}
 28				rawArgs := d.ArgumentMap(ec.Variables)
 29				args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs)
 30				if err != nil {
 31					ec.Error(ctx, err)
 32					return graphql.Null
 33				}
 34			{{- end }}
 35			n := next
 36			next = func(ctx context.Context) (interface{}, error) {
 37				return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
 38			}
 39		{{- end }}
 40		}
 41	}
 42	tmp, err := next(ctx)
 43	if err != nil {
 44		ec.Error(ctx, err)
 45		return graphql.Null
 46	}
 47	if data, ok := tmp.(graphql.Marshaler); ok {
 48		return data
 49	}
 50	ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp)
 51	return graphql.Null
 52{{end}}
 53
 54{{ if .Directives.LocationDirectives "QUERY" }}
 55func (ec *executionContext) _queryMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) graphql.Marshaler {
 56	{{ template "queryDirectives" .Directives.LocationDirectives "QUERY" }}
 57}
 58{{ end }}
 59
 60{{ if .Directives.LocationDirectives "MUTATION" }}
 61func (ec *executionContext) _mutationMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) graphql.Marshaler {
 62	{{ template "queryDirectives" .Directives.LocationDirectives "MUTATION" }}
 63}
 64{{ end }}
 65
 66{{ if .Directives.LocationDirectives "SUBSCRIPTION" }}
 67func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *ast.OperationDefinition, next func(ctx context.Context) (interface{}, error)) func() graphql.Marshaler {
 68	for _, d := range obj.Directives {
 69		switch d.Name {
 70		{{- range $directive := .Directives.LocationDirectives "SUBSCRIPTION" }}
 71		case "{{$directive.Name}}":
 72			{{- if $directive.Args }}
 73				rawArgs := d.ArgumentMap(ec.Variables)
 74				args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs)
 75				if err != nil {
 76					ec.Error(ctx, err)
 77					return func() graphql.Marshaler {
 78						return graphql.Null
 79					}
 80				}
 81			{{- end }}
 82			n := next
 83			next = func(ctx context.Context) (interface{}, error) {
 84				return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
 85			}
 86		{{- end }}
 87		}
 88	}
 89	tmp, err := next(ctx)
 90	if err != nil {
 91		ec.Error(ctx, err)
 92		return func() graphql.Marshaler {
 93			return graphql.Null
 94		}
 95	}
 96	if data, ok := tmp.(func() graphql.Marshaler); ok {
 97		return data
 98	}
 99	ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp)
100	return func() graphql.Marshaler {
101		return graphql.Null
102	}
103}
104{{ end }}
105
106{{ if .Directives.LocationDirectives "FIELD" }}
107	func (ec *executionContext) _fieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) interface{} {
108		{{- if .Directives.LocationDirectives "FIELD" }}
109		rctx := graphql.GetResolverContext(ctx)
110		for _, d := range rctx.Field.Directives {
111			switch d.Name {
112			{{- range $directive := .Directives.LocationDirectives "FIELD" }}
113			case "{{$directive.Name}}":
114				{{- if $directive.Args }}
115					rawArgs := d.ArgumentMap(ec.Variables)
116					args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs)
117					if err != nil {
118						ec.Error(ctx, err)
119						return nil
120					}
121				{{- end }}
122				n := next
123				next = func(ctx context.Context) (interface{}, error) {
124					return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
125				}
126			{{- end }}
127			}
128		}
129		{{- end }}
130		res, err := ec.ResolverMiddleware(ctx, next)
131		if err != nil {
132			ec.Error(ctx, err)
133			return nil
134		}
135		return res
136	}
137{{ end }}