MCPcopy Create free account
hub / github.com/cli/cli / checkObject

Method checkObject

script/ghmock/graphqlcheck.go:316–376  ·  view source on GitHub ↗
(path string, obj map[string]any, def *ast.Definition, sel ast.SelectionSet, required bool)

Source from the content-addressed store, hash-verified

314}
315
316func (w *gqlWalker) checkObject(path string, obj map[string]any, def *ast.Definition, sel ast.SelectionSet, required bool) {
317 concrete := def
318 if def.IsAbstractType() {
319 concrete = nil
320 keys := []string{"__typename"}
321 var candidates []selected
322 w.collectFields(sel, nil, false, false, &candidates)
323 for _, candidate := range candidates {
324 if candidate.field.Name == "__typename" && candidate.field.Alias != "__typename" {
325 keys = append(keys, candidate.field.Alias)
326 }
327 }
328 for _, key := range keys {
329 name, ok := obj[key].(string)
330 if !ok {
331 continue
332 }
333 if t := w.schema.Types[name]; t != nil && t.Kind == ast.Object && w.isA(t, def.Name) {
334 if concrete != nil && concrete != t {
335 w.addf(path+"."+key, "is %q, but this object's type is %s", name, concrete.Name)
336 continue
337 }
338 concrete = t
339 } else {
340 w.addf(path+"."+key, "%q isn't a type %s can be; it can be %s",
341 name, def.Name, listNames(possibleTypeNames(w.schema, def), 10))
342 }
343 }
344 }
345
346 var fields []selected
347 w.collectFields(sel, concrete, required, true, &fields)
348 for _, f := range fields {
349 if f.field.Definition == nil && f.field.Name != "__typename" {
350 // The query asks for a field the schema doesn't have. That's
351 // reported as a query problem.
352 continue
353 }
354 key := f.field.Alias
355 child := path + "." + key
356 val, ok := obj[key]
357 if !ok {
358 if f.required {
359 w.addf(child, "missing, but gh asked for it")
360 }
361 continue
362 }
363 if !f.checked {
364 continue
365 }
366 if f.field.Name == "__typename" {
367 if _, ok := val.(string); !ok {
368 w.addf(child, "should be a string, but is %s", describeJSON(val))
369 } else if !def.IsAbstractType() && val != def.Name {
370 w.addf(child, "is %q, but this object's type is %s", val, def.Name)
371 }
372 continue
373 }

Callers 2

checkGraphQLResponseFunction · 0.95
checkValueMethod · 0.95

Calls 7

collectFieldsMethod · 0.95
isAMethod · 0.95
addfMethod · 0.95
checkValueMethod · 0.95
listNamesFunction · 0.85
possibleTypeNamesFunction · 0.85
describeJSONFunction · 0.85

Tested by

no test coverage detected