@@ -2,18 +2,11 @@ use std::{fmt::Display, hash::Hash};
22
33use crate :: ast:: {
44 merge_path:: { Condition , Segment } ,
5- safe_merge:: { AliasesRecords , SafeSelectionSetMerger } ,
65 selection_set:: { FieldSelection , InlineFragmentSelection } ,
76} ;
87
98use super :: { merge_path:: MergePath , selection_item:: SelectionItem , selection_set:: SelectionSet } ;
109
11- #[ derive( Debug , Clone , thiserror:: Error ) ]
12- pub enum TypeAwareSelectionError {
13- #[ error( "Failed to locate path '{0}' in selection set '{1}'" ) ]
14- PathNotFound ( String , String ) ,
15- }
16-
1710#[ derive( Debug , Clone ) ]
1811pub struct TypeAwareSelection {
1912 pub type_name : String ,
@@ -75,62 +68,6 @@ impl TypeAwareSelection {
7568 pub fn add ( & mut self , to_add : & Self ) {
7669 merge_selection_set ( & mut self . selection_set , & to_add. selection_set , false ) ;
7770 }
78-
79- pub fn add_at_path (
80- & mut self ,
81- to_add : & Self ,
82- add_at_fetch_path : MergePath ,
83- as_first : bool ,
84- ) -> Result < ( ) , TypeAwareSelectionError > {
85- if let Some ( source) =
86- find_selection_set_by_path_mut ( & mut self . selection_set , & add_at_fetch_path)
87- {
88- merge_selection_set ( source, & to_add. selection_set , as_first) ;
89-
90- Ok ( ( ) )
91- } else {
92- Err ( TypeAwareSelectionError :: PathNotFound (
93- add_at_fetch_path. to_string ( ) ,
94- self . selection_set . to_string ( ) ,
95- ) )
96- }
97- }
98-
99- pub fn add_at_path_and_solve_conflicts (
100- & mut self ,
101- to_add : & Self ,
102- add_at_fetch_path : MergePath ,
103- ( self_used_for_requires, other_used_for_requires) : ( bool , bool ) ,
104- as_first : bool ,
105- ) -> Option < AliasesRecords > {
106- if let Some ( source) =
107- find_selection_set_by_path_mut ( & mut self . selection_set , & add_at_fetch_path)
108- {
109- let mut merger = SafeSelectionSetMerger :: default ( ) ;
110- let aliases_made = merger. merge_selection_set (
111- source,
112- & to_add. selection_set ,
113- ( self_used_for_requires, other_used_for_requires) ,
114- as_first,
115- ) ;
116-
117- if !aliases_made. is_empty ( ) {
118- return Some ( aliases_made) ;
119- }
120- }
121-
122- None
123- }
124-
125- pub fn has_typename_at_path ( & self , lookup_path : & MergePath ) -> bool {
126- find_selection_set_by_path (
127- & self . selection_set ,
128- // We pass None for the condition, because we are checking
129- // the presence of __typename at the given path, not type __typename with a condition.
130- & lookup_path. push ( Segment :: Field ( "__typename" . to_string ( ) , 0 , None ) ) ,
131- )
132- . is_some ( )
133- }
13471}
13572
13673fn selection_item_is_subset_of ( source : & SelectionItem , target : & SelectionItem ) -> bool {
@@ -154,15 +91,15 @@ fn selection_item_is_subset_of(source: &SelectionItem, target: &SelectionItem) -
15491 }
15592}
15693
157- fn selection_items_are_subset_of ( source : & [ SelectionItem ] , target : & [ SelectionItem ] ) -> bool {
94+ pub fn selection_items_are_subset_of ( source : & [ SelectionItem ] , target : & [ SelectionItem ] ) -> bool {
15895 target. iter ( ) . all ( |target_node| {
15996 source
16097 . iter ( )
16198 . any ( |source_node| selection_item_is_subset_of ( source_node, target_node) )
16299 } )
163100}
164101
165- fn merge_selection_set ( target : & mut SelectionSet , source : & SelectionSet , as_first : bool ) {
102+ pub fn merge_selection_set ( target : & mut SelectionSet , source : & SelectionSet , as_first : bool ) {
166103 if source. items . is_empty ( ) {
167104 return ;
168105 }
@@ -217,80 +154,6 @@ fn merge_selection_set(target: &mut SelectionSet, source: &SelectionSet, as_firs
217154 }
218155}
219156
220- pub fn find_selection_set_by_path < ' a > (
221- root_selection_set : & ' a SelectionSet ,
222- path : & MergePath ,
223- ) -> Option < & ' a SelectionSet > {
224- let mut current_selection_set = root_selection_set;
225-
226- for path_element in path. inner . iter ( ) {
227- match path_element {
228- Segment :: List => {
229- continue ;
230- }
231- Segment :: Cast ( type_name, condition) => {
232- let next_selection_set_option =
233- current_selection_set
234- . items
235- . iter ( )
236- . find_map ( |item| match item {
237- SelectionItem :: Field ( _) => None ,
238- SelectionItem :: InlineFragment ( f) => {
239- if f. type_condition . eq ( type_name)
240- && fragment_condition_equal ( condition, f)
241- {
242- Some ( & f. selections )
243- } else {
244- None
245- }
246- }
247- SelectionItem :: FragmentSpread ( _) => None ,
248- } ) ;
249-
250- match next_selection_set_option {
251- Some ( next_set) => {
252- current_selection_set = next_set;
253- }
254- None => {
255- return None ;
256- }
257- }
258- }
259- Segment :: Field ( field_name, args_hash, condition) => {
260- let next_selection_set_option =
261- current_selection_set
262- . items
263- . iter ( )
264- . find_map ( |item| match item {
265- SelectionItem :: Field ( field) => {
266- if & field. name == field_name
267- && field. arguments_hash ( ) == * args_hash
268- && field_condition_equal ( condition, field)
269- {
270- Some ( & field. selections )
271- } else {
272- None
273- }
274- }
275- SelectionItem :: InlineFragment ( ..) => None ,
276- SelectionItem :: FragmentSpread ( _) => None ,
277- } ) ;
278-
279- match next_selection_set_option {
280- Some ( next_set) => {
281- current_selection_set = next_set;
282- }
283- None => {
284- return None ;
285- }
286- }
287- }
288- }
289- }
290-
291- Some ( current_selection_set)
292- }
293-
294157pub fn find_selection_set_by_path_mut < ' a > (
295158 root_selection_set : & ' a mut SelectionSet ,
296159 path : & MergePath ,
0 commit comments