Skip to content

Commit 749b670

Browse files
committed
Dry Partial with Query
1 parent c37a670 commit 749b670

2 files changed

Lines changed: 36 additions & 58 deletions

File tree

lib/graphql/query.rb

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,40 @@ class Query
1717
autoload :VariableValidationError, "graphql/query/variable_validation_error"
1818
autoload :ValidationPipeline, "graphql/query/validation_pipeline"
1919

20+
# Code shared with {Partial}
21+
module Runnable
22+
def after_lazy(value, &block)
23+
if !defined?(@runtime_instance)
24+
@runtime_instance = context.namespace(:interpreter_runtime)[:runtime]
25+
end
26+
27+
if @runtime_instance
28+
@runtime_instance.minimal_after_lazy(value, &block)
29+
else
30+
@schema.after_lazy(value, &block)
31+
end
32+
end
33+
34+
# Node-level cache for calculating arguments. Used during execution and query analysis.
35+
# @param ast_node [GraphQL::Language::Nodes::AbstractNode]
36+
# @param definition [GraphQL::Schema::Field]
37+
# @param parent_object [GraphQL::Schema::Object]
38+
# @return [Hash{Symbol => Object}]
39+
def arguments_for(ast_node, definition, parent_object: nil)
40+
arguments_cache.fetch(ast_node, definition, parent_object)
41+
end
42+
43+
def arguments_cache
44+
@arguments_cache ||= Execution::Interpreter::ArgumentsCache.new(self)
45+
end
46+
47+
# @api private
48+
def handle_or_reraise(err)
49+
@query.schema.handle_or_reraise(context, err)
50+
end
51+
end
52+
53+
include Runnable
2054
class OperationNameMissingError < GraphQL::ExecutionError
2155
def initialize(name)
2256
msg = if name.nil?
@@ -291,19 +325,6 @@ def variables
291325
end
292326
end
293327

294-
# Node-level cache for calculating arguments. Used during execution and query analysis.
295-
# @param ast_node [GraphQL::Language::Nodes::AbstractNode]
296-
# @param definition [GraphQL::Schema::Field]
297-
# @param parent_object [GraphQL::Schema::Object]
298-
# @return [Hash{Symbol => Object}]
299-
def arguments_for(ast_node, definition, parent_object: nil)
300-
arguments_cache.fetch(ast_node, definition, parent_object)
301-
end
302-
303-
def arguments_cache
304-
@arguments_cache ||= Execution::Interpreter::ArgumentsCache.new(self)
305-
end
306-
307328
# A version of the given query string, with:
308329
# - Variables inlined to the query
309330
# - Strings replaced with `<REDACTED>`
@@ -413,23 +434,6 @@ def subscription?
413434
with_prepared_ast { @subscription }
414435
end
415436

416-
# @api private
417-
def handle_or_reraise(err)
418-
schema.handle_or_reraise(context, err)
419-
end
420-
421-
def after_lazy(value, &block)
422-
if !defined?(@runtime_instance)
423-
@runtime_instance = context.namespace(:interpreter_runtime)[:runtime]
424-
end
425-
426-
if @runtime_instance
427-
@runtime_instance.minimal_after_lazy(value, &block)
428-
else
429-
@schema.after_lazy(value, &block)
430-
end
431-
end
432-
433437
attr_reader :logger
434438

435439
private

lib/graphql/query/partial.rb

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class Query
44
# This class is _like_ a {GraphQL::Query}, except
55
# @see Query#run_partials
66
class Partial
7+
include Query::Runnable
8+
79
def initialize(path:, object:, query:, context: nil)
810
@path = path
911
@object = object
@@ -96,34 +98,6 @@ def types
9698
@query.types
9799
end
98100

99-
# TODO dry with query
100-
def after_lazy(value, &block)
101-
if !defined?(@runtime_instance)
102-
@runtime_instance = context.namespace(:interpreter_runtime)[:runtime]
103-
end
104-
105-
if @runtime_instance
106-
@runtime_instance.minimal_after_lazy(value, &block)
107-
else
108-
@schema.after_lazy(value, &block)
109-
end
110-
end
111-
112-
# TODO dry with query
113-
def arguments_for(ast_node, definition, parent_object: nil)
114-
arguments_cache.fetch(ast_node, definition, parent_object)
115-
end
116-
117-
# TODO dry with query
118-
def arguments_cache
119-
@arguments_cache ||= Execution::Interpreter::ArgumentsCache.new(self)
120-
end
121-
122-
# TODO dry
123-
def handle_or_reraise(err)
124-
@query.schema.handle_or_reraise(context, err)
125-
end
126-
127101
def resolve_type(...)
128102
@query.resolve_type(...)
129103
end

0 commit comments

Comments
 (0)