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

Method concreteType

script/ghmock/gqlscaffold.go:241–280  ·  view source on GitHub ↗

concreteType picks the object type to build for a field whose type is an interface or union: the type in __typename, else the first fragment's type, else the first possible type by name.

(def *ast.Definition, obj map[string]any, sel ast.SelectionSet)

Source from the content-addressed store, hash-verified

239// interface or union: the type in __typename, else the first fragment's type,
240// else the first possible type by name.
241func (s *gqlScaffolder) concreteType(def *ast.Definition, obj map[string]any, sel ast.SelectionSet) *ast.Definition {
242 if !def.IsAbstractType() {
243 return def
244 }
245 possible := s.schema.GetPossibleTypes(def)
246 find := func(name string) *ast.Definition {
247 for _, t := range possible {
248 if t.Name == name {
249 return t
250 }
251 }
252 return nil
253 }
254 if name, ok := obj["__typename"].(string); ok {
255 if t := find(name); t != nil {
256 return t
257 }
258 }
259 var keys []string
260 groups := map[string][]*ast.Field{}
261 s.collect(sel, nil, &keys, groups)
262 for _, key := range keys {
263 if groups[key][0].Name == "__typename" {
264 if name, ok := obj[key].(string); ok {
265 if t := find(name); t != nil {
266 return t
267 }
268 }
269 }
270 }
271 if t := s.fragmentType(sel, find); t != nil {
272 return t
273 }
274 if len(possible) == 0 {
275 return def
276 }
277 sorted := append([]*ast.Definition(nil), possible...)
278 sort.Slice(sorted, func(i, j int) bool { return sorted[i].Name < sorted[j].Name })
279 return sorted[0]
280}
281
282// fragmentType finds the first fragment, in query order, on a possible type.
283func (s *gqlScaffolder) fragmentType(sel ast.SelectionSet, find func(string) *ast.Definition) *ast.Definition {

Callers 1

fillObjectMethod · 0.95

Calls 2

collectMethod · 0.95
fragmentTypeMethod · 0.95

Tested by

no test coverage detected