Coverage for graphqler / compiler / parsers / interface_list_parser.py: 59%

22 statements  

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

1""" 

2Interface objects: https://spec.graphql.org/October2021/#sec-Interfaces 

3""" 

4 

5from .parser import Parser 

6from typing import List 

7 

8 

9class InterfaceListParser(Parser): 

10 def __init__(self): 

11 self.excluded_types = [ 

12 "Mutation", 

13 "Query", 

14 "__Schema", 

15 "__Type", 

16 "__TypeKind", 

17 "__Field", 

18 "__InputValue", 

19 "__EnumValue", 

20 "__Directive", 

21 "__DirectiveLocation", 

22 ] 

23 

24 def __extract_field_info(self, field): 

25 field_info = { 

26 "name": field["name"], 

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

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

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

30 } 

31 return field_info 

32 

33 def extract_possible_types(self, possible_types: List[dict]) -> List[dict]: 

34 """Extract the possible types from the interface 

35 

36 Args: 

37 possible_types (List[dict]): List of possible types of the INTERFACE object 

38 

39 Returns: 

40 List[dict]: List of possible values of the interface object 

41 """ 

42 possible_types_list = [] 

43 for possible_type in possible_types: 

44 formatted_possible_type = { 

45 "kind": possible_type["kind"], 

46 "name": possible_type["name"], 

47 "ofType": possible_type["ofType"], 

48 "type": possible_type["name"] if possible_type["kind"] == "OBJECT" else None, 

49 } 

50 possible_types_list.append(formatted_possible_type) 

51 return possible_types_list 

52 

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

54 """Parses the introspection data for only INTERFACE objects 

55 

56 Args: 

57 data (dict): Introspection JSON as a dictionary 

58 

59 Returns: 

60 dict: Dictionary of INTERFACE objects with their types 

61 """ 

62 # Grab just the objects from the dict 

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

64 interfaces = [t for t in schema_types if t.get("kind") == "INTERFACE" and t.get("name") not in self.excluded_types] 

65 

66 interfaces_dict = {} 

67 for interface in interfaces: 

68 interface_name = interface["name"] 

69 interfaces_dict[interface_name] = { 

70 "kind": interface["kind"], 

71 "name": interface["name"], 

72 "fields": [self.__extract_field_info(field) for field in interface["fields"]], 

73 "possibleTypes": self.extract_possible_types(interface["possibleTypes"]), 

74 } 

75 

76 return interfaces_dict