CompleteValue applies the value completion algorithm to a single value, which could turn out to be a list or object or scalar value.
(
path []interface{},
field Field,
val interface{})
| 174 | // CompleteValue applies the value completion algorithm to a single value, which |
| 175 | // could turn out to be a list or object or scalar value. |
| 176 | func CompleteValue( |
| 177 | path []interface{}, |
| 178 | field Field, |
| 179 | val interface{}) ([]byte, x.GqlErrorList) { |
| 180 | |
| 181 | switch val := val.(type) { |
| 182 | case map[string]interface{}: |
| 183 | switch field.Type().Name() { |
| 184 | case "String", "ID", "Boolean", "Float", "Int", "Int64", "DateTime": |
| 185 | return nil, x.GqlErrorList{field.GqlErrorf(path, ErrExpectedScalar)} |
| 186 | } |
| 187 | enumValues := field.EnumValues() |
| 188 | if len(enumValues) > 0 { |
| 189 | return nil, x.GqlErrorList{field.GqlErrorf(path, ErrExpectedScalar)} |
| 190 | } |
| 191 | return CompleteObject(path, field.SelectionSet(), val) |
| 192 | case []interface{}: |
| 193 | return completeList(path, field, val) |
| 194 | case []map[string]interface{}: |
| 195 | // This case is different from the []interface{} case above and is true for admin queries |
| 196 | // where we built the val ourselves. |
| 197 | listVal := make([]interface{}, 0, len(val)) |
| 198 | for _, v := range val { |
| 199 | listVal = append(listVal, v) |
| 200 | } |
| 201 | return completeList(path, field, listVal) |
| 202 | default: |
| 203 | if val == nil { |
| 204 | if b := field.NullValue(); b != nil { |
| 205 | return b, nil |
| 206 | } |
| 207 | |
| 208 | return nil, x.GqlErrorList{field.GqlErrorf(path, ErrExpectedNonNull, |
| 209 | field.Name(), field.Type())} |
| 210 | } |
| 211 | |
| 212 | // val is a scalar |
| 213 | val, gqlErr := coerceScalar(val, field, path) |
| 214 | if len(gqlErr) > 0 { |
| 215 | return nil, gqlErr |
| 216 | } |
| 217 | |
| 218 | // Can this ever error? We can't have an unsupported type or value because |
| 219 | // we just unmarshalled this val. |
| 220 | b, err := json.Marshal(val) |
| 221 | if err != nil { |
| 222 | gqlErr := x.GqlErrorList{field.GqlErrorf(path, |
| 223 | "Error marshalling value for field '%s' (type %s). "+ |
| 224 | "Resolved as null (which may trigger GraphQL error propagation) ", |
| 225 | field.Name(), field.Type())} |
| 226 | |
| 227 | if field.Type().Nullable() { |
| 228 | return JsonNull, gqlErr |
| 229 | } |
| 230 | |
| 231 | return nil, gqlErr |
| 232 | } |
| 233 |
no test coverage detected