forked from rmosolgo/graphql-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalize.rb
More file actions
230 lines (211 loc) · 8.22 KB
/
Copy pathfinalize.rb
File metadata and controls
230 lines (211 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# frozen_string_literal: true
module GraphQL
module Execution
class Finalize
def initialize(query, data, runner)
@query = query
@data = data
@static_type_at = runner.static_type_at
@runner = runner
@current_exec_path = query.path.dup
@current_result_path = query.path.dup
@finalizers = runner.finalizers ? runner.finalizers[query] : {}.compare_by_identity
@finalizers_count = 0
@finalizers.each do |key, values|
values.each do |key2, values2|
case values2
when Array
@finalizers_count += values2.size
else
@finalizers_count += 1
end
end
end
query.context.errors.each do |err|
err_path = err.path - @current_exec_path
key = err_path.pop
targets = [data]
while (part = err_path.shift)
targets.map! { |t| t[part] }
targets.flatten!
end
targets.each_with_index do |target, idx|
if target.is_a?(Hash)
if target[key].equal?(err)
tf = @finalizers[target] ||= {}.compare_by_identity
tf[key] = err
@finalizers_count += 1
elsif (arr = target[key]).is_a?(Array)
arr.each_with_index do |el, idx|
if el.equal?(err)
tf = @finalizers[arr] ||= {}.compare_by_identity
tf[idx] = err
@finalizers_count += 1
end
end
end
end
end
end
end
def run
if (selected_operation = @query.selected_operation) && @data
if @data.is_a?(Hash)
check_object_result(@data, @query.root_type, selected_operation.selections)
elsif @data.is_a?(Array)
check_list_result(@data, @query.root_type, selected_operation.selections)
elsif @data.is_a?(Finalizer)
dummy_data = {}
dummy_key = "__dummy"
@data.path = @query.path
@data.finalize_graphql_result(@query, dummy_data, dummy_key)
dummy_data[dummy_key]
else
raise ArgumentError, "Unexpected @data: #{@data.inspect}"
end
else
@data
end
end
private
def run_finalizers(result_path, finalizer_or_finalizers, result_data, result_key)
if finalizer_or_finalizers.is_a?(Array)
finalizer_or_finalizers.each { |f|
f.path = result_path
f.finalize_graphql_result(@query, result_data, result_key)
}
@finalizers_count -= finalizer_or_finalizers.size
else
f = finalizer_or_finalizers
f.path = result_path
f.finalize_graphql_result(@query, result_data, result_key)
@finalizers_count -= 1
end
end
def finalizers(result_value, key)
finalizers_for_value = @finalizers[result_value]
finalizers_for_value && finalizers_for_value[key]
end
def check_object_result(result_h, parent_type, ast_selections)
if (f = finalizers(result_h, nil))
run_finalizers(@current_result_path.dup, f, result_h, nil)
return result_h if @finalizers_count == 0
end
if parent_type.kind.abstract?
parent_type = @runner.runtime_type_at[result_h]
end
ast_selections.each do |ast_selection|
case ast_selection
when Language::Nodes::Field
key = ast_selection.alias || ast_selection.name
if (f = finalizers(result_h, key))
result_value = result_h[key]
run_finalizers(@current_result_path.dup << key, f, result_h, key)
new_result_value = result_h.key?(key) ? result_h[key] : :unassigned
end
next if !(f || result_h.key?(key))
begin
@current_exec_path << key
@current_result_path << key
field_defn = @query.context.types.field(parent_type, ast_selection.name) || raise("Invariant: No field found for #{parent_type.to_type_signature}.#{ast_selection.name}")
result_type = field_defn.type
if (result_type_non_null = result_type.non_null?)
result_type = result_type.of_type
end
if !f
result_value = result_h[key]
new_result_value = if result_type.list? && result_value
check_list_result(result_value, result_type.of_type, ast_selection.selections)
elsif !result_type.kind.leaf? && result_value
check_object_result(result_value, result_type, ast_selection.selections)
else
result_value
end
end
if new_result_value.nil? && result_type_non_null
return nil
elsif :unassigned.equal?(new_result_value)
# Do nothing
break if @finalizers_count == 0
elsif !new_result_value.equal?(result_value)
result_h[key] = new_result_value
break if @finalizers_count == 0
end
ensure
@current_exec_path.pop
@current_result_path.pop
end
when Language::Nodes::InlineFragment
static_type_at_result = @static_type_at[result_h]
if static_type_at_result && (
(t = ast_selection.type).nil? ||
@runner.type_condition_applies?(@query.context, static_type_at_result, t.name)
)
result_h = check_object_result(result_h, parent_type, ast_selection.selections)
return nil if result_h.nil?
end
when Language::Nodes::FragmentSpread
fragment_defn = @query.document.definitions.find { |defn| defn.is_a?(Language::Nodes::FragmentDefinition) && defn.name == ast_selection.name }
static_type_at_result = @static_type_at[result_h]
if static_type_at_result && @runner.type_condition_applies?(@query.context, static_type_at_result, fragment_defn.type.name)
result_h = check_object_result(result_h, parent_type, fragment_defn.selections)
return nil if result_h.nil?
end
end
end
result_h
end
def check_list_result(result_arr, inner_type, ast_selections)
inner_type_non_null = false
if inner_type.non_null?
inner_type_non_null = true
inner_type = inner_type.of_type
end
new_invalid_null = false
if (f = finalizers(result_arr, nil))
run_finalizers(@current_result_path.dup, f, result_arr, nil)
return result_arr if @finalizers_count == 0
end
effective_idx = -1
result_arr.each_with_index do |result_item, before_idx|
effective_idx += 1
@current_result_path << before_idx
new_result = if (f = finalizers(result_arr, before_idx))
before_size = result_arr.size
run_finalizers(@current_result_path.dup, f, result_arr, effective_idx)
after_size = result_arr.size
if after_size < before_size
effective_idx -= 1
:unassigned
else
result_arr[effective_idx]
end
elsif inner_type.list? && result_item
check_list_result(result_item, inner_type.of_type, ast_selections)
elsif !inner_type.kind.leaf? && result_item
check_object_result(result_item, inner_type, ast_selections)
else
result_item
end
if new_result.nil? && inner_type_non_null
new_invalid_null = true
result_arr[effective_idx] = nil
break if @finalizers_count == 0
elsif :unassigned.equal?(new_result)
break if @finalizers_count == 0
elsif !new_result.equal?(result_item)
result_arr[effective_idx] = new_result
break if @finalizers_count == 0
end
ensure
@current_result_path.pop
end
if new_invalid_null
nil
else
result_arr
end
end
end
end
end