2121from ..ast_manipulation import get_only_query_definition
2222from ..exceptions import GraphQLValidationError
2323from ..schema import FilterDirective , OutputDirective
24- from .split_query import AstType , SubQueryNode
24+ from .. schema_transformation . split_query import AstType , SubQueryNode
2525
2626
2727@dataclass
@@ -66,47 +66,6 @@ class QueryPlanDescriptor:
6666 output_join_descriptors : List [OutputJoinDescriptor ]
6767
6868
69- def make_query_plan (
70- root_sub_query_node : SubQueryNode , intermediate_output_names : FrozenSet [str ]
71- ) -> QueryPlanDescriptor :
72- """Return a QueryPlanDescriptor, whose query ASTs have @filters added.
73-
74- For each parent of parent and child SubQueryNodes, a new @filter directive will be added
75- in the child AST. It will be added on the field whose @output directive has the out_name
76- equal to the child's out name as specified in the QueryConnection. The newly added @filter
77- will be a 'in_collection' type filter, and the name of the local variable is guaranteed to
78- be the same as the out_name of the @output on the parent.
79-
80- ASTs contained in the input node and its children nodes will not be modified.
81-
82- Args:
83- root_sub_query_node: representing the base of a query split into pieces
84- that we want to turn into a query plan.
85- intermediate_output_names: names of outputs to be removed at the end.
86-
87- Returns:
88- QueryPlanDescriptor containing a tree of SubQueryPlans that wrap around each individual
89- query AST, the set of intermediate output names that are to be removed at the end, and
90- information on which outputs are to be connect to which in what manner.
91- """
92- output_join_descriptors : List [OutputJoinDescriptor ] = []
93-
94- root_sub_query_plan = SubQueryPlan (
95- query_ast = root_sub_query_node .query_ast ,
96- schema_id = root_sub_query_node .schema_id ,
97- parent_query_plan = None ,
98- child_query_plans = [],
99- )
100-
101- _make_query_plan_recursive (root_sub_query_node , root_sub_query_plan , output_join_descriptors )
102-
103- return QueryPlanDescriptor (
104- root_sub_query_plan = root_sub_query_plan ,
105- intermediate_output_names = intermediate_output_names ,
106- output_join_descriptors = output_join_descriptors ,
107- )
108-
109-
11069def _make_query_plan_recursive (
11170 sub_query_node : SubQueryNode ,
11271 sub_query_plan : SubQueryPlan ,
@@ -291,6 +250,66 @@ def _get_in_collection_filter_directive(input_filter_name: str) -> DirectiveNode
291250 )
292251
293252
253+ def _get_plan_and_depth_in_dfs_order (query_plan : SubQueryPlan ) -> List [Tuple [SubQueryPlan , int ]]:
254+ """Return a list of topologically sorted (query plan, depth) tuples."""
255+
256+ def _get_plan_and_depth_in_dfs_order_helper (query_plan , depth ):
257+ plan_and_depth_in_dfs_order = [(query_plan , depth )]
258+ for child_query_plan in query_plan .child_query_plans :
259+ plan_and_depth_in_dfs_order .extend (
260+ _get_plan_and_depth_in_dfs_order_helper (child_query_plan , depth + 1 )
261+ )
262+ return plan_and_depth_in_dfs_order
263+
264+ return _get_plan_and_depth_in_dfs_order_helper (query_plan , 0 )
265+
266+
267+ ######
268+ # Public API
269+ ######
270+
271+
272+ def make_query_plan (
273+ root_sub_query_node : SubQueryNode , intermediate_output_names : FrozenSet [str ]
274+ ) -> QueryPlanDescriptor :
275+ """Return a QueryPlanDescriptor, whose query ASTs have @filters added.
276+
277+ For each parent of parent and child SubQueryNodes, a new @filter directive will be added
278+ in the child AST. It will be added on the field whose @output directive has the out_name
279+ equal to the child's out name as specified in the QueryConnection. The newly added @filter
280+ will be a 'in_collection' type filter, and the name of the local variable is guaranteed to
281+ be the same as the out_name of the @output on the parent.
282+
283+ ASTs contained in the input node and its children nodes will not be modified.
284+
285+ Args:
286+ root_sub_query_node: representing the base of a query split into pieces
287+ that we want to turn into a query plan.
288+ intermediate_output_names: names of outputs to be removed at the end.
289+
290+ Returns:
291+ QueryPlanDescriptor containing a tree of SubQueryPlans that wrap around each individual
292+ query AST, the set of intermediate output names that are to be removed at the end, and
293+ information on which outputs are to be connect to which in what manner.
294+ """
295+ output_join_descriptors : List [OutputJoinDescriptor ] = []
296+
297+ root_sub_query_plan = SubQueryPlan (
298+ query_ast = root_sub_query_node .query_ast ,
299+ schema_id = root_sub_query_node .schema_id ,
300+ parent_query_plan = None ,
301+ child_query_plans = [],
302+ )
303+
304+ _make_query_plan_recursive (root_sub_query_node , root_sub_query_plan , output_join_descriptors )
305+
306+ return QueryPlanDescriptor (
307+ root_sub_query_plan = root_sub_query_plan ,
308+ intermediate_output_names = intermediate_output_names ,
309+ output_join_descriptors = output_join_descriptors ,
310+ )
311+
312+
294313def print_query_plan (query_plan_descriptor : QueryPlanDescriptor , indentation_depth : int = 4 ) -> str :
295314 """Return a string describing query plan."""
296315 query_plan_strings = ["" ]
@@ -311,17 +330,3 @@ def print_query_plan(query_plan_descriptor: QueryPlanDescriptor, indentation_dep
311330 query_plan_strings .append (str (query_plan_descriptor .intermediate_output_names ) + "\n " )
312331
313332 return "" .join (query_plan_strings )
314-
315-
316- def _get_plan_and_depth_in_dfs_order (query_plan : SubQueryPlan ) -> List [Tuple [SubQueryPlan , int ]]:
317- """Return a list of topologically sorted (query plan, depth) tuples."""
318-
319- def _get_plan_and_depth_in_dfs_order_helper (query_plan , depth ):
320- plan_and_depth_in_dfs_order = [(query_plan , depth )]
321- for child_query_plan in query_plan .child_query_plans :
322- plan_and_depth_in_dfs_order .extend (
323- _get_plan_and_depth_in_dfs_order_helper (child_query_plan , depth + 1 )
324- )
325- return plan_and_depth_in_dfs_order
326-
327- return _get_plan_and_depth_in_dfs_order_helper (query_plan , 0 )
0 commit comments