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)
| 239 | // interface or union: the type in __typename, else the first fragment's type, |
| 240 | // else the first possible type by name. |
| 241 | func (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. |
| 283 | func (s *gqlScaffolder) fragmentType(sel ast.SelectionSet, find func(string) *ast.Definition) *ast.Definition { |
no test coverage detected