github.com/shippio/gqlgen@v0.0.0-20220912092219-633ea699ef07/codegen/type.gotpl (about)

     1  {{- range $type := .ReferencedTypes }}
     2  	{{ with $type.UnmarshalFunc }}
     3  		func (ec *executionContext) {{ . }}(ctx context.Context, v interface{}) ({{ $type.GO | ref }}, error) {
     4  			{{- if and $type.IsNilable (not $type.GQL.NonNull) (not $type.IsPtrToPtr) }}
     5  				if v == nil { return nil, nil }
     6  			{{- end }}
     7  			{{- if $type.IsPtrToSlice }}
     8  				res, err := ec.{{ $type.Elem.UnmarshalFunc }}(ctx, v)
     9  				return &res, graphql.ErrorOnPath(ctx, err)
    10  			{{- else if $type.IsSlice }}
    11  				var vSlice []interface{}
    12  				if v != nil {
    13  					vSlice = graphql.CoerceList(v)
    14  				}
    15  				var err error
    16  				res := make([]{{$type.GO.Elem | ref}}, len(vSlice))
    17  				for i := range vSlice {
    18  					ctx := graphql.WithPathContext(ctx, graphql.NewPathWithIndex(i))
    19  					res[i], err = ec.{{ $type.Elem.UnmarshalFunc }}(ctx, vSlice[i])
    20  					if err != nil {
    21  						return nil, err
    22  					}
    23  				}
    24  				return res, nil
    25  			{{- else if and $type.IsPtrToPtr (not $type.Unmarshaler) (not $type.IsMarshaler) }}
    26  				var pres {{ $type.Elem.GO | ref }}
    27  				if v != nil {
    28  					res, err := ec.{{ $type.Elem.UnmarshalFunc }}(ctx, v)
    29  					if err != nil {
    30  						return nil, graphql.ErrorOnPath(ctx, err)
    31  					}
    32  					pres = res
    33  				}
    34  				return &pres, nil
    35  			{{- else }}
    36  				{{- if $type.Unmarshaler }}
    37  					{{- if $type.CastType }}
    38  						{{- if $type.IsContext }}
    39  							tmp, err := {{ $type.Unmarshaler | call }}(ctx, v)
    40  						{{- else }}
    41  							tmp, err := {{ $type.Unmarshaler | call }}(v)
    42  						{{- end }}
    43  						{{- if and $type.IsNilable $type.Elem }}
    44  							res := {{ $type.Elem.GO | ref }}(tmp)
    45  						{{- else}}
    46  							res := {{ $type.GO | ref }}(tmp)
    47  						{{- end }}
    48  					{{- else}}
    49  						{{- if $type.IsContext }}
    50  							res, err := {{ $type.Unmarshaler | call }}(ctx, v)
    51  						{{- else }}
    52  							res, err := {{ $type.Unmarshaler | call }}(v)
    53  						{{- end }}
    54  					{{- end }}
    55  					{{- if and $type.IsTargetNilable (not $type.IsNilable) }}
    56  						return *res, graphql.ErrorOnPath(ctx, err)
    57  					{{- else if and (not $type.IsTargetNilable) $type.IsNilable }}
    58  						return &res, graphql.ErrorOnPath(ctx, err)
    59  					{{- else}}
    60  						return res, graphql.ErrorOnPath(ctx, err)
    61  					{{- end }}
    62  				{{- else if eq ($type.GO | ref) "map[string]interface{}" }}
    63  					return v.(map[string]interface{}), nil
    64  				{{- else if $type.IsMarshaler }}
    65  					{{- if and $type.IsNilable $type.Elem }}
    66  						var res = new({{ $type.Elem.GO | ref }})
    67  					{{- else}}
    68  						var res {{ $type.GO | ref }}
    69  					{{- end }}
    70  					{{- if $type.IsContext }}
    71  						err := res.UnmarshalGQLContext(ctx, v)
    72  					{{- else }}
    73  						err := res.UnmarshalGQL(v)
    74  					{{- end }}
    75  					return res, graphql.ErrorOnPath(ctx, err)
    76  				{{- else }}
    77  					res, err := ec.unmarshalInput{{ $type.GQL.Name }}(ctx, v)
    78  					{{- if $type.IsNilable }}
    79  						return &res, graphql.ErrorOnPath(ctx, err)
    80  					{{- else}}
    81  						return res, graphql.ErrorOnPath(ctx, err)
    82  					{{- end }}
    83  				{{- end }}
    84  			{{- end }}
    85  		}
    86  	{{- end }}
    87  
    88  	{{ with $type.MarshalFunc }}
    89  		func (ec *executionContext) {{ . }}(ctx context.Context, sel ast.SelectionSet, v {{ $type.GO | ref }}) graphql.Marshaler {
    90  			{{- if $type.IsPtrToSlice }}
    91  				return ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, *v)
    92  			{{- else if $type.IsSlice }}
    93  				{{- if not $type.GQL.NonNull }}
    94  					if v == nil {
    95  						return graphql.Null
    96  					}
    97  				{{- end }}
    98  				ret := make(graphql.Array, len(v))
    99  				{{- if not $type.IsScalar }}
   100  					var wg sync.WaitGroup
   101  					isLen1 := len(v) == 1
   102  					if !isLen1 {
   103  						wg.Add(len(v))
   104  					}
   105  				{{- end }}
   106  				for i := range v {
   107  					{{- if not $type.IsScalar }}
   108  						i := i
   109  						fc := &graphql.FieldContext{
   110  							Index: &i,
   111  							Result: &v[i],
   112  						}
   113  						ctx := graphql.WithFieldContext(ctx, fc)
   114  						f := func(i int) {
   115  							defer func() {
   116  								if r := recover(); r != nil {
   117  									ec.Error(ctx, ec.Recover(ctx, r))
   118  									ret = nil
   119  								}
   120  							}()
   121  							if !isLen1 {
   122  								defer wg.Done()
   123  							}
   124  							ret[i] = ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, v[i])
   125  						}
   126  						if isLen1 {
   127  							f(i)
   128  						} else {
   129  							go f(i)
   130  						}
   131  					{{ else }}
   132  						ret[i] = ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, v[i])
   133  					{{- end }}
   134  				}
   135  				{{ if not $type.IsScalar }} wg.Wait() {{ end }}
   136  				{{ if $type.Elem.GQL.NonNull }}
   137  					for _, e := range ret {
   138  						if e == graphql.Null {
   139  							return graphql.Null
   140  						}
   141  					}
   142  				{{ end }}
   143  				return ret
   144  			{{- else if and $type.IsPtrToPtr (not $type.Unmarshaler) (not $type.IsMarshaler) }}
   145  				if v == nil {
   146  					return graphql.Null
   147  				}
   148  				return ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, *v)
   149  			{{- else }}
   150  				{{- if $type.IsNilable }}
   151  					if v == nil {
   152  						{{- if $type.GQL.NonNull }}
   153  							if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
   154  								ec.Errorf(ctx, "the requested element is null which the schema does not allow")
   155  							}
   156  						{{- end }}
   157  						return graphql.Null
   158  					}
   159  				{{- end }}
   160  				{{- if $type.IsMarshaler }}
   161  					{{- if $type.IsContext }}
   162  						return graphql.WrapContextMarshaler(ctx, v)
   163  					{{- else }}
   164  						return v
   165  					{{- end }}
   166  				{{- else if $type.Marshaler }}
   167  					{{- $v := "v" }}
   168  					{{- if and $type.IsTargetNilable (not $type.IsNilable) }}
   169  						{{- $v = "&v" }}
   170  					{{- else if and (not $type.IsTargetNilable) $type.IsNilable }}
   171  						{{- $v = "*v" }}
   172  					{{- end }}
   173  					res := {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}({{ $v }}){{else}}{{ $v }}{{- end }})
   174  					{{- if $type.GQL.NonNull }}
   175  						if res == graphql.Null {
   176  							if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
   177  								ec.Errorf(ctx, "the requested element is null which the schema does not allow")
   178  							}
   179  						}
   180  					{{- end }}
   181  					{{- if $type.IsContext }}
   182  						return graphql.WrapContextMarshaler(ctx, res)
   183  					{{- else }}
   184  						return res
   185  					{{- end }}
   186  				{{- else }}
   187  					return ec._{{$type.Definition.Name}}(ctx, sel, {{ if not $type.IsNilable}}&{{end}} v)
   188  				{{- end }}
   189  			{{- end }}
   190  		}
   191  	{{- end }}
   192  {{- end }}