github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/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 {{ if $type.Elem.GQL.NonNull }} 114 for _, e := range ret { 115 if e == graphql.Null { 116 return graphql.Null 117 } 118 } 119 {{ end }} 120 return ret 121 {{- else }} 122 {{- if $type.IsNilable }} 123 if v == nil { 124 {{- if $type.GQL.NonNull }} 125 if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { 126 ec.Errorf(ctx, "must not be null") 127 } 128 {{- end }} 129 return graphql.Null 130 } 131 {{- end }} 132 {{- if $type.IsMarshaler }} 133 return v 134 {{- else if $type.Marshaler }} 135 {{- $v := "v" }} 136 {{- if and $type.IsTargetNilable (not $type.IsNilable) }} 137 {{- $v = "&v" }} 138 {{- else if and (not $type.IsTargetNilable) $type.IsNilable }} 139 {{- $v = "*v" }} 140 {{- end }} 141 {{- if $type.GQL.NonNull }} 142 res := {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}({{ $v }}){{else}}{{ $v }}{{- end }}) 143 if res == graphql.Null { 144 if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { 145 ec.Errorf(ctx, "must not be null") 146 } 147 } 148 return res 149 {{- else }} 150 return {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}({{ $v }}){{else}}{{ $v }}{{- end }}) 151 {{- end }} 152 {{- else }} 153 return ec._{{$type.Definition.Name}}(ctx, sel, {{ if not $type.IsNilable}}&{{end}} v) 154 {{- end }} 155 {{- end }} 156 } 157 {{- end }} 158 {{- end }}