-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplan.rb
More file actions
144 lines (129 loc) · 3.44 KB
/
plan.rb
File metadata and controls
144 lines (129 loc) · 3.44 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
# frozen_string_literal: true
module GraphQL
module Stitching
# Immutable structures representing a query plan.
# May serialize to/from JSON.
class Plan
class Op
attr_reader :step
attr_reader :after
attr_reader :location
attr_reader :operation_type
attr_reader :selections
attr_reader :variables
attr_reader :path
attr_reader :if_type
attr_reader :resolver
def initialize(
step:,
after:,
location:,
operation_type:,
selections:,
variables: nil,
path: nil,
if_type: nil,
resolver: nil
)
@step = step
@after = after
@location = location
@operation_type = operation_type
@selections = selections
@variables = variables
@path = path
@if_type = if_type
@resolver = resolver
end
def as_json
{
step: step,
after: after,
location: location,
operation_type: operation_type,
selections: selections,
variables: variables,
path: path,
if_type: if_type,
resolver: resolver
}.tap(&:compact!)
end
def ==(other)
step == other.step &&
after == other.after &&
location == other.location &&
operation_type == other.operation_type &&
selections == other.selections &&
variables == other.variables &&
path == other.path &&
if_type == other.if_type &&
resolver == other.resolver
end
end
class Error
MESSAGE_BY_CODE = {
"unauthorized" => "Unauthorized access",
}.freeze
attr_reader :code, :path
def initialize(code:, path:)
@code = code
@path = path
end
def as_json
{
code: code,
path: path,
}
end
def to_h
{
"message" => MESSAGE_BY_CODE[@code],
"path" => @path,
"extensions" => { "code" => @code },
}
end
end
class << self
def from_json(json)
ops = json["ops"].map do |op|
Op.new(
step: op["step"],
after: op["after"],
location: op["location"],
operation_type: op["operation_type"],
selections: op["selections"],
variables: op["variables"],
path: op["path"],
if_type: op["if_type"],
resolver: op["resolver"],
)
end
errors = json["errors"]&.map do |err|
Error.new(
code: err["code"],
path: err["path"],
)
end
new(
ops: ops,
claims: json["claims"] || EMPTY_ARRAY,
errors: errors || EMPTY_ARRAY,
)
end
end
attr_reader :ops, :claims, :errors
def initialize(ops: EMPTY_ARRAY, claims: nil, errors: nil)
@ops = ops
@claims = claims || EMPTY_ARRAY
@errors = errors || EMPTY_ARRAY
end
def as_json
{
ops: @ops.map(&:as_json),
claims: @claims,
errors: @errors.map(&:as_json),
}.tap(&:compact!)
end
end
end
end