@@ -12,123 +12,30 @@ def initialize(target, type, selections, execution_context)
1212 end
1313
1414 def result
15- flatten_and_merge_selections ( selections )
16- . values
17- . reduce ( { } ) { |result , ast_node |
18- result . merge ( resolve_field ( ast_node ) )
19- }
15+ flattened_selections = GraphQL ::Execution ::SelectionOnType . flatten ( execution_context , target , type , selections )
16+ flattened_selections . reduce ( { } ) do |result , ast_node |
17+ field_result = if GraphQL ::Execution ::DirectiveChecks . skip? ( ast_node , execution_context . query )
18+ { }
19+ else
20+ execution_context . strategy . field_resolution . new (
21+ ast_node ,
22+ type ,
23+ target ,
24+ execution_context
25+ ) . result
26+ end
27+
28+ result . merge ( field_result )
29+ end
2030 rescue GraphQL ::InvalidNullError => err
2131 err . parent_error? || execution_context . add_error ( err )
2232 nil
2333 end
2434
2535 private
2636
27- def flatten_selection ( ast_node )
28- strategy_method = STRATEGIES [ ast_node . class ]
29- send ( strategy_method , ast_node )
30- end
31-
32- STRATEGIES = {
33- GraphQL ::Language ::Nodes ::Field => :flatten_field ,
34- GraphQL ::Language ::Nodes ::InlineFragment => :flatten_inline_fragment ,
35- GraphQL ::Language ::Nodes ::FragmentSpread => :flatten_fragment_spread ,
36- }
37-
38- def flatten_field ( ast_node )
39- result_name = ast_node . alias || ast_node . name
40- { result_name => ast_node }
41- end
42-
43- def flatten_inline_fragment ( ast_node )
44- if GraphQL ::Execution ::DirectiveChecks . skip? ( ast_node , execution_context . query )
45- { }
46- else
47- flatten_fragment ( ast_node )
48- end
49- end
50-
51- def flatten_fragment_spread ( ast_node )
52- if GraphQL ::Execution ::DirectiveChecks . skip? ( ast_node , execution_context . query )
53- { }
54- else
55- ast_fragment_defn = execution_context . get_fragment ( ast_node . name )
56- flatten_fragment ( ast_fragment_defn )
57- end
58- end
59-
60- def flatten_fragment ( ast_fragment )
61- if fragment_type_can_apply? ( ast_fragment )
62- flatten_and_merge_selections ( ast_fragment . selections )
63- else
64- { }
65- end
66- end
67-
68- def fragment_type_can_apply? ( ast_fragment )
69- if ast_fragment . type . nil?
70- true
71- else
72- child_type = execution_context . get_type ( ast_fragment . type )
73- resolved_type = GraphQL ::Query ::TypeResolver . new ( target , child_type , type , execution_context . query . context ) . type
74- !resolved_type . nil?
75- end
76- end
77-
78- def merge_fields ( field1 , field2 )
79- field_type = execution_context . get_field ( type , field2 . name ) . type . unwrap
80-
81- if field_type . kind . fields?
82- # create a new ast field node merging selections from each field.
83- # Because of static validation, we can assume that name, alias,
84- # arguments, and directives are exactly the same for fields 1 and 2.
85- GraphQL ::Language ::Nodes ::Field . new (
86- name : field2 . name ,
87- alias : field2 . alias ,
88- arguments : field2 . arguments ,
89- directives : field2 . directives ,
90- selections : field1 . selections + field2 . selections
91- )
92- else
93- field2
94- end
95- end
96-
9737 def resolve_field ( ast_node )
98- if GraphQL ::Execution ::DirectiveChecks . skip? ( ast_node , execution_context . query )
99- { }
100- else
101- execution_context . strategy . field_resolution . new (
102- ast_node ,
103- type ,
104- target ,
105- execution_context
106- ) . result
107- end
108- end
109-
110- def merge_into_result ( memo , selection )
111- name = if selection . respond_to? ( :alias )
112- selection . alias || selection . name
113- else
114- selection . name
115- end
116-
117- memo [ name ] = if memo . key? ( name )
118- merge_fields ( memo [ name ] , selection )
119- else
120- selection
121- end
122- end
12338
124- def flatten_and_merge_selections ( selections )
125- selections . reduce ( { } ) do |result , ast_node |
126- flattened_selections = flatten_selection ( ast_node )
127- flattened_selections . each do |name , selection |
128- merge_into_result ( result , selection )
129- end
130- result
131- end
13239 end
13340 end
13441 end
0 commit comments