-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecution_field.rb
More file actions
79 lines (68 loc) · 1.72 KB
/
execution_field.rb
File metadata and controls
79 lines (68 loc) · 1.72 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
# frozen_string_literal: true
module GraphQL::Cardinal
class Executor
class ExecutionField
attr_reader :key, :node
attr_accessor :scope, :type, :result
def initialize(key, scope = nil)
@key = key.freeze
@scope = scope
@name = nil
@node = nil
@nodes = nil
@type = nil
@result = nil
@arguments = nil
@path = nil
end
def name
@name ||= @node.name.freeze
end
def path
@path ||= (@scope ? [*@scope.path, @key] : []).freeze
end
def add_node(n)
if !@node
@node = n
elsif !@nodes
@nodes = [@node, n]
else
@nodes << n
end
end
def nodes
@nodes ? @nodes : [@node]
end
def selections
if @nodes
@nodes.flat_map(&:selections)
else
@node.selections
end
end
def arguments(vars)
return EMPTY_OBJECT if @node.arguments.empty?
@arguments ||= @node.arguments.each_with_object({}) do |arg, args|
args[arg.name] = build_arguments(arg.value, vars)
end
end
private
def build_arguments(value, vars)
case value
when GraphQL::Language::Nodes::VariableIdentifier
vars[value.name] || vars[value.name.to_sym]
when GraphQL::Language::Nodes::NullValue
nil
when GraphQL::Language::Nodes::InputObject
value.arguments.each_with_object({}) do |arg, obj|
obj[arg.name] = build_arguments(arg.value, vars)
end
when Array
value.map { |item| build_arguments(item, vars) }
else
value
end
end
end
end
end