Coverage for graphqler / utils / api.py: 90%

49 statements  

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

1"""Class for the target API 

2- Used to retrieve information about the API 

3""" 

4 

5from pathlib import Path 

6from graphqler import config 

7from graphqler.utils.file_utils import read_yaml_to_dict 

8 

9 

10class API: 

11 """Variables for the API""" 

12 

13 queries = {} 

14 objects = {} 

15 mutations = {} 

16 input_objects = {} 

17 enums = {} 

18 unions = {} 

19 interfaces = {} 

20 

21 def __init__(self, url: str = "http://localhost:4000/graphql", save_path: Path | str = "output/"): 

22 self.compiled_queries_save_path = Path(save_path) / config.COMPILED_QUERIES_FILE_NAME 

23 self.compiled_objects_save_path = Path(save_path) / config.COMPILED_OBJECTS_FILE_NAME 

24 self.compiled_mutations_save_path = Path(save_path) / config.COMPILED_MUTATIONS_FILE_NAME 

25 self.extracted_enums_save_path = Path(save_path) / config.ENUM_LIST_FILE_NAME 

26 self.extracted_input_objects_save_path = Path(save_path) / config.INPUT_OBJECT_LIST_FILE_NAME 

27 self.extracted_unions_save_path = Path(save_path) / config.UNION_LIST_FILE_NAME 

28 self.extracted_interfaces_save_path = Path(save_path) / config.INTERFACE_LIST_FILE_NAME 

29 

30 self.url = url 

31 self.queries = read_yaml_to_dict(self.compiled_queries_save_path) 

32 self.objects = read_yaml_to_dict(self.compiled_objects_save_path) 

33 self.mutations = read_yaml_to_dict(self.compiled_mutations_save_path) 

34 self.input_objects = read_yaml_to_dict(self.extracted_input_objects_save_path) 

35 self.enums = read_yaml_to_dict(self.extracted_enums_save_path) 

36 self.unions = read_yaml_to_dict(self.extracted_unions_save_path) 

37 self.interfaces = read_yaml_to_dict(self.extracted_interfaces_save_path) 

38 

39 def get_num_queries(self) -> int: 

40 """Gets the number of queries 

41 

42 Returns: 

43 int: The number of queries 

44 """ 

45 return len(self.queries.keys()) 

46 

47 def get_num_mutations(self) -> int: 

48 """Gets the number of mutations 

49 

50 Returns: 

51 int: The number of mutations 

52 """ 

53 return len(self.mutations.keys()) 

54 

55 def get_num_objects(self) -> int: 

56 """Gets the number of objects 

57 

58 Returns: 

59 int: The number of objects 

60 """ 

61 return len(self.objects.keys()) 

62 

63 def get_num_input_objects(self) -> int: 

64 """Gets the number of input objects 

65 

66 Returns: 

67 int: The number of input objects 

68 """ 

69 return len(self.input_objects.keys()) 

70 

71 def get_num_enums(self) -> int: 

72 """Gets the number of enums 

73 

74 Returns: 

75 int: The number of enums 

76 """ 

77 return len(self.enums.keys()) 

78 

79 def get_num_unions(self) -> int: 

80 """Gets the number of unions 

81 

82 Returns: 

83 int: The number of unions 

84 """ 

85 return len(self.unions.keys()) 

86 

87 def get_num_interfaces(self) -> int: 

88 """Gets the number of interfaces 

89 

90 Returns: 

91 int: The number of interfaces 

92 """ 

93 return len(self.interfaces.keys()) 

94 

95 def is_operation_in_api(self, operation: str) -> bool: 

96 """Checks if the operation is in the API 

97 

98 Args: 

99 operation (str): The operation 

100 

101 Returns: 

102 bool: True if the operation is in the API, False otherwise 

103 """ 

104 return operation in self.queries or operation in self.mutations 

105 

106 def get_operation(self, operation: str) -> dict: 

107 """Gets the operation from the API 

108 

109 Args: 

110 operation (str): The operation 

111 

112 Returns: 

113 dict: The operation 

114 """ 

115 if operation in self.queries: 

116 return self.queries[operation] 

117 if operation in self.mutations: 

118 return self.mutations[operation] 

119 return {}