Coverage for graphqler / compiler / parsers / parser.py: 88%
17 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-20 10:09 -0400
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-20 10:09 -0400
1"""Simple parser abstract class"""
4class Parser:
5 def __init__(self):
6 pass
8 def parse(self, introspection_data: dict) -> dict:
9 """Abtract parse method, should be overriden by children classes
11 Args:
12 introspection_result (dict): The introspection data
14 Raises:
15 Exception: Throws exception if this method isn't overriden by child class
17 Returns:
18 dict: The parse result
19 """
20 raise Exception("Should not call parse on base Parser class")
22 def extract_oftype(self, field: dict) -> dict | None:
23 """Extract the ofType. Assume that at the lowest level, nested ofType will always be null
25 Args:
26 field (dict): Field's "type" to extract from
28 Returns:
29 dict: The ofType dict
30 """
31 ofType = field["ofType"]
32 if ofType:
33 nested_ofType = self.extract_oftype(field["ofType"])
34 return {"kind": ofType["kind"], "name": ofType["name"], "ofType": nested_ofType, "type": ofType["name"]}
35 else:
36 return None
38 def extract_arg_info(self, args: list[dict]) -> dict:
39 """Extracts the arg information from a field
41 Args:
42 field (dict): An array of arguments
44 Returns:
45 dict: A dictionary of the arguments
46 """
47 input_args = {}
48 for arg in args:
49 arg_info = {
50 "name": arg["name"],
51 "description": arg["description"],
52 "type": arg["type"]["name"] if "name" in arg["type"] else None,
53 "kind": arg["type"]["kind"] if "kind" in arg["type"] else None,
54 "ofType": self.extract_oftype(arg["type"]),
55 "defaultValue": arg["defaultValue"],
56 }
57 input_args[arg["name"]] = arg_info
58 return input_args