graphql-armory 0.1.0.pre11 → 0.1.0.pre13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -1
- data/README.md +4 -4
- data/docs/authorization.md +7 -7
- data/docs/rate-limiting.md +1 -1
- data/docs/versioning.md +540 -0
- data/lib/graphql/armory/authorization/adapters/pundit.rb +26 -1
- data/lib/graphql/armory/authorization/audit/findings.rb +6 -1
- data/lib/graphql/armory/authorization/audit/overrides.rb +8 -2
- data/lib/graphql/armory/authorization/audit/payload_reuse.rb +9 -8
- data/lib/graphql/armory/authorization/definitions.rb +8 -92
- data/lib/graphql/armory/authorization/integration/object.rb +2 -2
- data/lib/graphql/armory/authorization/integration.rb +29 -2
- data/lib/graphql/armory/authorization/rule.rb +3 -3
- data/lib/graphql/armory/authorization.rb +3 -1
- data/lib/graphql/armory/definitions.rb +111 -0
- data/lib/graphql/armory/location.rb +101 -0
- data/lib/graphql/armory/rate_limits/analysis_guard.rb +9 -2
- data/lib/graphql/armory/version.rb +1 -1
- data/lib/graphql/armory/versioning/audit/command.rb +43 -0
- data/lib/graphql/armory/versioning/audit/own_checks.rb +139 -0
- data/lib/graphql/armory/versioning/audit/profiles.rb +123 -0
- data/lib/graphql/armory/versioning/audit/subscription_triggers.rb +98 -0
- data/lib/graphql/armory/versioning/audit.rb +559 -0
- data/lib/graphql/armory/versioning/controls.rb +403 -0
- data/lib/graphql/armory/versioning/decision.rb +89 -0
- data/lib/graphql/armory/versioning/deprecation.rb +95 -0
- data/lib/graphql/armory/versioning/diff/command.rb +49 -0
- data/lib/graphql/armory/versioning/diff.rb +349 -0
- data/lib/graphql/armory/versioning/dump/command.rb +46 -0
- data/lib/graphql/armory/versioning/dump.rb +107 -0
- data/lib/graphql/armory/versioning/gate.rb +140 -0
- data/lib/graphql/armory/versioning/integration.rb +903 -0
- data/lib/graphql/armory/versioning/lifecycle.rb +61 -0
- data/lib/graphql/armory/versioning/profiles.rb +122 -0
- data/lib/graphql/armory/versioning/rspec.rb +38 -0
- data/lib/graphql/armory/versioning/versions.rb +111 -0
- data/lib/graphql/armory/versioning/view.rb +61 -0
- data/lib/graphql/armory/versioning.rb +132 -0
- data/lib/graphql/armory.rb +1 -0
- data/lib/tasks/graphql_armory.rake +20 -0
- metadata +24 -2
- data/lib/graphql/armory/authorization/location.rb +0 -79
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Versioning
|
|
6
|
+
# What changes between two served versions for a client built against the first, in three buckets:
|
|
7
|
+
#
|
|
8
|
+
# - breaking: a query that worked against `from` can fail against `to` (a type, field, argument, input field, enum
|
|
9
|
+
# value or union member removed; a required argument or input field added; a type changed; an interface no
|
|
10
|
+
# longer implemented; a type's kind changed).
|
|
11
|
+
# - dangerous: queries still validate, but a client may see something it does not expect (an enum value, union
|
|
12
|
+
# member or interface implementation added; a deprecation added; a default value changed; an output made non-null,
|
|
13
|
+
# which can break a fragment that merges it with a nullable field of the same name).
|
|
14
|
+
# - safe: the rest.
|
|
15
|
+
#
|
|
16
|
+
# result = GraphQL::Armory::Versioning::Diff.(AppSchema, from: "2024-06-01", to: "2025-01-01")
|
|
17
|
+
# result.breaking? # true when a client of 2024-06-01 can break on 2025-01-01
|
|
18
|
+
# puts result
|
|
19
|
+
#
|
|
20
|
+
# Each version is read through `Versioning.types`, as a client of that version sees it, with `armory_full_schema:
|
|
21
|
+
# true` so Authorization's `visible_when:` hides nothing. Types are compared by name, so twins across the two versions
|
|
22
|
+
# are compared member by member. Directives are not compared. It records no signal.
|
|
23
|
+
class Diff
|
|
24
|
+
BUCKETS = %i[breaking dangerous safe].freeze
|
|
25
|
+
|
|
26
|
+
# One change: its bucket, the path of what changed ("Invoice.tags.first") and what happened to it.
|
|
27
|
+
Change = Struct.new(:bucket, :path, :message) do
|
|
28
|
+
def to_s = "#{path}: #{message}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The changes from one version to another, breaking first, each bucket sorted by path.
|
|
32
|
+
class Result
|
|
33
|
+
attr_reader :from, :to, :changes
|
|
34
|
+
|
|
35
|
+
def initialize(from:, to:, changes:)
|
|
36
|
+
@from = from
|
|
37
|
+
@to = to
|
|
38
|
+
@changes = changes.sort_by { |change| [BUCKETS.index(change.bucket), change.path, change.message] }.freeze
|
|
39
|
+
freeze
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def breaking = changes.select { |change| change.bucket == :breaking }
|
|
43
|
+
|
|
44
|
+
def dangerous = changes.select { |change| change.bucket == :dangerous }
|
|
45
|
+
|
|
46
|
+
def safe = changes.select { |change| change.bucket == :safe }
|
|
47
|
+
|
|
48
|
+
def breaking? = changes.any? { |change| change.bucket == :breaking }
|
|
49
|
+
|
|
50
|
+
def to_s
|
|
51
|
+
lines = ["From #{from} to #{to}:"]
|
|
52
|
+
if changes.empty?
|
|
53
|
+
lines << "No changes."
|
|
54
|
+
else
|
|
55
|
+
BUCKETS.each do |bucket|
|
|
56
|
+
listed = changes.select { |change| change.bucket == bucket }
|
|
57
|
+
next if listed.empty?
|
|
58
|
+
|
|
59
|
+
lines.push("", "#{bucket.to_s.capitalize} (#{listed.size}):", *listed.map { |change| " #{change}" })
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
lines.join("\n") << "\n"
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Answers each version's `View` of a schema. `Reader.new` with nothing reads nothing: it refuses.
|
|
67
|
+
class Reader
|
|
68
|
+
def initialize(schema = nil, context: {})
|
|
69
|
+
@schema = schema
|
|
70
|
+
@context = context
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def view(label)
|
|
74
|
+
raise ConfigurationError, "Diff has no schema to read: use Diff.build(schema) or Diff.(schema, from:, to:)" unless @schema
|
|
75
|
+
|
|
76
|
+
versions = Integration.gate(@schema).versions
|
|
77
|
+
raise ConfigurationError, "#{label.inspect} is retired: Diff compares served versions" if versions.fetch(label).retired?
|
|
78
|
+
|
|
79
|
+
Versioning.types(@schema, label, context: @context.merge(armory_full_schema: true))
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Answers the views it is told, per label, and records the labels it was asked about. A label it was not told
|
|
83
|
+
# about is refused.
|
|
84
|
+
class Substitute
|
|
85
|
+
attr_reader :labels
|
|
86
|
+
|
|
87
|
+
def initialize
|
|
88
|
+
@views = {}
|
|
89
|
+
@labels = []
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def answer(label, view)
|
|
93
|
+
@views[label] = view
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def view(label)
|
|
97
|
+
@labels << label
|
|
98
|
+
@views.fetch(label) { raise ConfigurationError, "Diff::Reader::Substitute was not told about #{label.inspect}" }
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Answers the result it is told and records what it was asked: for the rake task's and the matcher's specs.
|
|
104
|
+
class Substitute
|
|
105
|
+
attr_reader :calls
|
|
106
|
+
|
|
107
|
+
def initialize(result)
|
|
108
|
+
@result = result
|
|
109
|
+
@calls = []
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def call(schema, from:, to:)
|
|
113
|
+
@calls << [schema, { from: from, to: to }]
|
|
114
|
+
@result
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Checks the schema is versioned and wires its reader. `context:` is merged into each version's (a user, for an
|
|
119
|
+
# app `visible?` that needs one).
|
|
120
|
+
def self.build(schema, context: {})
|
|
121
|
+
Integration.gate(schema)
|
|
122
|
+
new(Reader.new(schema, context: context))
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def self.call(schema, from:, to:, context: {}) = build(schema, context: context).(from: from, to: to)
|
|
126
|
+
|
|
127
|
+
# `new` is inert: with no schema, it refuses instead of answering "no changes".
|
|
128
|
+
def initialize(reader = Reader.new)
|
|
129
|
+
@reader = reader
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def call(from:, to:)
|
|
133
|
+
older = @reader.view(from)
|
|
134
|
+
newer = @reader.view(to)
|
|
135
|
+
changes = []
|
|
136
|
+
Walk.new(older, newer, changes).()
|
|
137
|
+
Result.new(from: from, to: to, changes: changes)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Compares two views, type by type. Internal to `Diff`.
|
|
141
|
+
class Walk
|
|
142
|
+
def initialize(old, new, changes)
|
|
143
|
+
@old = old
|
|
144
|
+
@new = new
|
|
145
|
+
@changes = changes
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def call
|
|
149
|
+
old_types = types(@old)
|
|
150
|
+
new_types = types(@new)
|
|
151
|
+
old_types.each do |name, old_type|
|
|
152
|
+
new_type = new_types[name]
|
|
153
|
+
next add(:breaking, name, "type removed") unless new_type
|
|
154
|
+
|
|
155
|
+
type(name, old_type, new_type)
|
|
156
|
+
end
|
|
157
|
+
(new_types.keys - old_types.keys).each { |name| add(:safe, name, "type added") }
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
private
|
|
161
|
+
|
|
162
|
+
def types(view)
|
|
163
|
+
view.all_types.reject { |type| Armory::Definitions.introspection?(type) }.to_h { |type| [type.graphql_name, type] }
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def add(bucket, path, message)
|
|
167
|
+
@changes << Change.new(bucket, path, message)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def type(name, old_type, new_type)
|
|
171
|
+
if old_type.kind != new_type.kind
|
|
172
|
+
return add(:breaking, name, "kind changed from #{old_type.kind.name} to #{new_type.kind.name}")
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
description(name, old_type, new_type)
|
|
176
|
+
case old_type.kind.name
|
|
177
|
+
when "OBJECT", "INTERFACE"
|
|
178
|
+
fields(name, old_type, new_type)
|
|
179
|
+
interfaces(name, old_type, new_type)
|
|
180
|
+
when "UNION" then members(name, old_type, new_type)
|
|
181
|
+
when "ENUM" then values(name, old_type, new_type)
|
|
182
|
+
when "INPUT_OBJECT" then inputs(name, old_type, new_type, input_field: true)
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def fields(type_name, old_type, new_type)
|
|
187
|
+
old_fields = by_name(@old.fields(old_type))
|
|
188
|
+
new_fields = by_name(@new.fields(new_type))
|
|
189
|
+
old_fields.each do |name, old_field|
|
|
190
|
+
path = "#{type_name}.#{name}"
|
|
191
|
+
new_field = new_fields[name]
|
|
192
|
+
next add(:breaking, path, "field removed") unless new_field
|
|
193
|
+
|
|
194
|
+
type_change(path, old_field.type, new_field.type, output: true)
|
|
195
|
+
deprecation(path, old_field, new_field)
|
|
196
|
+
description(path, old_field, new_field)
|
|
197
|
+
inputs(path, old_field, new_field, input_field: false)
|
|
198
|
+
end
|
|
199
|
+
(new_fields.keys - old_fields.keys).each { |name| add(:safe, "#{type_name}.#{name}", "field added") }
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# Arguments of a field, or input fields of an input object.
|
|
203
|
+
def inputs(owner_path, old_owner, new_owner, input_field:)
|
|
204
|
+
noun = input_field ? "input field" : "argument"
|
|
205
|
+
old_inputs = by_name(@old.arguments(old_owner))
|
|
206
|
+
new_inputs = by_name(@new.arguments(new_owner))
|
|
207
|
+
old_inputs.each do |name, old_input|
|
|
208
|
+
path = "#{owner_path}.#{name}"
|
|
209
|
+
new_input = new_inputs[name]
|
|
210
|
+
next add(:breaking, path, "#{noun} removed") unless new_input
|
|
211
|
+
|
|
212
|
+
input(path, old_input, new_input)
|
|
213
|
+
end
|
|
214
|
+
(new_inputs.keys - old_inputs.keys).each do |name|
|
|
215
|
+
required = required?(new_inputs[name])
|
|
216
|
+
add(required ? :breaking : :safe, "#{owner_path}.#{name}", required ? "required #{noun} added" : "#{noun} added")
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def input(path, old_input, new_input)
|
|
221
|
+
bucket = type_change(path, old_input.type, new_input.type, output: false)
|
|
222
|
+
if bucket != :breaking && !required?(old_input) && required?(new_input)
|
|
223
|
+
add(:breaking, path, "now required: its default value was removed")
|
|
224
|
+
elsif bucket != :breaking && default_changed?(old_input, new_input)
|
|
225
|
+
add(:dangerous, path, default_message(old_input, new_input))
|
|
226
|
+
end
|
|
227
|
+
deprecation(path, old_input, new_input)
|
|
228
|
+
description(path, old_input, new_input)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def interfaces(type_name, old_type, new_type)
|
|
232
|
+
old_names = @old.interfaces(old_type).map(&:graphql_name)
|
|
233
|
+
new_names = @new.interfaces(new_type).map(&:graphql_name)
|
|
234
|
+
(old_names - new_names).sort.each { |name| add(:breaking, type_name, "no longer implements #{name}") }
|
|
235
|
+
(new_names - old_names).sort.each { |name| add(:dangerous, type_name, "now implements #{name}") }
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def members(type_name, old_type, new_type)
|
|
239
|
+
old_names = @old.possible_types(old_type).map(&:graphql_name)
|
|
240
|
+
new_names = @new.possible_types(new_type).map(&:graphql_name)
|
|
241
|
+
(old_names - new_names).sort.each { |name| add(:breaking, type_name, "member #{name} removed") }
|
|
242
|
+
(new_names - old_names).sort.each { |name| add(:dangerous, type_name, "member #{name} added") }
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def values(type_name, old_type, new_type)
|
|
246
|
+
old_values = by_name(@old.enum_values(old_type))
|
|
247
|
+
new_values = by_name(@new.enum_values(new_type))
|
|
248
|
+
old_values.each do |name, old_value|
|
|
249
|
+
path = "#{type_name}.#{name}"
|
|
250
|
+
new_value = new_values[name]
|
|
251
|
+
next add(:breaking, path, "value removed") unless new_value
|
|
252
|
+
|
|
253
|
+
deprecation(path, old_value, new_value)
|
|
254
|
+
description(path, old_value, new_value)
|
|
255
|
+
end
|
|
256
|
+
(new_values.keys - old_values.keys).each { |name| add(:dangerous, "#{type_name}.#{name}", "value added") }
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# Records a type change and answers its bucket, or nil when the type is the same.
|
|
260
|
+
def type_change(path, old_type, new_type, output:)
|
|
261
|
+
bucket = output ? output_change(old_type, new_type) : input_change(old_type, new_type)
|
|
262
|
+
return if bucket == :same
|
|
263
|
+
|
|
264
|
+
add(bucket, path, "type changed from #{old_type.to_type_signature} to #{new_type.to_type_signature}")
|
|
265
|
+
bucket
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# A client reading an output copes with it becoming non-null, but not with it becoming nullable. Becoming non-null
|
|
269
|
+
# is still dangerous: two fields of one response name must have the same shape to merge (GraphQL spec, "Field
|
|
270
|
+
# Selection Merging"), so a query selecting it beside a nullable field of another type stops validating.
|
|
271
|
+
def output_change(old_type, new_type)
|
|
272
|
+
if old_type.non_null?
|
|
273
|
+
return :breaking unless new_type.non_null?
|
|
274
|
+
|
|
275
|
+
output_change(old_type.of_type, new_type.of_type)
|
|
276
|
+
elsif new_type.non_null?
|
|
277
|
+
stricter(output_change(old_type, new_type.of_type))
|
|
278
|
+
else
|
|
279
|
+
wrapped_change(old_type, new_type) { |old_item, new_item| output_change(old_item, new_item) }
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# A client sending an input copes with it becoming nullable (a `T!` variable is allowed where `T` is expected,
|
|
284
|
+
# "All Variable Usages Are Allowed"), but not with it becoming non-null.
|
|
285
|
+
def input_change(old_type, new_type)
|
|
286
|
+
if new_type.non_null?
|
|
287
|
+
return :breaking unless old_type.non_null?
|
|
288
|
+
|
|
289
|
+
input_change(old_type.of_type, new_type.of_type)
|
|
290
|
+
elsif old_type.non_null?
|
|
291
|
+
looser(input_change(old_type.of_type, new_type))
|
|
292
|
+
else
|
|
293
|
+
wrapped_change(old_type, new_type) { |old_item, new_item| input_change(old_item, new_item) }
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def wrapped_change(old_type, new_type)
|
|
298
|
+
if old_type.list? || new_type.list?
|
|
299
|
+
return :breaking unless old_type.list? && new_type.list?
|
|
300
|
+
|
|
301
|
+
yield(old_type.of_type, new_type.of_type)
|
|
302
|
+
else
|
|
303
|
+
old_type.graphql_name == new_type.graphql_name ? :same : :breaking
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def stricter(bucket) = bucket == :same || bucket == :safe ? :dangerous : bucket
|
|
308
|
+
|
|
309
|
+
def looser(bucket) = bucket == :same ? :safe : bucket
|
|
310
|
+
|
|
311
|
+
def required?(input) = input.type.non_null? && !input.default_value?
|
|
312
|
+
|
|
313
|
+
def default_changed?(old_input, new_input)
|
|
314
|
+
old_input.default_value? != new_input.default_value? ||
|
|
315
|
+
(old_input.default_value? && old_input.default_value != new_input.default_value)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def default_message(old_input, new_input)
|
|
319
|
+
return "default value #{new_input.default_value.inspect} added" unless old_input.default_value?
|
|
320
|
+
return "default value #{old_input.default_value.inspect} removed" unless new_input.default_value?
|
|
321
|
+
|
|
322
|
+
"default value changed from #{old_input.default_value.inspect} to #{new_input.default_value.inspect}"
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
def deprecation(path, old_member, new_member)
|
|
326
|
+
old_reason = deprecation_reason(old_member)
|
|
327
|
+
new_reason = deprecation_reason(new_member)
|
|
328
|
+
if old_reason.nil? && new_reason
|
|
329
|
+
add(:dangerous, path, "deprecated: #{new_reason}")
|
|
330
|
+
elsif old_reason && new_reason.nil?
|
|
331
|
+
add(:safe, path, "no longer deprecated")
|
|
332
|
+
elsif old_reason != new_reason
|
|
333
|
+
add(:safe, path, "deprecation reason changed")
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def deprecation_reason(member) = member.respond_to?(:deprecation_reason) ? member.deprecation_reason : nil
|
|
338
|
+
|
|
339
|
+
def description(path, old_member, new_member)
|
|
340
|
+
add(:safe, path, "description changed") if old_member.description != new_member.description
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def by_name(members) = members.to_h { |member| [member.graphql_name, member] }
|
|
344
|
+
end
|
|
345
|
+
private_constant :Walk
|
|
346
|
+
end
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Versioning
|
|
6
|
+
class Dump
|
|
7
|
+
# What `rake "graphql_armory:versions:dump[YourSchema,schema]"` runs: writes the files, says which, and answers an
|
|
8
|
+
# exit status. The directory defaults to `schema`.
|
|
9
|
+
class Command
|
|
10
|
+
USAGE = "Name the schema and a directory: rake \"graphql_armory:versions:dump[YourSchema,schema]\""
|
|
11
|
+
|
|
12
|
+
# The rake task's action, looked for among the task's actions like the other tasks'.
|
|
13
|
+
RAKE_ACTION = proc do |_task, args|
|
|
14
|
+
Rake::Task[:environment].invoke if Rake::Task.task_defined?(:environment)
|
|
15
|
+
status = new.(args[:schema], args[:dir] || "schema")
|
|
16
|
+
exit(status) unless status.zero?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def initialize(out: $stdout)
|
|
20
|
+
@out = out
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def call(schema_name, dir)
|
|
24
|
+
return fail_with(USAGE) if schema_name.to_s.empty? || dir.to_s.empty?
|
|
25
|
+
|
|
26
|
+
Dump.(Object.const_get(schema_name), dir).each { |file| @out.puts("Wrote #{file}") }
|
|
27
|
+
0
|
|
28
|
+
rescue ConfigurationError => error
|
|
29
|
+
fail_with(error.message)
|
|
30
|
+
rescue NameError => error
|
|
31
|
+
raise unless schema_name.to_s.split("::").include?(error.name.to_s)
|
|
32
|
+
|
|
33
|
+
fail_with("No schema named #{schema_name}")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def fail_with(message)
|
|
39
|
+
@out.puts(message)
|
|
40
|
+
1
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
|
|
5
|
+
module GraphQL
|
|
6
|
+
module Armory
|
|
7
|
+
module Versioning
|
|
8
|
+
# Writes each served version's SDL to `<dir>/<label>.graphql`, plus `<dir>/latest.graphql`: committed, they make every
|
|
9
|
+
# schema change reviewable per version in a pull request.
|
|
10
|
+
#
|
|
11
|
+
# GraphQL::Armory::Versioning::Dump.(AppSchema, "schema") # => ["schema/2024-06-01.graphql", ..., "schema/latest.graphql"]
|
|
12
|
+
#
|
|
13
|
+
# Each file is `to_definition(context: { armory_version: label, armory_full_schema: true })`: what a client of that
|
|
14
|
+
# version sees, with nothing hidden by Authorization's `visible_when:`. Retired versions are not written, and a file
|
|
15
|
+
# already there for one is left alone. Every SDL is printed before the first file is written.
|
|
16
|
+
class Dump
|
|
17
|
+
LATEST = "latest"
|
|
18
|
+
|
|
19
|
+
# Where the files go. `Directory.new` with nothing writes nothing.
|
|
20
|
+
class Directory
|
|
21
|
+
def self.build(path)
|
|
22
|
+
raise ConfigurationError, "Dump needs a directory to write to, got #{path.inspect}" if path.to_s.empty?
|
|
23
|
+
|
|
24
|
+
new(path.to_s)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def initialize(path = nil)
|
|
28
|
+
@path = path
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Writes one file and answers its path, or nil when it wrote nothing.
|
|
32
|
+
def write(name, text)
|
|
33
|
+
return unless @path
|
|
34
|
+
|
|
35
|
+
FileUtils.mkdir_p(@path)
|
|
36
|
+
file = File.join(@path, name)
|
|
37
|
+
# A link would send the version's SDL wherever it points.
|
|
38
|
+
raise ConfigurationError, "#{file} is a symbolic link: the dump writes only plain files in its directory" if File.symlink?(file)
|
|
39
|
+
|
|
40
|
+
File.write(file, text)
|
|
41
|
+
file
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Records what it is asked to write (`written`, name => text) and answers the name; told to fail, it raises
|
|
45
|
+
# that error instead and records nothing.
|
|
46
|
+
class Substitute
|
|
47
|
+
attr_reader :written
|
|
48
|
+
|
|
49
|
+
def initialize
|
|
50
|
+
@written = {}
|
|
51
|
+
@error = nil
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def fail_with(error)
|
|
55
|
+
@error = error
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def write(name, text)
|
|
59
|
+
raise @error if @error
|
|
60
|
+
|
|
61
|
+
@written[name] = text
|
|
62
|
+
name
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.build(dir) = new(Directory.build(dir))
|
|
68
|
+
|
|
69
|
+
def self.call(schema, dir, context: {}) = build(dir).(schema, context: context)
|
|
70
|
+
|
|
71
|
+
# `new` is inert: it prints nothing anywhere.
|
|
72
|
+
def initialize(directory = Directory.new)
|
|
73
|
+
@directory = directory
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Answers what the directory wrote, in order: each served version oldest first, then latest.
|
|
77
|
+
def call(schema, context: {})
|
|
78
|
+
versions = Integration.gate(schema).versions
|
|
79
|
+
labels = versions.each_served.map(&:label)
|
|
80
|
+
check_labels(labels)
|
|
81
|
+
|
|
82
|
+
sdl = labels.to_h { |label| [label, schema.to_definition(context: context.merge(armory_version: label, armory_full_schema: true))] }
|
|
83
|
+
sdl[LATEST] = sdl.fetch(versions.latest.label)
|
|
84
|
+
sdl.filter_map { |label, text| @directory.write("#{label}.graphql", text) }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
# A label becomes a file name, so it must stay one inside the directory and not collide with another.
|
|
90
|
+
def check_labels(labels)
|
|
91
|
+
labels.each do |label|
|
|
92
|
+
if label.match?(%r{[/\\\0]}) || label.start_with?(".") || label.casecmp?(LATEST)
|
|
93
|
+
raise ConfigurationError, "Dump cannot write version #{label.inspect} to a file: a label it writes must not contain " \
|
|
94
|
+
"a slash, a backslash or a NUL, start with a dot, or be #{LATEST.inspect}"
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
labels.combination(2).each do |one, other|
|
|
98
|
+
if one.casecmp?(other)
|
|
99
|
+
raise ConfigurationError, "Dump cannot write versions #{one.inspect} and #{other.inspect}: their files would be one " \
|
|
100
|
+
"on a case-insensitive file system"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Versioning
|
|
6
|
+
# Decides, once per request, which version it is served, from `context[:armory_version]`: a served label is served,
|
|
7
|
+
# a retired one is refused, nothing at all follows the `default:` policy, and anything else (an unknown or future
|
|
8
|
+
# label, an empty String, a non-String) is refused. It never falls back to another version.
|
|
9
|
+
class Gate
|
|
10
|
+
# What `default:` holds when `use` was not given one.
|
|
11
|
+
UNSET = Object.new.tap { |unset| def unset.inspect = "(not given)" }.freeze
|
|
12
|
+
|
|
13
|
+
# When every label looks like a date, the list must be ascending: its order is the whole meaning.
|
|
14
|
+
DATE = /\A\d{4}-\d{2}-\d{2}\z/
|
|
15
|
+
|
|
16
|
+
# Validates `use`'s settings and wires the real list, policy and telemetry; `use` goes through here.
|
|
17
|
+
def self.build(versions:, default: UNSET, retired: [], sinks: [], on_sink_error: nil, own_checks: {})
|
|
18
|
+
check_versions(versions)
|
|
19
|
+
check_retired(versions, retired)
|
|
20
|
+
check_own_checks(own_checks)
|
|
21
|
+
policy = default_policy(default, versions, retired)
|
|
22
|
+
declared = own_checks.to_h { |holder, methods| [holder, methods.to_h { |name, reason| [name, reason.dup.freeze] }.freeze] }.freeze
|
|
23
|
+
|
|
24
|
+
new(versions: Versions.new(versions, retired: retired), default: policy, telemetry: Telemetry.build(sinks: sinks, on_sink_error: on_sink_error),
|
|
25
|
+
own_checks: declared)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.check_versions(versions)
|
|
29
|
+
raise ConfigurationError, "versions: must be an Array of labels, oldest first, got #{versions.class}" unless versions.is_a?(Array)
|
|
30
|
+
raise ConfigurationError, "versions: must name at least one version" if versions.empty?
|
|
31
|
+
|
|
32
|
+
versions.each do |label|
|
|
33
|
+
raise ConfigurationError, "versions: each label must be a non-empty String, got #{label.inspect}" unless label.is_a?(String) && !label.empty?
|
|
34
|
+
end
|
|
35
|
+
duplicate = versions.find { |label| versions.count(label) > 1 }
|
|
36
|
+
raise ConfigurationError, "versions: #{duplicate.inspect} is listed twice" if duplicate
|
|
37
|
+
raise ConfigurationError, "versions: dates must be in ascending order, oldest first" if versions.all?(DATE) && versions != versions.sort
|
|
38
|
+
end
|
|
39
|
+
private_class_method :check_versions
|
|
40
|
+
|
|
41
|
+
def self.check_retired(versions, retired)
|
|
42
|
+
raise ConfigurationError, "retired: must be an Array of labels, got #{retired.class}" unless retired.is_a?(Array)
|
|
43
|
+
raise ConfigurationError, "retired: must be the oldest versions, a prefix of versions:, got #{retired.inspect}" unless versions.first(retired.size) == retired
|
|
44
|
+
raise ConfigurationError, "retired: leaves no version to serve" if retired.size == versions.size
|
|
45
|
+
end
|
|
46
|
+
private_class_method :check_retired
|
|
47
|
+
|
|
48
|
+
# The app's own `visible?` methods the audit does not report: `{ Holder => { visible?: "the reason you keep it" } }`.
|
|
49
|
+
def self.check_own_checks(checks)
|
|
50
|
+
valid = checks.is_a?(Hash) && checks.all? do |holder, methods|
|
|
51
|
+
holder.is_a?(Module) && methods.is_a?(Hash) && !methods.empty? &&
|
|
52
|
+
methods.all? { |name, reason| name == :visible? && reason.is_a?(String) && !reason.strip.empty? }
|
|
53
|
+
end
|
|
54
|
+
return if valid
|
|
55
|
+
|
|
56
|
+
raise ConfigurationError, "own_checks: must map a class or module to { visible?: \"the reason you keep it\" }, got #{checks.inspect}"
|
|
57
|
+
end
|
|
58
|
+
private_class_method :check_own_checks
|
|
59
|
+
|
|
60
|
+
def self.default_policy(default, versions, retired)
|
|
61
|
+
case default
|
|
62
|
+
when :latest then Default::Latest.new
|
|
63
|
+
when :refuse then Default::Refuse.new
|
|
64
|
+
when String
|
|
65
|
+
raise ConfigurationError, "default: #{default.inspect} is retired: pick a served version" if retired.include?(default)
|
|
66
|
+
raise ConfigurationError, "default: #{default.inspect} is not in versions:" unless versions.include?(default)
|
|
67
|
+
|
|
68
|
+
Default::Label.new(default)
|
|
69
|
+
when UNSET
|
|
70
|
+
raise ConfigurationError, "default: is required: :latest (a new API), the label your clients see today (an API with clients in production), or :refuse"
|
|
71
|
+
else
|
|
72
|
+
raise ConfigurationError, "default: must be :latest, :refuse or a served label, got #{default.inspect}"
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
private_class_method :default_policy
|
|
76
|
+
|
|
77
|
+
# `new` is inert: it knows no version and refuses every request.
|
|
78
|
+
def initialize(versions: Versions::Null.new, default: Default::Refuse.new, telemetry: Telemetry.new, own_checks: {})
|
|
79
|
+
@versions = versions
|
|
80
|
+
@default = default
|
|
81
|
+
@telemetry = telemetry
|
|
82
|
+
@own_checks = own_checks
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The list, and `own_checks:`, for the audit.
|
|
86
|
+
attr_reader :versions, :own_checks
|
|
87
|
+
|
|
88
|
+
# The decision for a request, recorded as one signal. `Lookup` calls it once per query.
|
|
89
|
+
def call(context)
|
|
90
|
+
decision = resolve(context.respond_to?(:[]) ? context[:armory_version] : nil)
|
|
91
|
+
@telemetry.record(decision.signal, context: context.respond_to?(:[]) ? context : {}, **decision.data)
|
|
92
|
+
decision
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The same decision for a label, recording nothing: for contexts that did not come through `query_class`.
|
|
96
|
+
def resolve(label)
|
|
97
|
+
case label
|
|
98
|
+
when nil then @default.(@versions)
|
|
99
|
+
when String
|
|
100
|
+
return Decision.refused(:retired, version: @versions.fetch(label)) if @versions.retired?(label)
|
|
101
|
+
return Decision.served(@versions.fetch(label)) if @versions.served?(label)
|
|
102
|
+
|
|
103
|
+
Decision.refused(:unknown)
|
|
104
|
+
else Decision.refused(:unknown)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Answers the decision it is told (a refusal for a missing version until told otherwise) and records the contexts
|
|
109
|
+
# and labels it was asked about: for tests, instead of hand-written gates.
|
|
110
|
+
class Substitute
|
|
111
|
+
attr_reader :contexts, :labels
|
|
112
|
+
|
|
113
|
+
def initialize(decision = Decision.refused(:missing))
|
|
114
|
+
@decision = decision
|
|
115
|
+
@contexts = []
|
|
116
|
+
@labels = []
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def answer(decision)
|
|
120
|
+
@decision = decision
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def versions = Versions::Null.new
|
|
124
|
+
|
|
125
|
+
def own_checks = {}
|
|
126
|
+
|
|
127
|
+
def call(context)
|
|
128
|
+
@contexts << context
|
|
129
|
+
@decision
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def resolve(label)
|
|
133
|
+
@labels << label
|
|
134
|
+
@decision
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|