Coverage for graphqler / utils / parser_utils.py: 83%

23 statements  

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

1""" 

2Utilities that will come in handy when parsing the various dictionaries 

3""" 

4 

5 

6def get_base_oftype(oftype: dict) -> dict: 

7 """Gets the base oftype from a NON_NULL/LIST oftype (recursively goes down) 

8 

9 Args: 

10 oftype (dict): Oftype to get 

11 

12 Returns: 

13 dict: the base oftype with kind, name, and ofType 

14 """ 

15 if "ofType" in oftype and oftype["ofType"] is not None: 

16 return get_base_oftype(oftype["ofType"]) 

17 else: 

18 return oftype 

19 

20 

21def get_output_type(operation_name: str, operations: dict) -> str: 

22 """Gets the mutation/query's output type. If it's a SCALAR, just returns the name of the field 

23 If it's an object, returns the Object's name 

24 

25 Args: 

26 payload_name (str): The name of either the mutation or query 

27 payloads (dict): QUERIES or MUTATIONS in this API 

28 

29 Returns: 

30 str: The output name 

31 """ 

32 payload_info = operations[operation_name] 

33 if payload_info["output"]["ofType"] is not None: 

34 type_to_parse = get_base_oftype(payload_info["output"]["ofType"]) 

35 else: 

36 type_to_parse = payload_info["output"] 

37 

38 if type_to_parse["kind"] == "OBJECT": 

39 return type_to_parse["type"] 

40 else: 

41 return type_to_parse["name"] 

42 

43 

44def get_output_type_from_details(operation_details: dict) -> str: 

45 """Gets the output type from the operation details 

46 

47 Args: 

48 operation_details (str): The operation details 

49 

50 Returns: 

51 str: The output type 

52 """ 

53 if operation_details["output"]["ofType"] is not None: 

54 type_to_parse = get_base_oftype(operation_details["output"]["ofType"]) 

55 else: 

56 type_to_parse = operation_details["output"] 

57 

58 if type_to_parse["kind"] == "OBJECT": 

59 return type_to_parse["type"] 

60 else: 

61 return type_to_parse["name"] 

62 

63 

64def is_simple_scalar(output_field: dict) -> bool: 

65 """Determines if the output_field is a simple scalar. Useful for getting a minimal response 

66 Check the following: 

67 - The kind is SCALAR 

68 - Has no inpputs 

69 - Has no ofType 

70 

71 Args: 

72 output_field (dict): The output field 

73 

74 Returns: 

75 bool: True if it's a simple scalar, False otherwise 

76 """ 

77 base_oftype = get_base_oftype(output_field) 

78 has_inputs = ('inputs' in base_oftype and len(base_oftype["inputs"]) > 0 

79 or 'inputs' in output_field and len(output_field["inputs"]) > 0) 

80 return base_oftype["kind"] == "SCALAR" and base_oftype["ofType"] is None and not has_inputs