Coverage for graphqler / compiler / parsers / query_list_parser.py: 96%

24 statements  

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

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

2 

3from .parser import Parser 

4 

5 

6class QueryListParser(Parser): 

7 def __init__(self): 

8 pass 

9 

10 def __extract_arg_info(self, field): 

11 input_args = {} 

12 for arg in field: 

13 arg_info = { 

14 "name": arg["name"], 

15 "type": arg["type"]["name"] if "name" in arg["type"] else None, 

16 "kind": arg["type"]["kind"] if "kind" in arg["type"] else None, 

17 "ofType": self.extract_oftype(arg["type"]), 

18 } 

19 input_args[arg["name"]] = arg_info 

20 return input_args 

21 

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

23 """Parses the introspection data for only objects 

24 

25 Args: 

26 data (dict): Introspection JSON as a dictionary 

27 

28 Returns: 

29 dict: List of objects with their types 

30 """ 

31 # Grab just the objects from the dict 

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

33 query_type_name = introspection_data.get("data", {}).get("__schema", {}).get("queryType", {}).get("name", "Query") 

34 queries_object = [t for t in schema_types if t.get("kind") == "OBJECT" and t.get("name") == query_type_name] 

35 

36 # No queries in the introspection 

37 if len(queries_object) == 0: 

38 return {} 

39 

40 queries = queries_object[0]["fields"] 

41 

42 # Convert it to the YAML structure we want 

43 query_info_dict = {} 

44 for query in queries: 

45 query_name = query["name"] 

46 query_args = self.__extract_arg_info(query["args"]) 

47 return_type = {"kind": query["type"]["kind"], "name": query["type"]["name"], "ofType": self.extract_oftype(query["type"]), "type": query["type"]["name"]} 

48 

49 query_info_dict[query_name] = { 

50 "name": query_name, 

51 "inputs": query_args, 

52 "output": return_type, 

53 } 

54 

55 return query_info_dict