MCPcopy
hub / github.com/dgraph-io/dgraph / CompleteValue

Function CompleteValue

graphql/schema/completion.go:176–236  ·  view source on GitHub ↗

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{})

Source from the content-addressed store, hash-verified

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.
176func 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

Callers 3

CompleteObjectFunction · 0.85
completeListFunction · 0.85
mismatchedFunction · 0.85

Calls 10

CompleteObjectFunction · 0.85
completeListFunction · 0.85
coerceScalarFunction · 0.85
NameMethod · 0.65
TypeMethod · 0.65
GqlErrorfMethod · 0.65
EnumValuesMethod · 0.65
SelectionSetMethod · 0.65
NullValueMethod · 0.65
NullableMethod · 0.65

Tested by

no test coverage detected