aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/codegen/type.gotpl
blob: cb2782c39edf9ecb0c9f066139e73c28292162ca (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
{{- range $type := .ReferencedTypes }}
	{{ with $type.UnmarshalFunc }}
		func (ec *executionContext) {{ . }}(ctx context.Context, v interface{}) ({{ $type.GO | ref }}, error) {
			{{- if $type.IsNilable }}
				if v == nil { return nil, nil }
			{{- end }}
			{{- if $type.IsPtr }}
				res, err := ec.{{ $type.Elem.UnmarshalFunc }}(ctx, v)
				return &res, err
			{{- else if $type.IsSlice }}
				var vSlice []interface{}
				if v != nil {
					if tmp1, ok := v.([]interface{}); ok {
						vSlice = tmp1
					} else {
						vSlice = []interface{}{ v }
					}
				}
				var err error
				res := make([]{{$type.GO.Elem | ref}}, len(vSlice))
				for i := range vSlice {
					res[i], err = ec.{{ $type.Elem.UnmarshalFunc }}(ctx, vSlice[i])
					if err != nil {
						return nil, err
					}
				}
				return res, nil
			{{- else }}
				{{- if $type.Unmarshaler }}
					{{- if $type.CastType }}
						tmp, err := {{ $type.Unmarshaler | call }}(v)
						return {{ $type.GO | ref }}(tmp), err
					{{- else}}
						return {{ $type.Unmarshaler | call }}(v)
					{{- end }}
				{{- else if eq ($type.GO | ref) "map[string]interface{}" }}
					return v.(map[string]interface{}), nil
				{{- else if $type.IsMarshaler -}}
					var res {{ $type.GO | ref }}
					return res, res.UnmarshalGQL(v)
				{{- else }}
					return ec.unmarshalInput{{ $type.GQL.Name }}(ctx, v)
				{{- end }}
			{{- end }}
		}
	{{- end }}

	{{ with $type.MarshalFunc }}
		func (ec *executionContext) {{ . }}(ctx context.Context, sel ast.SelectionSet, v {{ $type.GO | ref }}) graphql.Marshaler {
			{{- if $type.IsNilable }}
				if v == nil {
					{{- if $type.GQL.NonNull }}
						if !ec.HasError(graphql.GetResolverContext(ctx)) {
							ec.Errorf(ctx, "must not be null")
						}
					{{- end }}
					return graphql.Null
				}
			{{- end }}

			{{- if $type.IsSlice }}
				{{- if not $type.GQL.NonNull }}
					if v == nil {
						return graphql.Null
					}
				{{- end }}
				ret := make(graphql.Array, len(v))
				{{- if not $type.IsScalar }}
					var wg sync.WaitGroup
					isLen1 := len(v) == 1
					if !isLen1 {
						wg.Add(len(v))
					}
				{{- end }}
				for i := range v {
					{{- if not $type.IsScalar }}
						i := i
						rctx := &graphql.ResolverContext{
							Index: &i,
							Result: &v[i],
						}
						ctx := graphql.WithResolverContext(ctx, rctx)
						f := func(i int) {
							defer func() {
                        		if r := recover(); r != nil {
                        			ec.Error(ctx, ec.Recover(ctx, r))
                        			ret = nil
                        		}
                        	}()
							if !isLen1 {
								defer wg.Done()
							}
							ret[i] = ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, v[i])
						}
						if isLen1 {
							f(i)
						} else {
							go f(i)
						}
					{{ else }}
						ret[i] = ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, v[i])
					{{- end}}
				}
				{{ if not $type.IsScalar }} wg.Wait() {{ end }}
				return ret
			{{- else }}

				{{- if $type.IsMarshaler }}
					return v
				{{- else if $type.Marshaler }}
					{{- if $type.IsPtr }}
						return ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, *v)
					{{- else if $type.GQL.NonNull }}
							res := {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}(v){{else}}v{{- end }})
							if res == graphql.Null {
								if !ec.HasError(graphql.GetResolverContext(ctx)) {
									ec.Errorf(ctx, "must not be null")
								}
							}
							return res
					{{- else }}
						return {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}(v){{else}}v{{- end }})
					{{- end }}
				{{- else }}
					return ec._{{$type.Definition.Name}}(ctx, sel, {{ if not $type.IsNilable}}&{{end}} v)
				{{- end }}
			{{- end }}
		}
	{{- end }}
{{- end }}