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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -1
  3. data/README.md +4 -4
  4. data/docs/authorization.md +7 -7
  5. data/docs/rate-limiting.md +1 -1
  6. data/docs/versioning.md +540 -0
  7. data/lib/graphql/armory/authorization/adapters/pundit.rb +26 -1
  8. data/lib/graphql/armory/authorization/audit/findings.rb +6 -1
  9. data/lib/graphql/armory/authorization/audit/overrides.rb +8 -2
  10. data/lib/graphql/armory/authorization/audit/payload_reuse.rb +9 -8
  11. data/lib/graphql/armory/authorization/definitions.rb +8 -92
  12. data/lib/graphql/armory/authorization/integration/object.rb +2 -2
  13. data/lib/graphql/armory/authorization/integration.rb +29 -2
  14. data/lib/graphql/armory/authorization/rule.rb +3 -3
  15. data/lib/graphql/armory/authorization.rb +3 -1
  16. data/lib/graphql/armory/definitions.rb +111 -0
  17. data/lib/graphql/armory/location.rb +101 -0
  18. data/lib/graphql/armory/rate_limits/analysis_guard.rb +9 -2
  19. data/lib/graphql/armory/version.rb +1 -1
  20. data/lib/graphql/armory/versioning/audit/command.rb +43 -0
  21. data/lib/graphql/armory/versioning/audit/own_checks.rb +139 -0
  22. data/lib/graphql/armory/versioning/audit/profiles.rb +123 -0
  23. data/lib/graphql/armory/versioning/audit/subscription_triggers.rb +98 -0
  24. data/lib/graphql/armory/versioning/audit.rb +559 -0
  25. data/lib/graphql/armory/versioning/controls.rb +403 -0
  26. data/lib/graphql/armory/versioning/decision.rb +89 -0
  27. data/lib/graphql/armory/versioning/deprecation.rb +95 -0
  28. data/lib/graphql/armory/versioning/diff/command.rb +49 -0
  29. data/lib/graphql/armory/versioning/diff.rb +349 -0
  30. data/lib/graphql/armory/versioning/dump/command.rb +46 -0
  31. data/lib/graphql/armory/versioning/dump.rb +107 -0
  32. data/lib/graphql/armory/versioning/gate.rb +140 -0
  33. data/lib/graphql/armory/versioning/integration.rb +903 -0
  34. data/lib/graphql/armory/versioning/lifecycle.rb +61 -0
  35. data/lib/graphql/armory/versioning/profiles.rb +122 -0
  36. data/lib/graphql/armory/versioning/rspec.rb +38 -0
  37. data/lib/graphql/armory/versioning/versions.rb +111 -0
  38. data/lib/graphql/armory/versioning/view.rb +61 -0
  39. data/lib/graphql/armory/versioning.rb +132 -0
  40. data/lib/graphql/armory.rb +1 -0
  41. data/lib/tasks/graphql_armory.rake +20 -0
  42. metadata +24 -2
  43. data/lib/graphql/armory/authorization/location.rb +0 -79
@@ -0,0 +1,559 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Versioning
6
+ # Checks a schema's versioning before production: every refusal a query of a served version would hit, and every
7
+ # member a served version lists outside its lifecycle. It never runs at boot or during a request.
8
+ #
9
+ # report = GraphQL::Armory::Versioning::Audit.(AppSchema)
10
+ # report.ok? # false when a query of some served version would be refused, or would see a member it should not
11
+ # puts report
12
+ # GraphQL::Armory::Versioning::Audit.(AppSchema, strict: true) # members only retired versions have fail it too
13
+ #
14
+ # Each member is asked with a context that names the version and `armory_full_schema: true`, so a check that hides it
15
+ # from a user (Authorization's `visible_when:`) never hides a refusal. Nothing it does records a signal.
16
+ class Audit
17
+ # What the report does not check yet: it says so, so `ok?` never claims more than was checked.
18
+ NOT_CHECKED = "whether a type's field or argument in a version has a type its interface's definition allows (covariance)"
19
+
20
+ # `location` is "path:line" where the app wrote what the problem is about, or nil. `severity` is nil for a problem,
21
+ # or :information for something listed that changes nothing a client relies on: `ok?` ignores it.
22
+ Problem = Struct.new(:kind, :path, :message, :location, :severity) do
23
+ def information? = severity == :information
24
+
25
+ def to_s
26
+ "#{path} (#{kind}): #{message}"
27
+ end
28
+ end
29
+
30
+ class Report
31
+ attr_reader :problems
32
+
33
+ def initialize(problems = [])
34
+ @problems = problems.freeze
35
+ freeze
36
+ end
37
+
38
+ def ok?
39
+ problems.all?(&:information?)
40
+ end
41
+
42
+ def to_s
43
+ failing, information = problems.partition { |problem| !problem.information? }
44
+ lines = if failing.empty?
45
+ ["No problems."]
46
+ else
47
+ ["#{failing.size} #{failing.size == 1 ? 'problem' : 'problems'}:", *failing.map { |problem| line(problem) }]
48
+ end
49
+ lines.push("", "Information:", *information.map { |problem| line(problem) }) unless information.empty?
50
+ [*lines, "", "Not checked yet: #{NOT_CHECKED}."].join("\n") << "\n"
51
+ end
52
+
53
+ private
54
+
55
+ def line(problem) = " #{problem.location ? "#{problem} (at #{problem.location})" : problem}"
56
+ end
57
+
58
+ # Collects what one audit finds: one problem per kind and path (per message for the schema's own), the first kept.
59
+ class Findings
60
+ def initialize
61
+ @problems = {}
62
+ end
63
+
64
+ # A problem replaces information of the same kind and path found in another version.
65
+ def problem(kind, path, message, at: nil, severity: nil)
66
+ key = path == "Schema" ? [kind, path, message] : [kind, path]
67
+ kept = @problems[key]
68
+ return kept if kept && (severity || !kept.information?)
69
+
70
+ @problems[key] = Problem.new(kind, path, message, Location.of(at), severity)
71
+ end
72
+
73
+ def information(kind, path, message, at: nil) = problem(kind, path, message, at: at, severity: :information)
74
+
75
+ # Information never stands for a problem: a member listed as information is still asked in every version.
76
+ def path?(path) = failing.any? { |problem| problem.path == path }
77
+
78
+ def any? = failing.any?
79
+
80
+ # Whether a refusal is one already reported: graphql-ruby's lookups ask a member again inside its owner's.
81
+ def explained?(message) = failing.any? { |problem| message.start_with?("#{problem.path}: ") || message == problem.message }
82
+
83
+ def failing = @problems.each_value.reject(&:information?)
84
+
85
+ def report
86
+ Report.new(@problems.values.sort_by { |problem| [problem.path, problem.kind.to_s] })
87
+ end
88
+ end
89
+
90
+ def self.call(schema, strict: false)
91
+ new.(schema, strict: strict)
92
+ end
93
+
94
+ # `strict: true` makes `:retired_only` (a member only retired versions have) a problem instead of information.
95
+ def call(schema, strict: false)
96
+ findings = Findings.new
97
+ return findings.report unless installed?(schema, findings) && visibility?(schema, findings)
98
+
99
+ refused = schema_refusals(schema, findings)
100
+ definitions = Integration.definitions(schema)
101
+ members = definitions.flat_map { |type| members_of(type) }.uniq
102
+ members.concat(schema.directives.values.flat_map(&:all_argument_definitions))
103
+ undeclared(schema, members, findings)
104
+ guarded(findings, "Schema") { OwnChecks.(schema, definitions, members, findings) }
105
+ outside_latest(members, findings)
106
+ guarded(findings, "Schema") { SubscriptionTriggers.(schema, findings) }
107
+ guarded(findings, "Schema") { Profiles.(schema, findings) }
108
+ guarded(findings, "Schema") { RetiredOnly.(schema, findings, strict: strict) }
109
+ # A schema-level refusal fails every query, before any member is asked.
110
+ schema.armory_versioning.versions.each_served { |version| walk(schema, version, definitions, members, findings) } unless refused
111
+ findings.report
112
+ end
113
+
114
+ private
115
+
116
+ def installed?(schema, findings)
117
+ return true if schema.respond_to?(:armory_versioning)
118
+
119
+ findings.problem(:plugin, "Schema", "#{schema} never installed the plugin: `visible?` of members with Base raises on " \
120
+ "every query. Add `use GraphQL::Armory::Versioning, versions: ..., default: ...`")
121
+ false
122
+ end
123
+
124
+ # Without Visibility or the Warden, graphql-ruby warns on every query that hides something, and may stop asking.
125
+ def visibility?(schema, findings)
126
+ visibility = schema.respond_to?(:use_visibility_profile?) && schema.use_visibility_profile?
127
+ return true if visibility || schema.plugins.any? { |plugin, _| plugin == GraphQL::Schema::Warden }
128
+
129
+ findings.problem(:visibility, "Schema", "neither GraphQL::Schema::Visibility nor GraphQL::Schema::Warden is used: graphql-ruby " \
130
+ "warns on every query that hides a member. Add `use GraphQL::Schema::Visibility`")
131
+ false
132
+ end
133
+
134
+ def schema_refusals(schema, findings)
135
+ checks = [-> { Integration.refuse_always_visible(schema) }, *Integration.schema_checks(schema)]
136
+ checks.count do |check|
137
+ check.()
138
+ false
139
+ rescue ConfigurationError => error
140
+ findings.problem(:refused, "Schema", error.message)
141
+ true
142
+ end.positive?
143
+ end
144
+
145
+ def members_of(type)
146
+ members = [type, *Armory::Definitions.fields_at_every_level(type), *Integration.arguments_in(type)]
147
+ members.concat(type.all_enum_value_definitions) if type.kind.enum?
148
+ members.concat(type.type_memberships) if type.kind.union?
149
+ members.concat(type.interface_type_memberships.select { |membership| membership.respond_to?(:visible?) }) if type.respond_to?(:interface_type_memberships)
150
+ members
151
+ end
152
+
153
+ # A label no version has fails the member's check whenever it is asked: reported even for a member no query asks.
154
+ def undeclared(schema, members, findings)
155
+ labels = schema.armory_versioning.versions.map(&:label)
156
+ members.each do |member|
157
+ lifecycle = Integration.lifecycle_of(member)
158
+ next if Lifecycle::EVERYWHERE.equal?(lifecycle)
159
+
160
+ lifecycle.to_h.each do |keyword, label|
161
+ next if labels.include?(label)
162
+
163
+ findings.problem(:undeclared_version, path_of(member), "#{keyword}: #{label.inspect} is not in versions: #{labels.inspect}", at: member)
164
+ end
165
+ end
166
+ end
167
+
168
+ def walk(schema, version, definitions, members, findings)
169
+ # Under `profiles: true`, the version's own profile, as its queries run: with named profiles and no `dynamic: true`,
170
+ # graphql-ruby refuses a query that names none.
171
+ profile = schema.armory_versioning_profiles.query_options(version)
172
+ query = GraphQL::Query.new(schema, "{ __typename }", validate: false, context: { armory_full_schema: true, armory_version: version.label }, **profile)
173
+ context = query.context
174
+ members.each do |member|
175
+ # graphql-ruby never asks a member whose owner the version does not have.
176
+ next if findings.path?(path_of(member)) || !owners_live?(member, version)
177
+
178
+ guarded(findings, member) { member.visible?(context) }
179
+ end
180
+ types = query.types
181
+ view = View.new(types)
182
+ # As `__type(name:)` asks it: graphql-ruby checks that the name is reachable in V first.
183
+ definitions.filter_map { |type| named(type) }.uniq.each do |name|
184
+ guarded(findings, name) { view.type(name) }
185
+ end
186
+ outside_lifecycle(view, version, findings)
187
+ directive_arguments(types, view, version, findings)
188
+ missing_members(schema, types, view, version, definitions, findings)
189
+ end
190
+
191
+ # What a version leaves out without refusing anything: members hidden because their type is, and types left with
192
+ # no field, no member or no implementer.
193
+ def missing_members(schema, types, view, version, definitions, findings)
194
+ listed = begin
195
+ view.all_types.reject { |type| Armory::Definitions.introspection?(type) }
196
+ rescue StandardError
197
+ return # reported by outside_lifecycle
198
+ end
199
+ listed.each do |type|
200
+ guarded(findings, type) do
201
+ hidden_by_type(view, type, version, findings)
202
+ empty(schema, view, type, version, definitions, findings)
203
+ end
204
+ guarded(findings, type) { served_arguments(view, type).each { |argument| default_outside(argument, version, findings) } }
205
+ end
206
+ (guarded(findings, "Schema") { types.directives } || []).each do |directive|
207
+ guarded(findings, directive) { hidden_arguments(view, directive, version, findings) }
208
+ guarded(findings, directive) { view.arguments(directive).each { |argument| default_outside(argument, version, findings) } }
209
+ end
210
+ guarded(findings, "Schema") { unlisted_abstract(view, version, definitions, listed, findings) }
211
+ listed.each do |type|
212
+ next unless type.kind.fields?
213
+
214
+ guarded(findings, type) { fallbacks(view, type, version, findings) }
215
+ guarded(findings, type) { contract_arguments(view, type, version, findings) } if type.kind.object?
216
+ end
217
+ end
218
+
219
+ # A field whose nearest definition V hides while a farther one (a parent's or an interface's) lives in V: the Warden
220
+ # serves the farther one, `Schema::Visibility` drops the name. graphql-ruby walks the ancestors in order, interfaces
221
+ # before the superclass, skipping an interface whose membership V hides.
222
+ def fallbacks(view, type, version, findings)
223
+ levels = type.ancestors.select { |ancestor| ancestor.respond_to?(:own_fields) && member_in?(type, ancestor, version) }
224
+ levels.flat_map { |level| level.own_fields.keys }.uniq.each do |name|
225
+ defining = levels.select { |level| level.own_fields.key?(name) }
226
+ next if defining.size < 2
227
+
228
+ nearest = Array(defining.first.own_fields[name])
229
+ served = view.field(type, name)
230
+ next if nearest.any? { |field| field.equal?(served) } || nearest.any? { |field| live?(field, version) }
231
+
232
+ farther = defining.drop(1).flat_map { |level| Array(level.own_fields[name]) }.select { |field| live?(field, version) }
233
+ next if farther.empty?
234
+
235
+ path = "#{type.graphql_name}.#{name}"
236
+ hidden = Integration.display_name(defining.first)
237
+ if served
238
+ findings.problem(:fallback, path, "#{label(version)} hides #{hidden}'s definition, so the Warden serves " \
239
+ "#{Integration.display_name(served.owner)}'s definition instead, with its type, " \
240
+ "arguments and resolver", at: nearest.first)
241
+ else
242
+ interface = farther.map(&:owner).find { |owner| owner.respond_to?(:kind) && owner.kind.interface? && view.visible?(owner) }
243
+ why = if interface
244
+ "which #{interface.graphql_name} declares: a schema with it is invalid"
245
+ else
246
+ "though #{Integration.display_name(farther.first.owner)}'s definition lives there: the Warden would serve that one"
247
+ end
248
+ findings.problem(:fallback, path, "#{type.graphql_name} no longer has #{name} in #{label(version)}, #{why}", at: nearest.first)
249
+ end
250
+ end
251
+ end
252
+
253
+ # An argument of an interface's field that the object's own field hides in V: the object no longer takes what the
254
+ # interface declares. Only an argument the object defines and V hides is versioning's doing.
255
+ def contract_arguments(view, type, version, findings)
256
+ view.interfaces(type).each do |interface|
257
+ view.fields(interface).each do |declared|
258
+ field = view.field(type, declared.graphql_name)
259
+ next if field.nil? || field.equal?(declared)
260
+
261
+ taken = view.arguments(field).map(&:graphql_name)
262
+ view.arguments(declared).each do |argument|
263
+ name = argument.graphql_name
264
+ next if taken.include?(name)
265
+
266
+ hidden = field.all_argument_definitions.select { |own| own.graphql_name == name && !Integration.covers?(own, version) }
267
+ next if hidden.empty?
268
+
269
+ findings.problem(:fallback, "#{type.graphql_name}.#{field.graphql_name}.#{name}",
270
+ "#{label(version)} hides it on #{type.graphql_name}.#{field.graphql_name}, though " \
271
+ "#{interface.graphql_name}.#{declared.graphql_name} takes it: a schema with it is invalid", at: hidden.first)
272
+ end
273
+ end
274
+ end
275
+ end
276
+
277
+ def served_arguments(view, type)
278
+ return view.arguments(type) if type.kind.input_object?
279
+ return [] unless type.kind.fields?
280
+
281
+ view.fields(type).flat_map { |field| view.arguments(field) }
282
+ end
283
+
284
+ # A default naming an enum value the latest version does not have: graphql-ruby's SDL printer reads defaults with no
285
+ # query, as the latest version sees them, so `to_definition` raises in every version. Any argument class.
286
+ def outside_latest(members, findings)
287
+ members.grep(GraphQL::Schema::Argument).each do |argument|
288
+ next unless argument.default_value?
289
+
290
+ guarded(findings, argument) do
291
+ missing = Integration.members_in(argument.type, argument.default_value).select do |choices|
292
+ choices.first.is_a?(GraphQL::Schema::EnumValue) && choices.none? { |value| Integration.lifecycle_of(value).covers?(Version::LATEST) }
293
+ end
294
+ unless missing.empty?
295
+ findings.problem(:default_value, argument.path, "its default_value names #{names(missing)}, which the latest version does not " \
296
+ "have: graphql-ruby prints defaults as the latest version sees them, so " \
297
+ "to_definition raises in every version. Use a default the latest version has", at: argument)
298
+ end
299
+ end
300
+ end
301
+ end
302
+
303
+ # A default naming an enum value or an input field V does not have, on an argument class without `Base` (whose own
304
+ # check refuses it): introspection of V raises, and V's SDL raises or prints what V does not have.
305
+ def default_outside(argument, version, findings)
306
+ return if !argument.default_value? || argument.is_a?(Integration::Argument)
307
+
308
+ missing = Integration.members_in(argument.type, argument.default_value).reject do |choices|
309
+ choices.any? { |member| Integration.covers?(member, version) }
310
+ end
311
+ return if missing.empty?
312
+
313
+ findings.problem(:default_value, argument.path, "its default_value names #{names(missing)}, not in #{label(version)}: introspection " \
314
+ "of #{label(version)} raises, and its SDL raises or prints a value it does not have. " \
315
+ "Use a default every version of this argument has, or include " \
316
+ "GraphQL::Armory::Versioning::Base in its argument class", at: argument)
317
+ end
318
+
319
+ def names(missing) = missing.map { |choices| choices.first.path }.join(", ")
320
+
321
+ # A field's own lifecycle, its owner's and its resolver's cover V.
322
+ # A definition whose own, owner's or resolver's lifecycle, or whose return type's, leaves the version out is not
323
+ # served there: graphql-ruby hides a field whose type it hides, and looks further for the name.
324
+ def live?(field, version)
325
+ [field, field.owner, field.resolver].compact.all? { |member| Integration.lifecycle_of(member).covers?(version) } &&
326
+ Integration.covers?(field, version)
327
+ end
328
+
329
+ def hidden_by_type(view, type, version, findings)
330
+ hidden_arguments(view, type, version, findings) if type.kind.input_object?
331
+ return unless type.kind.fields?
332
+
333
+ live_fields(type, version).each do |field|
334
+ # A twin of the same name served in its place is versioning, not a hidden field.
335
+ next if view.field(type, field.graphql_name)
336
+
337
+ hidden = hidden_type(view, field.type.unwrap, version)
338
+ findings.information(:implicit, field.path, "#{label(version)} hides it because its type #{hidden.graphql_name} is not in that version", at: field) if hidden
339
+ end
340
+ view.fields(type).each { |field| hidden_arguments(view, field, version, findings) }
341
+ end
342
+
343
+ # A required argument hidden this way lets a client of the version call its owner without it.
344
+ def hidden_arguments(view, owner, version, findings)
345
+ served = view.arguments(owner)
346
+ owner.all_argument_definitions.each do |argument|
347
+ next if served.any? { |member| member.graphql_name == argument.graphql_name } || !Integration.lifecycle_of(argument).covers?(version)
348
+
349
+ hidden = if argument.loads
350
+ argument.loads unless Integration.lifecycle_of(argument.loads).covers?(version)
351
+ else
352
+ hidden_type(view, argument.type.unwrap, version)
353
+ end
354
+ next unless hidden
355
+
356
+ why = "#{label(version)} hides it because its type #{hidden.graphql_name} is not in that version"
357
+ if argument.type.non_null? && !argument.default_value?
358
+ findings.problem(:implicit, argument.path, "required, but #{why}: a client of #{label(version)} can use " \
359
+ "#{path_of(owner)} without it", at: argument)
360
+ else
361
+ findings.information(:implicit, argument.path, why, at: argument)
362
+ end
363
+ end
364
+ end
365
+
366
+ # The type a version leaves out because of a lifecycle: its own, or, for an interface or a union, its members'.
367
+ def hidden_type(view, type, version)
368
+ return if view.visible?(type)
369
+
370
+ type if !Integration.lifecycle_of(type).covers?(version) || type.kind.abstract?
371
+ end
372
+
373
+ # The nearest definitions of each field name whose own lifecycle, owner and resolver live in the version, and, for an
374
+ # interface's field, whose membership does.
375
+ def live_fields(type, version)
376
+ Armory::Definitions.fields_at_every_level(type).select do |field|
377
+ !Armory::Definitions.shadowed?(type, field) && member_in?(type, field.owner, version) &&
378
+ [field, field.owner, field.resolver].compact.all? { |member| Integration.lifecycle_of(member).covers?(version) }
379
+ end
380
+ end
381
+
382
+ # An interface the type names with `implements` only counts where one of those memberships lives.
383
+ def member_in?(type, owner, version)
384
+ return true if owner.equal?(type) || !owner.respond_to?(:kind) || !owner.kind.interface? || !type.respond_to?(:interface_type_memberships)
385
+
386
+ named = type.ancestors.select { |ancestor| ancestor.respond_to?(:interface_type_memberships) }.flat_map(&:interface_type_memberships)
387
+ .select { |membership| membership.respond_to?(:abstract_type) && membership.abstract_type.equal?(owner) }
388
+ named.empty? || named.any? { |membership| Integration.lifecycle_of(membership).covers?(version) }
389
+ end
390
+
391
+ def empty(schema, view, type, version, definitions, findings)
392
+ if type.kind.input_object?
393
+ if type.all_argument_definitions.any? && view.arguments(type).empty?
394
+ findings.problem(:empty, path_of(type), "#{label(version)} serves it with no input field: a schema with it is invalid", at: type)
395
+ end
396
+ elsif type.kind.fields? && !type.has_no_fields? && Armory::Definitions.fields_at_every_level(type).any? && view.fields(type).empty?
397
+ findings.problem(:empty, path_of(type), "#{label(version)} serves it with no field: a client can select only " \
398
+ "__typename, and a schema with it is invalid", at: type)
399
+ end
400
+ # graphql 2.4's Schema::Visibility lists interfaces among an interface's possible types: only objects count.
401
+ return unless type.kind.abstract? && members?(type, definitions) && view.possible_types(type).none? { |member| member.kind.object? }
402
+
403
+ # Only Schema::Visibility serves an interface or a union with no member.
404
+ kind = type.kind.interface? ? :orphan_interface : :empty
405
+ findings.problem(kind, path_of(type), "#{label(version)} serves it with no #{type.kind.interface? ? 'implementer' : 'member'}: " \
406
+ "possibleTypes is empty, a fragment on any object fails, and data returned " \
407
+ "for it raises#{schema.use_visibility_profile? ? '' : ' (Warden)'}", at: type)
408
+ end
409
+
410
+ # The Warden hides an interface or a union with no member in the version, and every field that returns it.
411
+ def unlisted_abstract(view, version, definitions, listed, findings)
412
+ returned = listed.select { |type| type.kind.fields? }.flat_map { |type| live_fields(type, version) }.map { |field| field.type.unwrap }
413
+ returned.uniq.each do |type|
414
+ next unless type.kind.abstract? && !view.visible?(type) && Integration.lifecycle_of(type).covers?(version) && members?(type, definitions)
415
+
416
+ kind = type.kind.interface? ? :orphan_interface : :empty
417
+ findings.problem(kind, path_of(type), "#{label(version)} has no #{type.kind.interface? ? 'implementer' : 'member'} of it, " \
418
+ "so graphql-ruby hides it and every field that returns it", at: type)
419
+ end
420
+ end
421
+
422
+ # An interface or a union with no member in any version is not versioning's doing.
423
+ def members?(type, definitions)
424
+ return type.type_memberships.any? if type.kind.union?
425
+
426
+ definitions.any? { |definition| definition.is_a?(Class) && definition.kind.object? && definition.ancestors.include?(type) }
427
+ end
428
+
429
+ # A base interface module brought in with `include` has no name of its own.
430
+ def named(type)
431
+ type.graphql_name
432
+ rescue GraphQL::RequiredImplementationMissingError
433
+ nil
434
+ end
435
+
436
+ # The field, input object or directive that holds a member, and the type that holds that field, all live in V.
437
+ def owners_live?(member, version)
438
+ owner = member.respond_to?(:owner) ? member.owner : nil
439
+ while owner
440
+ return false unless owner.is_a?(Module) ? Integration.lifecycle_of(owner).covers?(version) : Integration.covers?(owner, version)
441
+
442
+ owner = owner.respond_to?(:owner) && !owner.is_a?(Module) ? owner.owner : nil
443
+ end
444
+ true
445
+ rescue ConfigurationError
446
+ false # an owner's label not in versions: is reported as :undeclared_version
447
+ end
448
+
449
+ # Directives are not types: their arguments are listed apart.
450
+ def directive_arguments(types, view, version, findings)
451
+ directives = guarded(findings, "Schema") { types.directives } || []
452
+ directives.each do |directive|
453
+ guarded(findings, directive) { view.arguments(directive) }&.each do |member|
454
+ outside(findings, member, version) unless covered?(member, version)
455
+ end
456
+ end
457
+ end
458
+
459
+ # A member that a version lists must live there: an app `visible?` that answers before the plugin's, or a check
460
+ # graphql-ruby leaves out, would otherwise serve it with nothing refused.
461
+ def outside_lifecycle(view, version, findings)
462
+ listed = begin
463
+ view.all_types
464
+ rescue StandardError => error
465
+ # What raised is reported above; otherwise nothing could be listed, so nothing was checked.
466
+ findings.problem(:unchecked, "Schema", "listing #{label(version)}'s types raised #{error.class}: #{error.message}") unless findings.any?
467
+ return
468
+ end
469
+ listed.each do |type|
470
+ next if Armory::Definitions.introspection?(type)
471
+
472
+ outside(findings, type, version) unless Integration.lifecycle_of(type).covers?(version)
473
+ served_members(view, type, findings).each do |member|
474
+ outside(findings, member, version) unless covered?(member, version)
475
+ end
476
+ memberships(view, type, findings).each do |abstract, object|
477
+ next if membership_covers?(abstract, object, version)
478
+
479
+ findings.problem(:outside_lifecycle, "#{object.graphql_name}.#{abstract.graphql_name}",
480
+ "#{label(version)} lists #{object.graphql_name} as a member of #{abstract.graphql_name}, outside the membership's lifecycle", at: object)
481
+ end
482
+ end
483
+ end
484
+
485
+ def served_members(view, type, findings)
486
+ guarded(findings, type) do
487
+ members = []
488
+ view.fields(type).each { |field| members << field << view.arguments(field) } if type.kind.fields?
489
+ members << view.arguments(type) if type.kind.input_object?
490
+ members << view.enum_values(type) if type.kind.enum?
491
+ members.flatten
492
+ end || []
493
+ end
494
+
495
+ def memberships(view, type, findings)
496
+ guarded(findings, type) do
497
+ if type.kind.abstract?
498
+ view.possible_types(type).map { |object| [type, object] }
499
+ elsif type.kind.object?
500
+ view.interfaces(type).map { |interface| [interface, type] }
501
+ else
502
+ []
503
+ end
504
+ end || []
505
+ end
506
+
507
+ def covered?(member, version)
508
+ return false unless Integration.covers?(member, version)
509
+
510
+ !member.is_a?(GraphQL::Schema::Field) || Integration.lifecycle_of(member.owner).covers?(version)
511
+ end
512
+
513
+ def membership_covers?(abstract, object, version)
514
+ found = abstract.kind.union? ? abstract.type_memberships : object.interface_type_memberships
515
+ found.any? do |membership|
516
+ # An inherited membership names the class that declared it.
517
+ membership.respond_to?(:abstract_type) && membership.abstract_type.equal?(abstract) && object <= membership.object_type &&
518
+ Integration.lifecycle_of(membership).covers?(version)
519
+ end
520
+ end
521
+
522
+ def outside(findings, member, version)
523
+ findings.problem(:outside_lifecycle, path_of(member), "#{label(version)} lists it outside its lifecycle " \
524
+ "(#{Integration.lifecycle_of(member).to_h.inspect})", at: member)
525
+ end
526
+
527
+ # Runs one check; whatever it raises becomes the member's problem, so one failure never hides another.
528
+ def guarded(findings, member)
529
+ yield
530
+ rescue ConfigurationError => error
531
+ findings.problem(:refused, path_of(member), without_path(error.message, member), at: member) unless findings.explained?(error.message)
532
+ nil
533
+ rescue GraphQL::Schema::DuplicateNamesError => error
534
+ findings.problem(:duplicate, path_of(member), error.message, at: member)
535
+ nil
536
+ rescue GraphQL::Schema::Enum::MissingValuesError => error
537
+ findings.problem(:empty, path_of(member), error.message, at: member)
538
+ nil
539
+ rescue StandardError => error
540
+ findings.problem(:unchecked, path_of(member), "raised #{error.class} when asked with no user: #{error.message}", at: member)
541
+ nil
542
+ end
543
+
544
+ def path_of(member)
545
+ return member if member.is_a?(String)
546
+ return member.path if member.respond_to?(:path) && !member.is_a?(Module)
547
+
548
+ Integration.display_name(member)
549
+ end
550
+
551
+ def without_path(message, member)
552
+ message.delete_prefix("#{path_of(member)}: ")
553
+ end
554
+
555
+ def label(version) = Integration.label(version)
556
+ end
557
+ end
558
+ end
559
+ end