aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/codegen/directives.gotpl
blob: 67a0783e6500e2febe0013a02a20512d204eece8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{{ define "implDirectives" }}{{ $in := .DirectiveObjName }}
	{{- range $i, $directive := .ImplDirectives -}}
		directive{{add $i 1}} := func(ctx context.Context) (interface{}, error) {
			{{- range $arg := $directive.Args }}
				{{- if notNil "Value" $arg }}
						{{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Value | dump }})
						if err != nil{
							return nil, err
						}
					{{- else if notNil "Default" $arg }}
						{{ $arg.VarName }}, err := ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, {{ $arg.Default | dump }})
						if err != nil{
							return nil, err
						}
					{{- end }}
			{{- end }}
			return ec.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs $in $i }})
		}
	{{- end -}}
{{ end }}

{{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 }}

{{ if .Directives.LocationDirectives "FIELD" }}
	func (ec *executionContext) _fieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) interface{} {
		{{- if .Directives.LocationDirectives "FIELD" }}
		rctx := graphql.GetResolverContext(ctx)
		for _, d := range rctx.Field.Directives {
			switch d.Name {
			{{- range $directive := .Directives.LocationDirectives "FIELD" }}
			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 nil
					}
				{{- end }}
				n := next
				next = func(ctx context.Context) (interface{}, error) {
					return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
				}
			{{- end }}
			}
		}
		{{- end }}
		res, err := ec.ResolverMiddleware(ctx, next)
		if err != nil {
			ec.Error(ctx, err)
			return nil
		}
		return res
	}
{{ end }}