Class: GraphQL::Execution::Runner
- Inherits:
-
Object
- Object
- GraphQL::Execution::Runner
- Defined in:
- lib/graphql/execution/runner.rb
Instance Attribute Summary collapse
-
#authorizes ⇒ Object
readonly
Returns the value of attribute authorizes.
-
#dataloader ⇒ Object
readonly
Returns the value of attribute dataloader.
-
#finalizer_keys ⇒ Object
readonly
Returns the value of attribute finalizer_keys.
-
#finalizers ⇒ Object
readonly
Returns the value of attribute finalizers.
-
#input_values ⇒ Object
readonly
Returns the value of attribute input_values.
-
#resolves_lazies ⇒ Object
readonly
Returns the value of attribute resolves_lazies.
-
#runtime_directives ⇒ Object
readonly
Returns the value of attribute runtime_directives.
-
#runtime_type_at ⇒ Object
readonly
Returns the value of attribute runtime_type_at.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#static_type_at ⇒ Object
readonly
Returns the value of attribute static_type_at.
-
#uses_runtime_directives ⇒ Object
readonly
Returns the value of attribute uses_runtime_directives.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
- #add_finalizer(query, result_value, key, finalizer) ⇒ void
- #add_step(step) ⇒ Object
- #authorizes?(graphql_definition, query_context) ⇒ Boolean
- #execute ⇒ Object
- #gather_selections(type_defn, ast_selections, selections_step, query, all_selections, prototype_result, into:) ⇒ Object
-
#initialize(multiplex) ⇒ Runner
constructor
A new instance of Runner.
- #lazy?(object) ⇒ Boolean
- #type_condition_applies?(context, concrete_type, type_name) ⇒ Boolean
Constructor Details
#initialize(multiplex) ⇒ Runner
Returns a new instance of Runner.
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 |
# File 'lib/graphql/execution/runner.rb', line 5 def initialize(multiplex) @multiplex = multiplex @schema = multiplex.schema @runtime_type_at = {}.compare_by_identity @static_type_at = {}.compare_by_identity @finalizers = nil @selected_operation = nil @dataloader = multiplex.context[:dataloader] ||= @schema.dataloader_class.new @resolves_lazies = @schema.resolves_lazies? @input_values = Hash.new do |h, query| h[query] = InputValues.new(query, self) end.compare_by_identity @runtime_directives = nil @schema.directives.each do |name, dir_class| if dir_class.runtime? && name != "include" && name != "skip" @runtime_directives ||= {} @runtime_directives[dir_class.graphql_name] = dir_class end end if @runtime_directives.nil? @uses_runtime_directives = false @runtime_directives = EmptyObjects::EMPTY_HASH else @uses_runtime_directives = true end @lazy_cache = resolves_lazies ? {}.compare_by_identity : nil @authorizes_cache = Hash.new do |h, query_context| h[query_context] = {}.compare_by_identity end.compare_by_identity end |
Instance Attribute Details
#authorizes ⇒ Object (readonly)
Returns the value of attribute authorizes.
55 56 57 |
# File 'lib/graphql/execution/runner.rb', line 55 def @authorizes end |
#dataloader ⇒ Object (readonly)
Returns the value of attribute dataloader.
55 56 57 |
# File 'lib/graphql/execution/runner.rb', line 55 def dataloader @dataloader end |
#finalizer_keys ⇒ Object (readonly)
Returns the value of attribute finalizer_keys.
39 40 41 |
# File 'lib/graphql/execution/runner.rb', line 39 def finalizer_keys @finalizer_keys end |
#finalizers ⇒ Object (readonly)
Returns the value of attribute finalizers.
55 56 57 |
# File 'lib/graphql/execution/runner.rb', line 55 def finalizers @finalizers end |
#input_values ⇒ Object (readonly)
Returns the value of attribute input_values.
55 56 57 |
# File 'lib/graphql/execution/runner.rb', line 55 def input_values @input_values end |
#resolves_lazies ⇒ Object (readonly)
Returns the value of attribute resolves_lazies.
55 56 57 |
# File 'lib/graphql/execution/runner.rb', line 55 def resolves_lazies @resolves_lazies end |
#runtime_directives ⇒ Object (readonly)
Returns the value of attribute runtime_directives.
39 40 41 |
# File 'lib/graphql/execution/runner.rb', line 39 def runtime_directives @runtime_directives end |
#runtime_type_at ⇒ Object (readonly)
Returns the value of attribute runtime_type_at.
55 56 57 |
# File 'lib/graphql/execution/runner.rb', line 55 def runtime_type_at @runtime_type_at end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
55 56 57 |
# File 'lib/graphql/execution/runner.rb', line 55 def schema @schema end |
#static_type_at ⇒ Object (readonly)
Returns the value of attribute static_type_at.
55 56 57 |
# File 'lib/graphql/execution/runner.rb', line 55 def static_type_at @static_type_at end |
#uses_runtime_directives ⇒ Object (readonly)
Returns the value of attribute uses_runtime_directives.
39 40 41 |
# File 'lib/graphql/execution/runner.rb', line 39 def uses_runtime_directives @uses_runtime_directives end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
55 56 57 |
# File 'lib/graphql/execution/runner.rb', line 55 def variables @variables end |
Instance Method Details
#add_finalizer(query, result_value, key, finalizer) ⇒ void
This method returns an undefined value.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/graphql/execution/runner.rb', line 58 def add_finalizer(query, result_value, key, finalizer) @finalizers ||= {}.compare_by_identity f_for_query = @finalizers[query] ||= {}.compare_by_identity f_for_result = f_for_query[result_value] ||= {}.compare_by_identity if (f = f_for_result[key]) if f.is_a?(Array) f << finalizer else f_for_result[key] = [f, finalizer] end else f_for_result[key] = finalizer end nil end |
#add_step(step) ⇒ Object
51 52 53 |
# File 'lib/graphql/execution/runner.rb', line 51 def add_step(step) @dataloader.append_job(step) end |
#authorizes?(graphql_definition, query_context) ⇒ Boolean
41 42 43 44 45 46 47 48 49 |
# File 'lib/graphql/execution/runner.rb', line 41 def (graphql_definition, query_context) auth_cache = @authorizes_cache[query_context] case (auth_res = auth_cache[graphql_definition]) when nil auth_cache[graphql_definition] = graphql_definition.(query_context) else auth_res end end |
#execute ⇒ Object
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 |
# File 'lib/graphql/execution/runner.rb', line 74 def execute Fiber[:__graphql_current_multiplex] = @multiplex isolated_steps = [[]] trace = @multiplex.current_trace queries = @multiplex.queries multiplex_analyzers = @schema.multiplex_analyzers if @multiplex.max_complexity multiplex_analyzers += [GraphQL::Analysis::MaxQueryComplexity] end trace.execute_multiplex(multiplex: @multiplex) do trace.begin_analyze_multiplex(@multiplex, multiplex_analyzers) @schema.analysis_engine.analyze_multiplex(@multiplex, multiplex_analyzers) trace.end_analyze_multiplex(@multiplex, multiplex_analyzers) results = [] queries.each do |query| if query.validate && !query.valid? results << { "errors" => query.static_errors.map(&:to_h) } next end root_type = query.root_type if root_type.non_null? root_type = root_type.of_type end root_value = query.root_value if resolves_lazies root_value = schema.sync_lazy(root_value) end trace.execute_query(query: query) do begin_execute(isolated_steps, results, query, root_type, root_value) end rescue GraphQL::RuntimeError => err err.ast_node = query.selected_operation err.path = query.path query.context.add_error(err) end trace.execute_query_lazy(query: @multiplex.queries.size == 1 ? @multiplex.queries.first : nil, multiplex: @multiplex) do while (next_isolated_steps = isolated_steps.shift) next_isolated_steps.each do |step| add_step(step) end @dataloader.run end end queries.each_with_index.map do |query, idx| result = results[idx] fin_result = if (!@finalizers&.key?(query) && query.context.errors.empty?) || !query.valid? result else if result data = result["data"] data = Finalize.new(query, data, self).run end errors = [] query.context.errors.each do |err| if err.respond_to?(:to_h) errors << err.to_h end end res_h = {} if !errors.empty? res_h["errors"] = errors end res_h["data"] = data res_h end query.result_values = fin_result if query.context.namespace?(:__query_result_extensions__) query.result_values["extensions"] = query.context.namespace(:__query_result_extensions__) end query.result end end ensure Fiber[:__graphql_current_multiplex] = nil end |
#gather_selections(type_defn, ast_selections, selections_step, query, all_selections, prototype_result, into:) ⇒ Object
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 |
# File 'lib/graphql/execution/runner.rb', line 162 def gather_selections(type_defn, ast_selections, selections_step, query, all_selections, prototype_result, into:) ast_selections.each do |ast_selection| next if !directives_include?(query, ast_selection) case ast_selection when GraphQL::Language::Nodes::Field key = ast_selection.alias || ast_selection.name step = into[key] ||= begin prototype_result[key] = nil FieldResolveStep.new( selections_step: selections_step, key: key, parent_type: type_defn, runner: self, ) end step.append_selection(ast_selection) when GraphQL::Language::Nodes::InlineFragment type_condition = ast_selection.type&.name if type_condition.nil? || type_condition_applies?(query.context, type_defn, type_condition) if uses_runtime_directives && !ast_selection.directives.empty? all_selections << (into = { __node: ast_selection }) all_selections << (prototype_result = {}) end gather_selections(type_defn, ast_selection.selections, selections_step, query, all_selections, prototype_result, into: into) end when GraphQL::Language::Nodes::FragmentSpread fragment_definition = query.fragments[ast_selection.name] type_condition = fragment_definition.type.name if type_condition_applies?(query.context, type_defn, type_condition) if uses_runtime_directives && !ast_selection.directives.empty? all_selections << (into = { __node: ast_selection }) all_selections << (prototype_result = {}) end gather_selections(type_defn, fragment_definition.selections, selections_step, query, all_selections, prototype_result, into: into) end else raise ArgumentError, "Unsupported graphql selection node: #{ast_selection.class} (#{ast_selection.inspect})" end end end |
#lazy?(object) ⇒ Boolean
205 206 207 208 209 210 211 212 |
# File 'lib/graphql/execution/runner.rb', line 205 def lazy?(object) obj_class = object.class is_lazy = @lazy_cache[obj_class] if is_lazy.nil? is_lazy = @lazy_cache[obj_class] = @schema.lazy?(object) end is_lazy end |
#type_condition_applies?(context, concrete_type, type_name) ⇒ Boolean
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/graphql/execution/runner.rb', line 214 def type_condition_applies?(context, concrete_type, type_name) if type_name == concrete_type.graphql_name true else abs_t = @schema.get_type(type_name, context) p_types = @schema.possible_types(abs_t, context) c_p_types = @schema.possible_types(concrete_type, context) p_types.any? { |t| c_p_types.include?(t) } end end |