github.com/animeshon/gqlgen@v0.13.1-0.20210304133704-3a770431bb6d/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) }} 5 if v == nil { return nil, nil } 6 {{- end }} 7 {{- if $type.IsSlice }} 8 var vSlice []interface{} 9 if v != nil { 10 if tmp1, ok := v.([]interface{}); ok { 11 vSlice = tmp1 12 } else { 13 vSlice = []interface{}{ v } 14 } 15 } 16 var err error 17 res := make([]{{$type.GO.Elem | ref}}, len(vSlice)) 18 for i := range vSlice { 19 ctx := graphql.WithPathContext(ctx, graphql.NewPathWithIndex(i)) 20 res[i], err = ec.{{ $type.Elem.UnmarshalFunc }}(ctx, vSlice[i]) 21 if err != nil { 22 return nil, err 23 } 24 } 25 return res, nil 26 {{- else }} 27 {{- if $type.Unmarshaler }} 28 {{- if $type.CastType }} 29 tmp, err := {{ $type.Unmarshaler | call }}(v) 30 {{- if $type.IsNilable }} 31 res := {{ $type.Elem.GO | ref }}(tmp) 32 {{- else}} 33 res := {{ $type.GO | ref }}(tmp) 34 {{- end }} 35 {{- else}} 36 res, err := {{ $type.Unmarshaler | call }}(v) 37 {{- end }} 38 {{- if and $type.IsTargetNilable (not $type.IsNilable) }} 39 return *res, graphql.ErrorOnPath(ctx, err) 40 {{- else if and (not $type.IsTargetNilable) $type.IsNilable }} 41 return &res, graphql.ErrorOnPath(ctx, err) 42 {{- else}} 43 return res, graphql.ErrorOnPath(ctx, err) 44 {{- end }} 45 {{- else if eq ($type.GO | ref) "map[string]interface{}" }} 46 return v.(map[string]interface{}), nil 47 {{- else if $type.IsMarshaler }} 48 {{- if $type.IsNilable }} 49 var res = new({{ $type.Elem.GO | ref }}) 50 {{- else}} 51 var res {{ $type.GO | ref }} 52 {{- end }} 53 err := res.UnmarshalGQL(v) 54 return res, graphql.ErrorOnPath(ctx, err) 55 {{- else }} 56 res, err := ec.unmarshalInput{{ $type.GQL.Name }}(ctx, v) 57 {{- if $type.IsNilable }} 58 return &res, graphql.ErrorOnPath(ctx, err) 59 {{- else}} 60 return res, graphql.ErrorOnPath(ctx, err) 61 {{- end }} 62 {{- end }} 63 {{- end }} 64 } 65 {{- end }} 66 67 {{ with $type.MarshalFunc }} 68 func (ec *executionContext) {{ . }}(ctx context.Context, sel ast.SelectionSet, v {{ $type.GO | ref }}) graphql.Marshaler { 69 {{- if $type.IsSlice }} 70 {{- if not $type.GQL.NonNull }} 71 if v == nil { 72 return graphql.Null 73 } 74 {{- end }} 75 ret := make(graphql.Array, len(v)) 76 {{- if not $type.IsScalar }} 77 var wg sync.WaitGroup 78 isLen1 := len(v) == 1 79 if !isLen1 { 80 wg.Add(len(v)) 81 } 82 {{- end }} 83 for i := range v { 84 {{- if not $type.IsScalar }} 85 i := i 86 fc := &graphql.FieldContext{ 87 Index: &i, 88 Result: &v[i], 89 } 90 ctx := graphql.WithFieldContext(ctx, fc) 91 f := func(i int) { 92 defer func() { 93 if r := recover(); r != nil { 94 ec.Error(ctx, ec.Recover(ctx, r)) 95 ret = nil 96 } 97 }() 98 if !isLen1 { 99 defer wg.Done() 100 } 101 ret[i] = ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, v[i]) 102 } 103 if isLen1 { 104 f(i) 105 } else { 106 go f(i) 107 } 108 {{ else }} 109 ret[i] = ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, v[i]) 110 {{- end}} 111 } 112 {{ if not $type.IsScalar }} wg.Wait() {{ end }} 113 return ret 114 {{- else }} 115 {{- if $type.IsNilable }} 116 if v == nil { 117 {{- if $type.GQL.NonNull }} 118 if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { 119 ec.Errorf(ctx, "must not be null") 120 } 121 {{- end }} 122 return graphql.Null 123 } 124 {{- end }} 125 {{- if $type.IsMarshaler }} 126 return v 127 {{- else if $type.Marshaler }} 128 {{- $v := "v" }} 129 {{- if and $type.IsTargetNilable (not $type.IsNilable) }} 130 {{- $v = "&v" }} 131 {{- else if and (not $type.IsTargetNilable) $type.IsNilable }} 132 {{- $v = "*v" }} 133 {{- end }} 134 {{- if $type.GQL.NonNull }} 135 res := {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}({{ $v }}){{else}}{{ $v }}{{- end }}) 136 if res == graphql.Null { 137 if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { 138 ec.Errorf(ctx, "must not be null") 139 } 140 } 141 return res 142 {{- else }} 143 return {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}({{ $v }}){{else}}{{ $v }}{{- end }}) 144 {{- end }} 145 {{- else }} 146 return ec._{{$type.Definition.Name}}(ctx, sel, {{ if not $type.IsNilable}}&{{end}} v) 147 {{- end }} 148 {{- end }} 149 } 150 {{- end }} 151 {{- end }}