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,403 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Versioning
|
|
6
|
+
# Test data for versioning, reusable by apps and other gems.
|
|
7
|
+
module Controls
|
|
8
|
+
# Three versions: the oldest retired, two served.
|
|
9
|
+
module Versions
|
|
10
|
+
LABELS = %w[2024-01-15 2024-06-01 2025-01-01].freeze
|
|
11
|
+
RETIRED = %w[2024-01-15].freeze
|
|
12
|
+
|
|
13
|
+
def self.example = Versioning::Versions.new(LABELS, retired: RETIRED)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# A request's context naming a version.
|
|
17
|
+
module Context
|
|
18
|
+
def self.example(version: "2024-06-01") = { armory_version: version }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# A base field class with `Base`.
|
|
22
|
+
module Field
|
|
23
|
+
def self.example
|
|
24
|
+
Class.new(GraphQL::Schema::Field) { include Versioning::Base }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# `number` in every version; `total` from 2024-06-01; `amount` deprecated with graphql-ruby's own
|
|
29
|
+
# `deprecation_reason:` and removed in 2025-01-01; `tags(first:)` from 2025-01-01.
|
|
30
|
+
module Invoice
|
|
31
|
+
def self.example(base_field = Field.example)
|
|
32
|
+
Class.new(GraphQL::Schema::Object) do
|
|
33
|
+
graphql_name "Invoice"
|
|
34
|
+
field_class(base_field)
|
|
35
|
+
field :number, String, null: false
|
|
36
|
+
field :total, Integer, null: false, added: "2024-06-01"
|
|
37
|
+
field :amount, Integer, null: false, deprecation_reason: "Use `total`", removed: "2025-01-01"
|
|
38
|
+
field :tags, [String], null: false, added: "2025-01-01" do
|
|
39
|
+
argument :first, Integer, required: false
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
module Query
|
|
46
|
+
def self.example(invoice = Invoice.example)
|
|
47
|
+
Class.new(GraphQL::Schema::Object) do
|
|
48
|
+
graphql_name "Query"
|
|
49
|
+
field :invoice, invoice, null: false
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The root object for Query.example; it adds the name of each field it resolves to `resolved`.
|
|
55
|
+
module Root
|
|
56
|
+
def self.example(resolved = [])
|
|
57
|
+
invoice = Class.new do
|
|
58
|
+
define_method(:number) { resolved << :number; "A-1" }
|
|
59
|
+
define_method(:total) { resolved << :total; 100 }
|
|
60
|
+
define_method(:amount) { resolved << :amount; 100 }
|
|
61
|
+
define_method(:tags) { |first: nil| resolved << :tags; %w[paid urgent].first(first || 2) }
|
|
62
|
+
end.new
|
|
63
|
+
Class.new { define_method(:invoice) { resolved << :invoice; invoice } }.new
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# A base argument class with `Base`, for field arguments and input-object fields.
|
|
68
|
+
module Argument
|
|
69
|
+
def self.example
|
|
70
|
+
Class.new(GraphQL::Schema::Argument) { include Versioning::Base }
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# A base enum value class with `Base`.
|
|
75
|
+
module EnumValue
|
|
76
|
+
def self.example
|
|
77
|
+
Class.new(GraphQL::Schema::EnumValue) { include Versioning::Base }
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# `PAID` in every version; `VOID` from 2024-06-01; `CANCELLED` removed in 2025-01-01.
|
|
82
|
+
module Status
|
|
83
|
+
def self.example(base_value = EnumValue.example)
|
|
84
|
+
Class.new(GraphQL::Schema::Enum) do
|
|
85
|
+
graphql_name "Status"
|
|
86
|
+
enum_value_class(base_value)
|
|
87
|
+
value "PAID"
|
|
88
|
+
value "VOID", added: "2024-06-01"
|
|
89
|
+
value "CANCELLED", removed: "2025-01-01"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# `status` in every version; `since` from 2025-01-01; `code` removed in 2025-01-01.
|
|
95
|
+
module InvoiceFilter
|
|
96
|
+
def self.example(status = Status.example, base_argument = Argument.example)
|
|
97
|
+
Class.new(GraphQL::Schema::InputObject) do
|
|
98
|
+
graphql_name "InvoiceFilter"
|
|
99
|
+
argument_class(base_argument)
|
|
100
|
+
argument :status, status, required: false
|
|
101
|
+
argument :since, String, required: false, added: "2025-01-01"
|
|
102
|
+
argument :code, String, required: false, removed: "2025-01-01"
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# `invoices(filter:, first:, page:)`: `first` from 2025-01-01, `page` removed in 2025-01-01; `status` answers what the
|
|
108
|
+
# root holds, so a test can return a value outside the client's version.
|
|
109
|
+
module MembersQuery
|
|
110
|
+
def self.example(status: Status.example, base_argument: Argument.example)
|
|
111
|
+
filter = InvoiceFilter.example(status, base_argument)
|
|
112
|
+
base_field = Class.new(GraphQL::Schema::Field) { argument_class(base_argument) }
|
|
113
|
+
|
|
114
|
+
Class.new(GraphQL::Schema::Object) do
|
|
115
|
+
graphql_name "Query"
|
|
116
|
+
field_class(base_field)
|
|
117
|
+
field :invoices, [String], null: false do
|
|
118
|
+
argument :filter, filter, required: false
|
|
119
|
+
argument :first, Integer, required: false, added: "2025-01-01"
|
|
120
|
+
argument :page, Integer, required: false, removed: "2025-01-01"
|
|
121
|
+
end
|
|
122
|
+
field :status, status, null: false
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# The root object for MembersQuery.example: `invoices` adds `[:invoices, arguments]` to `resolved` and answers the
|
|
128
|
+
# arguments as `name=value` Strings (`filter.key=value` for the filter's fields); `status` answers the value it was given.
|
|
129
|
+
module MembersRoot
|
|
130
|
+
def self.example(resolved = [], status: "PAID")
|
|
131
|
+
Class.new do
|
|
132
|
+
define_method(:invoices) do |filter: nil, first: nil, page: nil|
|
|
133
|
+
arguments = { filter: filter&.to_h, first: first, page: page }.compact
|
|
134
|
+
resolved << [:invoices, arguments]
|
|
135
|
+
arguments.flat_map do |name, value|
|
|
136
|
+
value.is_a?(Hash) ? value.map { |key, item| "#{name}.#{key}=#{item}" } : ["#{name}=#{value}"]
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
define_method(:status) { resolved << :status; status }
|
|
140
|
+
end.new
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# `deprecated:` on every member kind, each deprecated from `FROM`: an interface's field, an object's field with
|
|
145
|
+
# arguments of their own lifecycles in its block, a field argument, an input-object field, a resolver's argument, a
|
|
146
|
+
# `resolver:` field and an enum value.
|
|
147
|
+
module Deprecations
|
|
148
|
+
FROM = "2025-01-01"
|
|
149
|
+
|
|
150
|
+
# Base classes with `Base` for every owner and member kind; with `authorization: true`, Authorization's `Base` too.
|
|
151
|
+
module Bases
|
|
152
|
+
def self.example(authorization: false)
|
|
153
|
+
mods = [Versioning::Base]
|
|
154
|
+
mods << Armory::Authorization::Base if authorization
|
|
155
|
+
argument = Class.new(GraphQL::Schema::Argument) { mods.each { |mod| include mod } }
|
|
156
|
+
field = Class.new(GraphQL::Schema::Field) { mods.each { |mod| include mod }; argument_class(argument) }
|
|
157
|
+
value = Class.new(GraphQL::Schema::EnumValue) { include Versioning::Base }
|
|
158
|
+
{
|
|
159
|
+
argument: argument, field: field,
|
|
160
|
+
object: Class.new(GraphQL::Schema::Object) { mods.each { |mod| include mod }; field_class(field) },
|
|
161
|
+
# Named as an app's `Types::BaseInterface` is: some walkers read the name of every interface an interface includes.
|
|
162
|
+
interface: Module.new { include GraphQL::Schema::Interface; include Versioning::Base; graphql_name "BaseInterface"; field_class(field) },
|
|
163
|
+
input: Class.new(GraphQL::Schema::InputObject) { mods.each { |mod| include mod }; argument_class(argument) },
|
|
164
|
+
enum: Class.new(GraphQL::Schema::Enum) { include Versioning::Base; enum_value_class(value) },
|
|
165
|
+
resolver: Class.new(GraphQL::Schema::Resolver) { include Versioning::Base; argument_class(argument) },
|
|
166
|
+
subscription: Class.new(GraphQL::Schema::Subscription) { include Versioning::Base; argument_class(argument); field_class(field) }
|
|
167
|
+
}
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# The query type. Objects declare an Authorization rule when their base has Authorization's `Base`.
|
|
172
|
+
module Query
|
|
173
|
+
def self.example(bases = Bases.example)
|
|
174
|
+
from = FROM
|
|
175
|
+
open = ->(type) { type.authorize(:none, reason: "test data", covers_fields: true) if type.respond_to?(:authorize) }
|
|
176
|
+
status = Class.new(bases[:enum]) do
|
|
177
|
+
graphql_name "Status"
|
|
178
|
+
value "PAID", value: :paid
|
|
179
|
+
value "CANCELLED", value: :cancelled, deprecated: from, deprecation_reason: "Use `VOID`"
|
|
180
|
+
end
|
|
181
|
+
filter = Class.new(bases[:input]) do
|
|
182
|
+
graphql_name "InvoiceFilter"
|
|
183
|
+
argument :code, String, required: false, deprecated: from, deprecation_reason: "Use `number`"
|
|
184
|
+
argument :number, String, required: false
|
|
185
|
+
end
|
|
186
|
+
priced = Module.new do
|
|
187
|
+
include bases[:interface]
|
|
188
|
+
graphql_name "Priced"
|
|
189
|
+
field :amount, Integer, null: true, deprecated: from, deprecation_reason: "Use `total`"
|
|
190
|
+
end
|
|
191
|
+
invoice = Class.new(bases[:object]) do
|
|
192
|
+
graphql_name "Invoice"
|
|
193
|
+
open.(self)
|
|
194
|
+
implements priced
|
|
195
|
+
field :total, Integer, null: true
|
|
196
|
+
field :tags, [String], null: true, deprecated: from, deprecation_reason: "Use `labels`" do
|
|
197
|
+
argument :first, Integer, required: false, added: from
|
|
198
|
+
argument :page, Integer, required: false, removed: from
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
lookup = Class.new(bases[:resolver]) do
|
|
202
|
+
graphql_name "Lookup"
|
|
203
|
+
type [String], null: false
|
|
204
|
+
argument :code, String, required: false, deprecated: from, deprecation_reason: "Use `number`"
|
|
205
|
+
argument :number, String, required: false
|
|
206
|
+
define_method(:resolve) { |**arguments| arguments.map { |name, item| "#{name}=#{item}" }.sort }
|
|
207
|
+
end
|
|
208
|
+
Class.new(bases[:object]) do
|
|
209
|
+
graphql_name "Query"
|
|
210
|
+
open.(self)
|
|
211
|
+
field :invoice, invoice, null: false
|
|
212
|
+
field :invoices, [String], null: false do
|
|
213
|
+
argument :filter, filter, required: false
|
|
214
|
+
argument :page, Integer, required: false, deprecated: from, deprecation_reason: "Use `cursor`"
|
|
215
|
+
end
|
|
216
|
+
field :lookup, resolver: lookup
|
|
217
|
+
field :find, resolver: lookup, deprecated: from, deprecation_reason: "Use `lookup`"
|
|
218
|
+
field :status, status, null: false
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# The root object for Query.example: `tags` and `invoices` answer their arguments as `name=value` Strings
|
|
224
|
+
# (`filter.key=value` for the filter's fields); `status` answers CANCELLED.
|
|
225
|
+
module Root
|
|
226
|
+
def self.example
|
|
227
|
+
strings = ->(arguments) { arguments.compact.flat_map { |name, item| item.is_a?(Hash) ? item.map { |key, inner| "#{name}.#{key}=#{inner}" } : ["#{name}=#{item}"] } }
|
|
228
|
+
invoice = Class.new do
|
|
229
|
+
define_method(:total) { 100 }
|
|
230
|
+
define_method(:amount) { 100 }
|
|
231
|
+
define_method(:tags) { |first: nil, page: nil| strings.(first: first, page: page) }
|
|
232
|
+
end.new
|
|
233
|
+
Class.new do
|
|
234
|
+
define_method(:invoice) { invoice }
|
|
235
|
+
define_method(:invoices) { |filter: nil, page: nil| strings.(filter: filter&.to_h, page: page) }
|
|
236
|
+
define_method(:status) { :cancelled }
|
|
237
|
+
end.new
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# A schema that evolves from 2024-06-01 to 2025-01-01 with one change of each kind `Diff` classifies, under
|
|
243
|
+
# `Schema::Visibility` or the legacy Warden. `Evolution::BREAKING`, `DANGEROUS` and `SAFE` list what `Diff` answers.
|
|
244
|
+
#
|
|
245
|
+
# - Money changes shape (a type twin: `cents`, then `amount` and `currency`); Ref changes kind (a scalar, then an
|
|
246
|
+
# object); Customer is renamed Client; Receipt leaves and Contract arrives (both in the Document union);
|
|
247
|
+
# Archivable leaves, and with it Invoice's `archived`.
|
|
248
|
+
# - Invoice: `amount` removed; `note` String to String! (an output made stricter); `code` String! to String;
|
|
249
|
+
# `label` deprecated; `tags` gains a required `after:` and an optional `last:`.
|
|
250
|
+
# - Status: VOID added, CANCELLED removed. InvoiceFilter: `code` removed, `since` added, a required `kind` added.
|
|
251
|
+
# - Query.invoices: `first:` default 10 to 20; `page:` Int! to Int; `limit:` Int to Int!.
|
|
252
|
+
module Evolution
|
|
253
|
+
FROM = "2024-06-01"
|
|
254
|
+
TO = "2025-01-01"
|
|
255
|
+
|
|
256
|
+
BREAKING = [
|
|
257
|
+
["Archivable", "type removed"],
|
|
258
|
+
["Customer", "type removed"],
|
|
259
|
+
["Document", "member Receipt removed"],
|
|
260
|
+
["Invoice", "no longer implements Archivable"],
|
|
261
|
+
["Invoice.amount", "field removed"],
|
|
262
|
+
["Invoice.archived", "field removed"],
|
|
263
|
+
["Invoice.code", "type changed from String! to String"],
|
|
264
|
+
["Invoice.tags.after", "required argument added"],
|
|
265
|
+
["InvoiceFilter.code", "input field removed"],
|
|
266
|
+
["InvoiceFilter.kind", "required input field added"],
|
|
267
|
+
["Money.cents", "field removed"],
|
|
268
|
+
["Query.customer", "type changed from Customer to Client"],
|
|
269
|
+
["Query.invoices.limit", "type changed from Int to Int!"],
|
|
270
|
+
["Receipt", "type removed"],
|
|
271
|
+
["Ref", "kind changed from SCALAR to OBJECT"],
|
|
272
|
+
["Status.CANCELLED", "value removed"],
|
|
273
|
+
].freeze
|
|
274
|
+
|
|
275
|
+
DANGEROUS = [
|
|
276
|
+
["Document", "member Contract added"],
|
|
277
|
+
["Invoice.label", "deprecated: Use `number`"],
|
|
278
|
+
["Invoice.note", "type changed from String to String!"],
|
|
279
|
+
["Query.invoices.first", "default value changed from 10 to 20"],
|
|
280
|
+
["Status.VOID", "value added"],
|
|
281
|
+
].freeze
|
|
282
|
+
|
|
283
|
+
SAFE = [
|
|
284
|
+
["Client", "type added"],
|
|
285
|
+
["Contract", "type added"],
|
|
286
|
+
["Invoice.tags.last", "argument added"],
|
|
287
|
+
["InvoiceFilter.since", "input field added"],
|
|
288
|
+
["Money.amount", "field added"],
|
|
289
|
+
["Money.currency", "field added"],
|
|
290
|
+
["Query.invoices.page", "type changed from Int! to Int"],
|
|
291
|
+
].freeze
|
|
292
|
+
|
|
293
|
+
def self.example(visibility: :visibility, base: GraphQL::Schema)
|
|
294
|
+
base_argument = Argument.example
|
|
295
|
+
base_field = Class.new(GraphQL::Schema::Field) { include Versioning::Base; argument_class(base_argument) }
|
|
296
|
+
base_object = Class.new(GraphQL::Schema::Object) { include Versioning::Base; field_class(base_field) }
|
|
297
|
+
base_value = EnumValue.example
|
|
298
|
+
base_input = Class.new(GraphQL::Schema::InputObject) { include Versioning::Base; argument_class(base_argument) }
|
|
299
|
+
|
|
300
|
+
archivable = Module.new do
|
|
301
|
+
include GraphQL::Schema::Interface
|
|
302
|
+
include Versioning::Base
|
|
303
|
+
graphql_name "Archivable"
|
|
304
|
+
field_class(base_field)
|
|
305
|
+
lifecycle removed: TO
|
|
306
|
+
field :archived, GraphQL::Types::Boolean, null: false
|
|
307
|
+
end
|
|
308
|
+
money_v1 = Class.new(base_object) { graphql_name "Money"; lifecycle removed: TO; field :cents, Integer, null: false }
|
|
309
|
+
money_v2 = Class.new(base_object) do
|
|
310
|
+
graphql_name "Money"
|
|
311
|
+
lifecycle added: TO
|
|
312
|
+
field :amount, String, null: false
|
|
313
|
+
field :currency, String, null: false
|
|
314
|
+
end
|
|
315
|
+
status = Class.new(GraphQL::Schema::Enum) do
|
|
316
|
+
include Versioning::Base
|
|
317
|
+
graphql_name "Status"
|
|
318
|
+
enum_value_class(base_value)
|
|
319
|
+
value "PAID"
|
|
320
|
+
value "VOID", added: TO
|
|
321
|
+
value "CANCELLED", removed: TO
|
|
322
|
+
end
|
|
323
|
+
invoice = Class.new(base_object) do
|
|
324
|
+
graphql_name "Invoice"
|
|
325
|
+
implements archivable
|
|
326
|
+
field :number, String, null: false
|
|
327
|
+
field :total, money_v1, null: false, removed: TO
|
|
328
|
+
field :total, money_v2, null: false, added: TO
|
|
329
|
+
field :amount, Integer, null: false, removed: TO
|
|
330
|
+
field :note, String, null: true, removed: TO
|
|
331
|
+
field :note, String, null: false, added: TO
|
|
332
|
+
field :code, String, null: false, removed: TO
|
|
333
|
+
field :code, String, null: true, added: TO
|
|
334
|
+
field :label, String, null: true, removed: TO
|
|
335
|
+
field :label, String, null: true, added: TO, deprecation_reason: "Use `number`"
|
|
336
|
+
field :status, status, null: false
|
|
337
|
+
field :tags, [String], null: false do
|
|
338
|
+
argument :first, Integer, required: false
|
|
339
|
+
argument :after, String, required: true, added: TO
|
|
340
|
+
argument :last, Integer, required: false, added: TO
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
receipt = Class.new(base_object) { graphql_name "Receipt"; lifecycle removed: TO; field :number, String, null: false }
|
|
344
|
+
contract = Class.new(base_object) { graphql_name "Contract"; lifecycle added: TO; field :party, String, null: false }
|
|
345
|
+
document = Class.new(GraphQL::Schema::Union) do
|
|
346
|
+
graphql_name "Document"
|
|
347
|
+
possible_types invoice, receipt, contract
|
|
348
|
+
end
|
|
349
|
+
filter = Class.new(base_input) do
|
|
350
|
+
graphql_name "InvoiceFilter"
|
|
351
|
+
argument :status, status, required: false
|
|
352
|
+
argument :code, String, required: false, removed: TO
|
|
353
|
+
argument :since, String, required: false, added: TO
|
|
354
|
+
argument :kind, String, required: true, added: TO
|
|
355
|
+
end
|
|
356
|
+
ref_v1 = Class.new(GraphQL::Schema::Scalar) { include Versioning::Base; graphql_name "Ref"; lifecycle removed: TO }
|
|
357
|
+
ref_v2 = Class.new(base_object) { graphql_name "Ref"; lifecycle added: TO; field :id, String, null: false }
|
|
358
|
+
customer = Class.new(base_object) { graphql_name "Customer"; lifecycle removed: TO; field :name, String, null: false }
|
|
359
|
+
client = Class.new(base_object) { graphql_name "Client"; lifecycle added: TO; field :name, String, null: false }
|
|
360
|
+
|
|
361
|
+
query = Class.new(base_object) do
|
|
362
|
+
graphql_name "Query"
|
|
363
|
+
field :invoice, invoice, null: false
|
|
364
|
+
field :document, document, null: true
|
|
365
|
+
field :ref, ref_v1, null: true, removed: TO
|
|
366
|
+
field :ref, ref_v2, null: true, added: TO
|
|
367
|
+
field :customer, customer, null: true, removed: TO
|
|
368
|
+
field :customer, client, null: true, added: TO
|
|
369
|
+
field :invoices, [String], null: false do
|
|
370
|
+
argument :filter, filter, required: false
|
|
371
|
+
argument :first, Integer, required: false, default_value: 10, removed: TO
|
|
372
|
+
argument :first, Integer, required: false, default_value: 20, added: TO
|
|
373
|
+
argument :page, Integer, required: true, removed: TO
|
|
374
|
+
argument :page, Integer, required: false, added: TO
|
|
375
|
+
argument :limit, Integer, required: false, removed: TO
|
|
376
|
+
argument :limit, Integer, required: true, added: TO
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
Schema.example(visibility: visibility, query: query, base: base)
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
# The schema over Query.example and Versions.example, under `Schema::Visibility` (`visibility: :visibility`) or the
|
|
385
|
+
# legacy `Schema::Warden` (`visibility: :warden`).
|
|
386
|
+
module Schema
|
|
387
|
+
PLUGINS = { visibility: GraphQL::Schema::Visibility, warden: GraphQL::Schema::Warden }.freeze
|
|
388
|
+
|
|
389
|
+
def self.example(visibility: :visibility, default: :latest, query: Query.example, base: GraphQL::Schema, sinks: [], on_sink_error: nil)
|
|
390
|
+
plugin = PLUGINS.fetch(visibility) { raise ArgumentError, "visibility: must be one of #{PLUGINS.keys.inspect}, got #{visibility.inspect}" }
|
|
391
|
+
query_type = query
|
|
392
|
+
|
|
393
|
+
Class.new(base) do
|
|
394
|
+
query(query_type)
|
|
395
|
+
use plugin
|
|
396
|
+
use Versioning, versions: Versions::LABELS, retired: Versions::RETIRED, default: default, sinks: sinks, on_sink_error: on_sink_error
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
end
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Versioning
|
|
6
|
+
# What the gate decided for one request: the version it is served, or why it is refused.
|
|
7
|
+
class Decision
|
|
8
|
+
# What a client reads in `extensions.code`, by reason.
|
|
9
|
+
CODES = { unknown: "UNKNOWN_API_VERSION", retired: "RETIRED_API_VERSION", missing: "MISSING_API_VERSION" }.freeze
|
|
10
|
+
|
|
11
|
+
# Fixed messages: they never quote what the client sent, nor list the versions.
|
|
12
|
+
MESSAGES = { unknown: "Unknown API version", retired: "This API version is retired", missing: "Send an API version" }.freeze
|
|
13
|
+
|
|
14
|
+
def self.served(version) = new(:served, version, nil)
|
|
15
|
+
def self.defaulted(version) = new(:defaulted, version, nil)
|
|
16
|
+
|
|
17
|
+
def self.refused(reason, version: nil)
|
|
18
|
+
raise ArgumentError, "reason must be one of #{CODES.keys.inspect}, got #{reason.inspect}" unless CODES.key?(reason)
|
|
19
|
+
|
|
20
|
+
new(:refused, version, reason)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attr_reader :outcome, :version, :reason
|
|
24
|
+
|
|
25
|
+
def initialize(outcome, version, reason)
|
|
26
|
+
@outcome = outcome
|
|
27
|
+
@version = version
|
|
28
|
+
@reason = reason
|
|
29
|
+
freeze
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def refused?
|
|
33
|
+
@outcome == :refused
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def code
|
|
37
|
+
CODES[@reason]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def message
|
|
41
|
+
MESSAGES[@reason]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The telemetry signal and its data: the schema's own label, or the reason. Never the client's value.
|
|
45
|
+
def signal
|
|
46
|
+
:"version_#{@outcome}"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def data
|
|
50
|
+
refused? ? { reason: @reason } : { version: @version.label }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# What `visible?` raises for a query whose version is refused, when a caller that skips validation reaches it.
|
|
54
|
+
def error
|
|
55
|
+
return MissingVersionError.new("#{message}: pass context: { armory_version: \"<label>\" }, or set default: on use", code) if @reason == :missing
|
|
56
|
+
|
|
57
|
+
RefusedVersionError.new(message, code)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def ==(other)
|
|
61
|
+
other.is_a?(Decision) && [outcome, version, reason] == [other.outcome, other.version, other.reason]
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# What a request with no version gets, chosen by `use`'s `default:`.
|
|
66
|
+
module Default
|
|
67
|
+
# `default: :latest`, for a new API: clients that send nothing follow the schema forward.
|
|
68
|
+
class Latest
|
|
69
|
+
def call(versions) = Decision.defaulted(versions.latest)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# `default: "2024-06-01"`, for an API with clients in production: they keep the schema they were built against.
|
|
73
|
+
class Label
|
|
74
|
+
def initialize(label)
|
|
75
|
+
@label = label.dup.freeze
|
|
76
|
+
freeze
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def call(versions) = Decision.defaulted(versions.fetch(@label))
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# `default: :refuse`: every request must name its version.
|
|
83
|
+
class Refuse
|
|
84
|
+
def call(_versions) = Decision.refused(:missing)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Versioning
|
|
6
|
+
# `deprecated: D` with `deprecation_reason:` deprecates a member from version D on. graphql-ruby's
|
|
7
|
+
# `deprecation_reason` has no context (introspection and the SDL read it as it is), so one definition is deprecated
|
|
8
|
+
# in every version or in none: the class that defines the member makes two definitions of it through its own DSL
|
|
9
|
+
# method, one living up to D with no reason and one from D with it. They are ordinary twins: each version sees one.
|
|
10
|
+
#
|
|
11
|
+
# field :amount, Integer, null: false, deprecated: "2024-06-01", deprecation_reason: "Use `total`"
|
|
12
|
+
# # is
|
|
13
|
+
# field :amount, Integer, null: false, removed: "2024-06-01"
|
|
14
|
+
# field :amount, Integer, null: false, added: "2024-06-01", deprecation_reason: "Use `total`"
|
|
15
|
+
module Deprecation
|
|
16
|
+
# The keywords of the two definitions `deprecated:` stands for, after checking what can be checked before
|
|
17
|
+
# `versions:` is known: the order of D against `added:` and `removed:` is checked by each definition's lifecycle
|
|
18
|
+
# the first time a query asks about it.
|
|
19
|
+
def self.pair(name, deprecated, kwargs)
|
|
20
|
+
reason = kwargs[:deprecation_reason]
|
|
21
|
+
unless deprecated.is_a?(String) && !deprecated.empty?
|
|
22
|
+
raise ConfigurationError, "#{name}: deprecated: must be a non-empty String naming a version, got #{deprecated.inspect}"
|
|
23
|
+
end
|
|
24
|
+
if !reason.is_a?(String) || reason.strip.empty?
|
|
25
|
+
raise ConfigurationError, "#{name}: deprecated: needs deprecation_reason:, the reason clients read from that version on"
|
|
26
|
+
end
|
|
27
|
+
if [kwargs[:added], kwargs[:removed]].include?(deprecated)
|
|
28
|
+
raise ConfigurationError, "#{name}: deprecated: must come after added: and before removed:, and #{deprecated.inspect} is one of them"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
before = kwargs.except(:deprecation_reason).merge(removed: deprecated)
|
|
32
|
+
[before, kwargs.merge(added: deprecated)]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.name_of(args, kwargs) = kwargs[:name] || args.first
|
|
36
|
+
|
|
37
|
+
# The definition from D on remembers the one before D, so tooling can read the pair as one member written once.
|
|
38
|
+
def self.mark(before, after)
|
|
39
|
+
after.instance_variable_set(:@armory_deprecated_of, before)
|
|
40
|
+
after
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# `field` on an object, interface, resolver or mutation class.
|
|
44
|
+
module Fields
|
|
45
|
+
def field(*args, deprecated: nil, **kwargs, &block)
|
|
46
|
+
return super(*args, **kwargs, &block) if deprecated.nil?
|
|
47
|
+
|
|
48
|
+
name = Deprecation.name_of(args, kwargs)
|
|
49
|
+
resolver = kwargs[:resolver] || kwargs[:mutation] || kwargs[:subscription]
|
|
50
|
+
if resolver.respond_to?(:deprecation_reason) && resolver.deprecation_reason
|
|
51
|
+
raise ConfigurationError, "#{name}: deprecated: cannot apply, its resolver #{resolver.name || resolver.inspect} has its own " \
|
|
52
|
+
"deprecation_reason, which graphql-ruby gives the field in every version. Remove the resolver's"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
before, after = Deprecation.pair(name, deprecated, kwargs)
|
|
56
|
+
Deprecation.mark(super(*args, **before, &block), super(*args, **after, &block))
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# `argument` on a field, an input object, a resolver or a mutation: input fields are arguments.
|
|
61
|
+
module Arguments
|
|
62
|
+
def argument(*args, deprecated: nil, **kwargs, &block)
|
|
63
|
+
return super(*args, **kwargs, &block) if deprecated.nil?
|
|
64
|
+
|
|
65
|
+
name = Deprecation.name_of(args, kwargs)
|
|
66
|
+
if Deprecation.subscription?(self)
|
|
67
|
+
raise ConfigurationError, "#{name}: deprecated: cannot be used on a subscription's arguments: graphql-ruby's trigger reads " \
|
|
68
|
+
"them with no version, where two definitions raise"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
before, after = Deprecation.pair(name, deprecated, kwargs)
|
|
72
|
+
Deprecation.mark(super(*args, **before, &block), super(*args, **after, &block))
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# `value` on an enum. graphql-ruby defines one method per value name, so only the first definition gets it.
|
|
77
|
+
module Values
|
|
78
|
+
def value(*args, deprecated: nil, **kwargs, &block)
|
|
79
|
+
return super(*args, **kwargs, &block) if deprecated.nil?
|
|
80
|
+
|
|
81
|
+
before, after = Deprecation.pair(Deprecation.name_of(args, kwargs), deprecated, kwargs)
|
|
82
|
+
Deprecation.mark(super(*args, **before, &block), super(*args, **after.merge(value_method: false), &block))
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# A subscription class, or a field that a subscription class resolves. A field declared directly on the
|
|
87
|
+
# subscription root is not known as one yet: the schema check refuses its twins when the schema is used.
|
|
88
|
+
def self.subscription?(owner)
|
|
89
|
+
owner = owner.resolver if owner.is_a?(GraphQL::Schema::Field)
|
|
90
|
+
owner.is_a?(Class) && owner < GraphQL::Schema::Subscription
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Versioning
|
|
6
|
+
class Diff
|
|
7
|
+
# What `rake "graphql_armory:versions:diff[YourSchema,FROM,TO]"` runs: prints the diff and answers an exit status,
|
|
8
|
+
# 1 when something breaks.
|
|
9
|
+
class Command
|
|
10
|
+
USAGE = "Name the schema and two versions: rake \"graphql_armory:versions:diff[YourSchema,FROM,TO]\""
|
|
11
|
+
|
|
12
|
+
# The rake task's action. The rake file looks for this object among the task's actions, so an app that names the
|
|
13
|
+
# task first (to add a prerequisite) still gets it once.
|
|
14
|
+
RAKE_ACTION = proc do |_task, args|
|
|
15
|
+
Rake::Task[:environment].invoke if Rake::Task.task_defined?(:environment)
|
|
16
|
+
status = new.(args[:schema], args[:from], args[:to])
|
|
17
|
+
exit(status) unless status.zero?
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(out: $stdout, diff: Diff)
|
|
21
|
+
@out = out
|
|
22
|
+
@diff = diff
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def call(schema_name, from, to)
|
|
26
|
+
return fail_with(USAGE) if [schema_name, from, to].any? { |value| value.to_s.empty? }
|
|
27
|
+
|
|
28
|
+
result = @diff.(Object.const_get(schema_name), from: from, to: to)
|
|
29
|
+
@out.print(result)
|
|
30
|
+
result.breaking? ? 1 : 0
|
|
31
|
+
rescue ConfigurationError => error
|
|
32
|
+
fail_with(error.message)
|
|
33
|
+
rescue NameError => error
|
|
34
|
+
raise unless schema_name.to_s.split("::").include?(error.name.to_s)
|
|
35
|
+
|
|
36
|
+
fail_with("No schema named #{schema_name}")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def fail_with(message)
|
|
42
|
+
@out.puts(message)
|
|
43
|
+
1
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|