Coverage for graphqler / compiler / resolvers / object_method_resolver.py: 92%

40 statements  

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

1"""Related queries and mutations to objects based on output type. 

2We only look at the output to determine if a method is related to an object 

3""" 

4 

5 

6class ObjectMethodResolver: 

7 def __init__(self): 

8 pass 

9 

10 def resolve(self, objects: dict, queries: dict, mutations: dict) -> dict: 

11 """Resolves the objects by attaching the correlated queries/mutations that output this object 

12 

13 Args: 

14 objects (dict): The objects available 

15 queries (dict): The queries available 

16 mutations (dict): The mutations available 

17 

18 Returns: 

19 dict: The objects dict enriched with a queries key 

20 """ 

21 

22 object_query_mapping = self.get_object_query_mapping(queries) 

23 object_mutation_mapping = self.get_object_mutation_mapping(mutations) 

24 

25 # Enrich each object with its mapped queries 

26 for object_name in objects.keys(): 

27 if object_name in object_query_mapping: 

28 objects[object_name]["associatedQueries"] = object_query_mapping[object_name] 

29 else: 

30 objects[object_name]["associatedQueries"] = [] 

31 

32 if object_name in object_mutation_mapping: 

33 objects[object_name]["associatedMutatations"] = object_mutation_mapping[object_name] 

34 else: 

35 objects[object_name]["associatedMutatations"] = [] 

36 return objects 

37 

38 def get_object_query_mapping(self, queries: dict) -> dict: 

39 """Grab all objects -> List[query]. Must have kind of 'OBJECT' 

40 

41 Args: 

42 mutations (dict): The queries 

43 

44 Returns: 

45 dict: A mapping of object_name -> List of queries associated to the object 

46 """ 

47 object_query_mapping = {} 

48 for query_name, query_body in queries.items(): 

49 object_name = self.get_output_object(query_body["output"]) 

50 if object_name == "": 

51 continue 

52 elif object_name in object_query_mapping: 

53 object_query_mapping[object_name].append(query_name) 

54 else: 

55 object_query_mapping[object_name] = [query_name] 

56 return object_query_mapping 

57 

58 def get_object_mutation_mapping(self, mutations: dict) -> dict: 

59 """Grab all objects -> List[mutation]. Must have kind of 'OBJECT' 

60 

61 Args: 

62 mutations (dict): The mutations 

63 

64 Returns: 

65 dict: A mapping of object_name -> List of mutations associated to the object 

66 """ 

67 # Grab all objects -> mutation list 

68 # if object name is empty, just skip to next object like above 

69 object_mutation_mapping = {} 

70 for mutation_name, mutation_body in mutations.items(): 

71 object_name = self.get_output_object(mutation_body["output"]) 

72 if object_name == "": 

73 continue 

74 elif object_name in object_mutation_mapping: 

75 object_mutation_mapping[object_name].append(mutation_name) 

76 else: 

77 object_mutation_mapping[object_name] = [mutation_name] 

78 return object_mutation_mapping 

79 

80 def get_output_object(self, outputType: dict) -> str: 

81 """Gets the object as a string from the method's output 

82 

83 Args: 

84 outputType (dict): The 'output' key of the method 

85 

86 Returns: 

87 str: A string of the object, or empty if it's a simple scalar that doesn't map to any object 

88 """ 

89 if outputType["kind"] == "OBJECT": 

90 return outputType["name"] 

91 elif outputType["kind"] == "NON_NULL" or outputType["kind"] == "LIST": 

92 return self.get_output_object(outputType["ofType"]) 

93 else: 

94 return ""