Coverage for graphqler / compiler / parsers / object_list_parser.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-20 10:09 -0400

1"""Simple singleton class to parse object listings from the introspection query""" 

2 

3from .parser import Parser 

4 

5 

6class ObjectListParser(Parser): 

7 def __init__(self): 

8 self.excluded_types = [ 

9 "__Schema", 

10 "__Type", 

11 "__TypeKind", 

12 "__Field", 

13 "__InputValue", 

14 "__EnumValue", 

15 "__Directive", 

16 "__DirectiveLocation", 

17 ] 

18 

19 def __extract_field_info(self, field): 

20 field_info = { 

21 "name": field["name"], 

22 "kind": field["type"]["kind"], 

23 "type": field["type"]["name"] if "name" in field["type"] else None, 

24 "inputs": self.extract_arg_info(field["args"]), 

25 "ofType": self.extract_oftype(field["type"]), 

26 } 

27 return field_info 

28 

29 def parse(self, introspection_data: dict) -> dict: 

30 """Parses the introspection data for only objects 

31 

32 Args: 

33 data (dict): Introspection JSON as a dictionary 

34 

35 Returns: 

36 dict: List of objects with their types 

37 """ 

38 # Grab just the objects from the dict 

39 schema_types = introspection_data.get("data", {}).get("__schema", {}).get("types", []) 

40 object_types = [t for t in schema_types if t.get("kind") == "OBJECT" and t.get("name") not in self.excluded_types] 

41 

42 # Convert it to the YAML structure we want 

43 object_info_dict = {} 

44 for obj in object_types: 

45 object_name = obj["name"] 

46 object_info_dict[object_name] = { 

47 "kind": obj["kind"], 

48 "name": object_name, 

49 "fields": [self.__extract_field_info(field) for field in obj["fields"]], 

50 } 

51 

52 return object_info_dict