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