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

19 statements  

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

1""" 

2Enum objects: https://spec.graphql.org/June2018/#sec-Enum 

3""" 

4 

5from .parser import Parser 

6from typing import List 

7 

8 

9class EnumListParser(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_enum_values(self, enum_values: List[dict]) -> List[dict]: 

25 """Extract only the relavent fields from the possible enumeration values 

26 

27 Args: 

28 enum_values (List[dict]): List of possible values of the ENUM object 

29 

30 Returns: 

31 List[dict]: List of possible balues of the ENUM object but filtered for only relavent fields 

32 """ 

33 list_of_enum_values = [] 

34 for enum_value in enum_values: 

35 filtered_enum_value = {"name": enum_value["name"], "isDeprecated": enum_value["isDeprecated"]} 

36 list_of_enum_values.append(filtered_enum_value) 

37 return list_of_enum_values 

38 

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

40 """Parses the introspection data for only objects 

41 

42 Args: 

43 data (dict): Introspection JSON as a dictionary 

44 

45 Returns: 

46 dict: List of objects with their types 

47 """ 

48 # Grab just the objects from the dict 

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

50 enum_objects = [t for t in schema_types if t.get("kind") == "ENUM" and t.get("name") not in self.excluded_types] 

51 

52 built_enum_objects = {} 

53 for enum_object in enum_objects: 

54 enum_name = enum_object["name"] 

55 built_enum_objects[enum_name] = {"enumValues": self.extract_enum_values(enum_object["enumValues"])} 

56 return built_enum_objects