graphql-armory 0.1.0.pre12 → 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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -1
  3. data/README.md +3 -3
  4. data/docs/versioning.md +540 -0
  5. data/lib/graphql/armory/authorization/audit/findings.rb +6 -1
  6. data/lib/graphql/armory/authorization/audit/overrides.rb +8 -2
  7. data/lib/graphql/armory/authorization/audit/payload_reuse.rb +9 -8
  8. data/lib/graphql/armory/authorization/definitions.rb +8 -92
  9. data/lib/graphql/armory/authorization.rb +3 -1
  10. data/lib/graphql/armory/definitions.rb +111 -0
  11. data/lib/graphql/armory/location.rb +101 -0
  12. data/lib/graphql/armory/version.rb +1 -1
  13. data/lib/graphql/armory/versioning/audit/command.rb +43 -0
  14. data/lib/graphql/armory/versioning/audit/own_checks.rb +139 -0
  15. data/lib/graphql/armory/versioning/audit/profiles.rb +123 -0
  16. data/lib/graphql/armory/versioning/audit/subscription_triggers.rb +98 -0
  17. data/lib/graphql/armory/versioning/audit.rb +559 -0
  18. data/lib/graphql/armory/versioning/controls.rb +403 -0
  19. data/lib/graphql/armory/versioning/decision.rb +89 -0
  20. data/lib/graphql/armory/versioning/deprecation.rb +95 -0
  21. data/lib/graphql/armory/versioning/diff/command.rb +49 -0
  22. data/lib/graphql/armory/versioning/diff.rb +349 -0
  23. data/lib/graphql/armory/versioning/dump/command.rb +46 -0
  24. data/lib/graphql/armory/versioning/dump.rb +107 -0
  25. data/lib/graphql/armory/versioning/gate.rb +140 -0
  26. data/lib/graphql/armory/versioning/integration.rb +903 -0
  27. data/lib/graphql/armory/versioning/lifecycle.rb +61 -0
  28. data/lib/graphql/armory/versioning/profiles.rb +122 -0
  29. data/lib/graphql/armory/versioning/rspec.rb +38 -0
  30. data/lib/graphql/armory/versioning/versions.rb +111 -0
  31. data/lib/graphql/armory/versioning/view.rb +61 -0
  32. data/lib/graphql/armory/versioning.rb +132 -0
  33. data/lib/graphql/armory.rb +1 -0
  34. data/lib/tasks/graphql_armory.rake +20 -0
  35. metadata +24 -2
  36. data/lib/graphql/armory/authorization/location.rb +0 -103
@@ -0,0 +1,540 @@
1
+ # API versioning
2
+
3
+ Each client gets the schema it was built against, and fields can arrive and leave without breaking older clients.
4
+
5
+ ## The problem
6
+
7
+ A public GraphQL API has clients you do not control: mobile builds that stay in the stores for months, partners who integrated once. graphql-ruby gives you one tool for change, `deprecation_reason:`, and it only labels a field:
8
+
9
+ - **Nothing tells a client which schema it is talking to.** A field you remove disappears for everyone at once, including the app build from last spring.
10
+ - **So nothing is ever removed.** The schema grows forever, or a client breaks.
11
+ - **Hand-written `visible?` methods** reading ad-hoc context flags can hide fields per client, but there is no list of what exists in which version, and every team writes its own.
12
+
13
+ ## The solution
14
+
15
+ You declare an ordered list of versions once, and each type, field, argument, input field and enum value says when it arrived and when it left. Each request names the version its client was built against, and the plugin applies it:
16
+
17
+ - **One version per query, everywhere.** The query sees that version's schema in validation, execution on both engines, introspection, "Did you mean" suggestions and `to_definition`. A type, field, argument, input field or enum value outside the version does not exist for that client, as a literal, a fragment or through a variable.
18
+ - **Older clients keep their schema** until you retire their version.
19
+ - **Refused, never re-routed.** A retired version, an unknown or future label, an empty String or anything that is not a String is refused before anything runs, with a stable code. The plugin never serves a different version than the one asked for.
20
+ - **Built on graphql-ruby's own `visible?`.** It hooks `Schema.query_class`, to decide the version once per query, and `visible?` on the base classes you give it.
21
+
22
+ Versioning is **not authorization**. Any client may ask for any served version, so a field that exists in a served older version is public to anyone who asks for that version. What a user may read is [Authorization](authorization.md)'s job.
23
+
24
+ ## How to use it
25
+
26
+ Declare the versions on the schema, oldest first, and include `Base` in your base classes:
27
+
28
+ ```ruby
29
+ class AppSchema < GraphQL::Schema
30
+ query Types::Query
31
+ use GraphQL::Schema::Visibility # or GraphQL::Schema::Warden to stay on the legacy path
32
+ use GraphQL::Armory::Versioning,
33
+ versions: %w[2024-01-15 2024-06-01 2025-01-01], # oldest first: the order is the order
34
+ retired: %w[2024-01-15], # no longer served
35
+ default: :latest # what a request with no version gets
36
+ end
37
+
38
+ class Types::BaseArgument < GraphQL::Schema::Argument
39
+ include GraphQL::Armory::Versioning::Base
40
+ end
41
+
42
+ class Types::BaseField < GraphQL::Schema::Field
43
+ include GraphQL::Armory::Versioning::Base
44
+ argument_class Types::BaseArgument
45
+ end
46
+
47
+ class Types::BaseEnumValue < GraphQL::Schema::EnumValue
48
+ include GraphQL::Armory::Versioning::Base
49
+ end
50
+
51
+ class Types::BaseObject < GraphQL::Schema::Object
52
+ include GraphQL::Armory::Versioning::Base
53
+ field_class Types::BaseField
54
+ end
55
+
56
+ class Types::BaseTypeMembership < GraphQL::Schema::TypeMembership
57
+ include GraphQL::Armory::Versioning::Base # for implements X, added: and possible_types Y, added:
58
+ end
59
+
60
+ module Types::BaseInterface
61
+ include GraphQL::Schema::Interface # first, then Base
62
+ include GraphQL::Armory::Versioning::Base
63
+ field_class Types::BaseField
64
+ type_membership_class Types::BaseTypeMembership
65
+ end
66
+
67
+ class Types::BaseInputObject < GraphQL::Schema::InputObject
68
+ include GraphQL::Armory::Versioning::Base
69
+ argument_class Types::BaseArgument # input fields are arguments
70
+ end
71
+
72
+ class Types::BaseEnum < GraphQL::Schema::Enum
73
+ include GraphQL::Armory::Versioning::Base
74
+ enum_value_class Types::BaseEnumValue
75
+ end
76
+
77
+ class Types::BaseUnion < GraphQL::Schema::Union
78
+ include GraphQL::Armory::Versioning::Base
79
+ type_membership_class Types::BaseTypeMembership
80
+ end
81
+
82
+ class Types::BaseScalar < GraphQL::Schema::Scalar; include GraphQL::Armory::Versioning::Base; end
83
+ class Types::BaseMutation < GraphQL::Schema::RelayClassicMutation
84
+ include GraphQL::Armory::Versioning::Base
85
+ argument_class Types::BaseArgument
86
+ field_class Types::BaseField
87
+ object_class Types::BaseObject
88
+ input_object_class Types::BaseInputObject
89
+ end
90
+ ```
91
+
92
+ Set `argument_class Types::BaseArgument` wherever arguments are declared: base fields, input objects, resolvers, mutations and your own directives.
93
+
94
+ Then give members a lifecycle:
95
+
96
+ ```ruby
97
+ class Types::Invoice < Types::BaseObject
98
+ field :number, String, null: false # every version
99
+ field :total, Integer, null: false, added: "2024-06-01" # from 2024-06-01 on
100
+ field :amount, Integer, null: false, removed: "2025-01-01", # up to 2024-06-01
101
+ deprecation_reason: "Use `total`"
102
+ field :tags, [String], null: false, added: "2025-01-01" do
103
+ argument :first, Integer, required: false
104
+ argument :page, Integer, required: false, removed: "2025-01-01" # an argument, up to 2024-06-01
105
+ end
106
+ end
107
+
108
+ class Types::Status < Types::BaseEnum
109
+ value "PAID"
110
+ value "VOID", added: "2024-06-01" # an enum value, from 2024-06-01 on
111
+ end
112
+
113
+ class Types::InvoiceFilter < Types::BaseInputObject
114
+ argument :status, Types::Status, required: false
115
+ argument :since, String, required: false, added: "2025-01-01" # an input field, from 2025-01-01 on
116
+ end
117
+ ```
118
+
119
+ A member lives from `added:` (the first version when absent) up to, not including, `removed:` (still here when absent). A member with neither lives in every version. Every field and enum value of a class that includes `Base` still reads the query's version when graphql-ruby asks whether it is visible; an argument reads it only when it, its default value or the type it `loads:` is versioned. `deprecation_reason:` is graphql-ruby's own and works as always: the member is deprecated in every version it exists in. To deprecate it from a given version on, use `deprecated:` (see "Deprecating").
120
+
121
+ A versioned argument is the client's to omit in the versions it does not live in, so give its resolver keyword a Ruby default (`def tags(first: 10)`): graphql-ruby never applies the `default_value:` of an argument hidden in the query's version, and calls the resolver without it.
122
+
123
+ Types take `lifecycle` in their class body:
124
+
125
+ ```ruby
126
+ class Types::Contract < Types::BaseObject
127
+ lifecycle added: "2025-01-01" # the type, and every field and argument that uses it
128
+ field :party, String, null: false
129
+ end
130
+
131
+ class Types::CreateContract < Types::BaseMutation
132
+ lifecycle added: "2025-01-01" # the mutation, its field, its input and payload types
133
+ end
134
+ ```
135
+
136
+ A type outside the version is gone with everything that uses it: graphql-ruby hides each field that returns it and each argument that takes it, and the plugin also hides an argument that `loads:` it. An argument that `loads:` an interface or union still lives, but an object of a member type outside the version is not authorized there: graphql-ruby's unauthorized path runs (`unauthorized_object`), never the resolver. A field leaves with the type that declared it, so an interface that includes a versioned interface loses that interface's fields where it does not live. A subclass keeps its parent's lifecycle unless it declares its own, which must fit inside every versioned ancestor's; a mutation's generated input and payload types take the mutation's; an object never takes its interfaces', and an interface never takes the interfaces it includes. `lifecycle` needs `added:`, `removed:` or both.
137
+
138
+ A membership takes the same keywords when an existing type joins an interface or a union:
139
+
140
+ ```ruby
141
+ class Types::Invoice < Types::BaseObject
142
+ implements Types::Archivable, added: "2025-01-01" # an Archivable, with its fields, from 2025-01-01
143
+ end
144
+
145
+ class Types::Document < Types::BaseUnion
146
+ possible_types Types::Invoice
147
+ possible_types Types::Contract, added: "2025-01-01"
148
+ end
149
+ ```
150
+
151
+ Outside the membership, the object is not a member: the interface's fields are gone from it (a field the object declares itself stays), `interfaces` and `possibleTypes` omit it, a fragment on the abstract type does not apply to it, and a `resolve_type` answering it raises `GraphQL::UnresolvedTypeError` (see "Data outside the version"). The membership only hides the relationship: the object stays in the schema, so a type new with the membership takes its own `lifecycle` too. A membership lifecycle wider than its types' changes nothing outside theirs. A type new to a union or interface altogether simply takes `lifecycle added:`.
152
+
153
+ To change a field, argument or enum value's shape without renaming it, give it two definitions on the same owner, with lifecycles that do not overlap:
154
+
155
+ ```ruby
156
+ field :total, Integer, null: false, method: :total_cents, removed: "2025-01-01" # an Int up to 2024-06-01
157
+ field :total, String, null: false, method: :total_text, added: "2025-01-01" # a String from 2025-01-01
158
+
159
+ argument :first, Integer, required: false, removed: "2025-01-01"
160
+ argument :first, String, required: false, added: "2025-01-01"
161
+
162
+ value "VOID", "Cancelled.", removed: "2025-01-01"
163
+ value "VOID", "Voided by the issuer.", value_method: false, added: "2025-01-01" # the second skips graphql-ruby's value method
164
+ ```
165
+
166
+ Each version sees exactly one of them, in every place graphql-ruby reads the schema. Two definitions that one version would both see raise `ConfigurationError` (an unversioned one beside a versioned one included). [Authorization](authorization.md)'s audit reads them as one member, and each keeps its own rule. Two definitions may also be kept apart by their types' lifecycles. Field twins need graphql 2.4.9 or later, and `value_method: false` on an enum value graphql 2.4.10 or later.
167
+
168
+ A type changes shape the same way: two classes with one `graphql_name` and lifecycles that do not overlap, each returned by its own field twin:
169
+
170
+ ```ruby
171
+ class Types::MoneyV1 < Types::BaseObject
172
+ graphql_name "Money"
173
+ lifecycle removed: "2025-01-01"
174
+ field :cents, Integer, null: false
175
+ end
176
+
177
+ class Types::MoneyV2 < Types::BaseObject
178
+ graphql_name "Money"
179
+ lifecycle added: "2025-01-01"
180
+ field :amount, String, null: false
181
+ field :currency, String, null: false
182
+ end
183
+
184
+ field :total, Types::MoneyV1, null: false, removed: "2025-01-01"
185
+ field :total, Types::MoneyV2, null: false, added: "2025-01-01"
186
+ ```
187
+
188
+ Each version sees one `Money`. Reference each twin by its class: a `GraphQL::Schema::LateBoundType` naming a type with twins raises `ConfigurationError`, since graphql-ruby binds the name to the first class registered with it in every version. Give both twins a common base without a lifecycle; never subclass one twin from the other (the subclass would be refused as wider than its parent). A `resolve_type` answers the twin of the query's version (see "Data outside the version"), and Authorization's audit judges each twin's rule.
189
+
190
+ Use `GraphQL::Schema::Visibility` (or `GraphQL::Schema::Warden`): without either, graphql-ruby prints a deprecation warning the first time a query hides something.
191
+
192
+ ### Deprecating
193
+
194
+ `deprecated:` with `deprecation_reason:` deprecates a member from one version on. Clients of earlier versions see it as it was, with no `@deprecated`; clients of that version and later ones see the reason in introspection, in the SDL, and in `includeDeprecated: false` lists, which leave it out. It works on fields (of objects, interfaces and mutation payloads), arguments (of fields, resolvers and mutations), input fields and enum values, and combines with `added:` and `removed:`:
195
+
196
+ ```ruby
197
+ class Types::Invoice < Types::BaseObject
198
+ field :amount, Integer, null: false, deprecated: "2024-06-01", deprecation_reason: "Use `total`",
199
+ removed: "2025-01-01" # plain up to 2024-06-01, deprecated in 2024-06-01, gone after
200
+ field :tags, [String], null: false do
201
+ argument :page, Integer, required: false, deprecated: "2025-01-01", deprecation_reason: "Use `after`"
202
+ end
203
+ end
204
+
205
+ class Types::Status < Types::BaseEnum
206
+ value "CANCELLED", deprecated: "2025-01-01", deprecation_reason: "Use `VOID`"
207
+ end
208
+ ```
209
+
210
+ graphql-ruby's `deprecation_reason` is the same in every version a definition lives in, so `deprecated: D` is written for you as the two definitions of the section above: one living up to `D` with no reason, and one from `D` with it. The `amount` field above is exactly:
211
+
212
+ ```ruby
213
+ field :amount, Integer, null: false, removed: "2024-06-01"
214
+ field :amount, Integer, null: false, added: "2024-06-01", removed: "2025-01-01", deprecation_reason: "Use `total`"
215
+ ```
216
+
217
+ So everything said about two definitions applies: [Authorization](authorization.md)'s audit reads them as one member (a problem with both is listed once), each keeps the field's keywords (its rule, its `resolver:`), lookups with no query raise graphql-ruby's `DuplicateNamesError` on the name (see "Guarantees and limits"), and field definitions need graphql 2.4.9 or later, enum values 2.4.10 or later. A field's block runs once per definition, so the arguments it declares, with their own lifecycles, belong to both. An enum value's Ruby method (`Types::Status.cancelled`) is defined once, by the first definition.
218
+
219
+ The class that defines the member writes the two definitions, so `deprecated:` needs `Base` in that class (the object, interface, input object, enum, resolver or mutation, or the field class for a field's arguments) as well as in the member's class. It is refused on a subscription's arguments: graphql-ruby's trigger reads them with no version, where two definitions raise. It is refused on `implements` and `possible_types` too, since GraphQL cannot deprecate a membership: use `removed:`.
220
+
221
+ To test against it, `Versioning::Controls::Deprecations::Query.example(Bases.example)` is a query type with `deprecated:` on each member kind, deprecated from `2025-01-01`, and `Deprecations::Root.example` its root object.
222
+
223
+ ### Labels
224
+
225
+ Labels are opaque Strings, ordered by their position in `versions:`. Calendar dates (`YYYY-MM-DD`) are the recommended convention, as public APIs such as Stripe's and GitHub's use, and when every label looks like a date the list must be ascending. Any other labels work too (`%w[1 2 3]`): their order is the order you wrote.
226
+
227
+ A version stays in `versions:` while any field names it. Retiring a version moves it to `retired:`, which must be the oldest versions and must leave at least one served.
228
+
229
+ ### Selecting a version
230
+
231
+ Your app puts the version in the context, from wherever your protocol carries it. One line in the controller, from a header:
232
+
233
+ ```ruby
234
+ AppSchema.execute(params[:query], variables: variables, context: {
235
+ current_user: current_user,
236
+ armory_version: request.headers["X-API-Version"], # nil is fine: default: applies
237
+ })
238
+ ```
239
+
240
+ It could as well come from the URL or from a version pinned on the client's account. What the plugin does with it:
241
+
242
+ | `context[:armory_version]` | Result | `extensions.code` |
243
+ |---|---|---|
244
+ | A served version's label | That version | — |
245
+ | `nil`, or no key (the key is the Symbol `:armory_version`: a String key `"armory_version"` counts as no key) | The `default:` policy | `MISSING_API_VERSION` under `default: :refuse` |
246
+ | A retired version's label | Refused, nothing runs | `RETIRED_API_VERSION` |
247
+ | Anything else: an unknown or future label, `""`, a label with extra spaces, a Symbol, a number | Refused, nothing runs | `UNKNOWN_API_VERSION` |
248
+
249
+ `default:` is required, because the plugin must not guess:
250
+
251
+ - `:latest` for a new API: clients that send nothing follow the schema forward.
252
+ - A label (`"2024-06-01"`) for an API that already has clients in production: they keep the schema they see today.
253
+ - `:refuse` once every client sends a version (watch `:version_defaulted` reach zero first).
254
+
255
+ A version set by a custom `context_class` counts too: the plugin decides after the query and its context are built.
256
+
257
+ ### Telling a refusal apart
258
+
259
+ A refusal is the request's only error, with a stable code in `extensions.code`: `UNKNOWN_API_VERSION`, `RETIRED_API_VERSION` or `MISSING_API_VERSION`. Match on the code. Messages are fixed ("Unknown API version", "This API version is retired", "Send an API version"): they never repeat what the client sent and never list your versions. If you want clients to discover the served versions, add a root field of your own that lists them.
260
+
261
+ ### Reading the version in your code
262
+
263
+ `GraphQL::Armory::Versioning.version(context)` answers the query's `Version`: its `label`, its `index`, and comparisons with another version or a label.
264
+
265
+ ```ruby
266
+ def notes
267
+ version = GraphQL::Armory::Versioning.version(context)
268
+ version < "2025-01-01" ? object.legacy_notes : object.notes
269
+ end
270
+ ```
271
+
272
+ The plugin decides the schema, never the data: when the data a client gets must differ per version, your resolvers decide it from this. Comparing with a label your list does not hold raises `UnknownVersionError`, so a typo fails loudly. It raises `ConfigurationError` for anything that is not a query's context.
273
+
274
+ ### Dumping a version's schema
275
+
276
+ ```ruby
277
+ AppSchema.to_definition(context: { armory_version: "2024-06-01" })
278
+ ```
279
+
280
+ For tooling, `GraphQL::Armory::Versioning.types(AppSchema, "2024-06-01")` answers that version's schema: `type(name)`, `all_types`, `fields(type)`, `field(type, name)`, `arguments(field_or_input)`, `possible_types(type)`, `interfaces(type)`, `enum_values(enum)`, and `visible?(type)`. A type answers only when that version lists it, and a member only when its owner does. Pass `context: { current_user: ... }` when other `visible?` methods (such as Authorization's `visible_when:`) need it. It records no signal, and raises the refusal for a version that is not served. The objects it answers are graphql-ruby's own: calling `type.fields` on one, without a context, sees every version.
281
+
282
+ Without `armory_version:`, the `default:` policy applies: `:latest` dumps the latest version, and `:refuse` makes `to_definition` raise `MissingVersionError`, telling you the context to pass. `context: { armory_full_schema: true }` (Authorization's switch for a full dump) does not widen a version: there is no schema that holds every version at once. `Schema.validate(query, context: { armory_version: ... })` validates against that version the same way.
283
+
284
+ ### Watching the rollout
285
+
286
+ Pass `sinks:` to see what the plugin decided, one signal per query:
287
+
288
+ | Signal | Data | When |
289
+ |---|---|---|
290
+ | `:version_served` | `version` | The request named a served version |
291
+ | `:version_defaulted` | `version` | The request named none, and `default:` picked one |
292
+ | `:version_refused` | `reason`: `:unknown`, `:retired` or `:missing` | The request was refused |
293
+
294
+ `version` is the schema's own label, never the client's value. `:version_defaulted` counts the clients that do not send a version yet; `:version_refused` with `:retired` counts the clients still on a version you retired. See [Telemetry](telemetry.md).
295
+
296
+ ### One visibility profile per version
297
+
298
+ By default graphql-ruby asks every member's `visible?` again for each query. With `profiles: true`, each served version gets one named `Schema::Visibility` profile, computed once and kept for the life of the schema, and each query runs under its own version's profile (graphql 2.4.10 or later):
299
+
300
+ ```ruby
301
+ class AppSchema < GraphQL::Schema
302
+ query Types::Query
303
+ use GraphQL::Armory::Versioning, versions: %w[2024-06-01 2025-01-01], default: :latest, profiles: true
304
+ use GraphQL::Schema::Visibility, profiles: GraphQL::Armory::Versioning.profiles(self), preload: true
305
+ end
306
+ ```
307
+
308
+ - `use GraphQL::Armory::Versioning` comes first: `Versioning.profiles(self)` builds the profiles from its own version list, one per served version, named `:"armory_v_<label>"`, so the two lists cannot drift. A retired version has no profile.
309
+ - The plugin names the profile for each query from its `armory_version`, before graphql-ruby builds it. Your code never sets `visibility_profile`: a query whose context or `visibility_profile:` names another profile, even another version's, raises `ConfigurationError` before anything runs, and so does a version a custom `context_class` changes after the query was built. A refused version runs under no profile and is refused as usual.
310
+ - With `preload: true` (graphql-ruby's default in production) every version's visibility is computed when the schema loads, so a lifecycle naming a label `versions:` does not hold raises there, not on the first request.
311
+ - `dynamic: true` is optional: it keeps working a `GraphQL::Query` your code builds by hand without a version's profile. Do not add profiles of your own: a profile Hash that did not come from `Versioning.profiles` raises `ConfigurationError` when a versioned member is asked under it.
312
+ - **A profile holds only the version, never a user.** Authorization's `visible_when:` raises under it, and your own `visible?` is asked once per version, not once per query, so it must not read the user. Use `profiles: true` on schemas whose visibility depends on the version alone. The audit reports both (`:profiles`).
313
+
314
+ Without `profiles: true`, a query that runs under a named profile of your own raises `ConfigurationError`: a named profile keeps one visibility for every query that names it, so it cannot follow each query's version.
315
+
316
+ ### The audit
317
+
318
+ Most mistakes in a versioned schema are refused with `ConfigurationError`, but only when a query first asks the member, which may be in production. The audit asks every member of every served version before that:
319
+
320
+ ```ruby
321
+ report = GraphQL::Armory::Versioning::Audit.(AppSchema)
322
+ report.ok?
323
+ puts report
324
+ ```
325
+
326
+ ```ruby
327
+ # spec/graphql/versions_spec.rb
328
+ require "graphql/armory/versioning/rspec"
329
+
330
+ RSpec.describe AppSchema do
331
+ it { is_expected.to have_consistent_versions }
332
+ end
333
+ ```
334
+
335
+ ```sh
336
+ bin/rake "graphql_armory:versions:audit[AppSchema]" # prints the report; exit 1 on problems
337
+ ```
338
+
339
+ `Audit.(AppSchema, strict: true)` also fails on members that live only in retired versions (`:retired_only`, information otherwise). Under `profiles: true` the audit asks each version under its own profile, as its queries run.
340
+
341
+ It asks each member with a context that names the version and `armory_full_schema: true`, so Authorization's `visible_when:` never hides a refusal from it; it records no signal. What it reports:
342
+
343
+ | Kind | What it means |
344
+ |---|---|
345
+ | `:plugin` | The schema never installed `use GraphQL::Armory::Versioning` |
346
+ | `:visibility` | Neither `Schema::Visibility` nor `Schema::Warden` is used |
347
+ | `:refused` | A query of a served version would be refused: under `Schema` for the schema's own refusals (a root type or an `extra_types` type with a lifecycle, `AlwaysVisible`, a late-bound name with twins, an abstract load, subscription twins), otherwise at the member that refuses (overlapping twins, a default naming a member outside the version, shadowing, ...) |
348
+ | `:duplicate` | Two definitions of one type name that one version both sees (type twins whose lifecycles overlap) |
349
+ | `:empty` | A served version has an enum with no value, an object, interface or input object with no field (a client can select only `__typename`; a type with `has_no_fields(true)` is not reported), or a union with no member |
350
+ | `:orphan_interface` | A served version has no object implementing an interface. The Warden hides the interface and every field that returns it; `Schema::Visibility` serves it with an empty `possibleTypes`, so a fragment on any object fails, and data returned for it raises |
351
+ | `:implicit` | A served version hides a required argument or input field because its type is outside the version (for `loads:`, the loaded type): a client of that version can call the field without it, and the resolver runs with no value. An optional member or a field hidden this way is listed as information, and so is an argument with `required: :nullable` |
352
+ | `:fallback` | A served version hides a type's own definition of a field while a parent class's or an interface's definition of that name lives there. The legacy Warden serves that other definition, with its type, arguments and resolver; `Schema::Visibility` drops the field, and when an interface declares it the type no longer implements the interface. Also reported: an argument the type's field hides in a version while the interface's field takes it |
353
+ | `:own_check` | Your own `visible?` answers before the plugin's: on a type, resolver or mutation class, a field, argument, enum value or membership class, or the schema's `self.visible?`. If it does not call `super`, it serves members outside their lifecycle (on the schema, nothing is versioned). Declare each one you keep in `own_checks:` with a reason: it is then listed as information. A `def self.visible?` on one member (inside a field's block) is reported at that member and cannot be declared. A class named in `own_checks:` with no such method is reported too |
354
+ | `:default_value` | A `default_value:` names an enum value the latest version does not have (graphql-ruby prints defaults as the latest version sees them, so `to_definition` raises in every version), or, on an argument class without `Base`, an enum value or input field a served version does not have (that version's introspection raises, and its SDL raises or prints what the version does not have) |
355
+ | `:subscription_trigger` | A subscription field's argument or nested input field is in a served version and not in the latest, or the reverse. graphql-ruby's `trigger` fills in the default of every argument whatever its version: with a default, a trigger naming the version without it raises and a trigger naming another version never reaches that version's subscribers; without one, a trigger that passes it raises unless its context names a version that has it. Listed as information: a subscription field the `default:` version does not have, and `default: :refuse`, since a trigger that names no version raises |
356
+ | `:undeclared_version` | `added:` or `removed:` names a label that is not in `versions:` |
357
+ | `:outside_lifecycle` | A served version lists a type, field, argument, enum value or membership outside its lifecycle: your own `visible?` answers before the plugin's, or graphql-ruby serves it without asking. A client of that version gets it |
358
+ | `:unchecked` | Asking the member raised something else (an app `visible?` that needs a user, for example): it was not checked |
359
+ | `:profiles` | `profiles: true` meets a check that needs the query's user: a `visible_when:` (it raises under a profile), or your own `visible?` on a type, field, argument, enum value or membership class, or on the schema (asked once per version, so it decides for every user at once; the audit cannot read whether it reads the user). Drop one of the two. A `visible?` you declare in `own_checks:` is not reported here: the declaration is your word that it is safe, and it is listed as `:own_check` information |
360
+ | `:retired_only` | A type, field, argument, enum value or membership (one of two twins included) whose lifecycle covers only retired versions: no client can reach it. Delete it. Information, which `ok?` ignores, unless `strict: true` |
361
+
362
+ `ok?` means: no query of a served version is refused, no served version lists a member outside its lifecycle, and none is left with a required argument it cannot see, a type with nothing in it, or a field or argument that another definition of its name serves or that an interface still declares; no `visible?` of yours answers before the plugin's undeclared; no default names a value or input field its version does not have; and no subscription trigger breaks on an argument that differs between versions; under `profiles: true`, no check needs the query's user. Information (an optional member hidden because its type is, the natural way to version a field with its type, a declared `visible?`, a subscription field outside the `default:` version, a member only retired versions have) is printed under "Information:" and does not fail `ok?`. It does not check yet, and the report says so: whether a type's field or argument in a version has a type its interface's definition allows (covariance). A schema-level refusal fails every query, so the audit reports it and asks no member; each schema check reports its first refusal.
363
+
364
+ ### Evolving a version
365
+
366
+ A change reaches clients in four steps. Each one is a new label at the end of `versions:` (or a lifecycle that names one you already have), and older clients keep what they had until their version is retired:
367
+
368
+ 1. **Add.** Give the new member `added: "2025-01-01"`. Older versions never see it.
369
+ 2. **Deprecate.** `deprecation_reason:` labels the member in every version it lives in; `deprecated:` labels it from one version on (see [Deprecating](#deprecating)). Clients still get it.
370
+ 3. **Remove.** Give it `removed: "2025-06-01"`: clients of the versions before keep it, and only a client that moves to 2025-06-01 loses it. To change a member's shape instead, give it a twin (see "How to use it").
371
+ 4. **Retire.** Once no client asks for the oldest versions (`:version_served` for them reaches zero), move them to `retired:`. A member whose `removed:` is the oldest served version or earlier is now served nowhere: delete it, and delete a label from `versions:` once nothing names it. The audit lists each such member (`:retired_only`).
372
+
373
+ Two tools show what each step changes. The dump writes each served version's SDL to a file, so a pull request shows every version's schema change line by line:
374
+
375
+ ```sh
376
+ bin/rake "graphql_armory:versions:dump[AppSchema,schema]" # schema/2024-06-01.graphql, schema/2025-01-01.graphql, schema/latest.graphql
377
+ ```
378
+
379
+ ```ruby
380
+ GraphQL::Armory::Versioning::Dump.(AppSchema, "schema") # the paths it wrote
381
+ ```
382
+
383
+ Commit the directory, and in CI run the dump and then `git diff --exit-code schema/` to fail a build whose dumps are out of date. Each file is `to_definition(context: { armory_version: label, armory_full_schema: true })`, so Authorization's `visible_when:` hides nothing from it; `latest.graphql` is the latest version's. Retired versions are not written, and the file of a version you retire stays until you delete it. Every SDL is printed before the first file is written. A label becomes a file name, so the dump refuses (`ConfigurationError`, writing nothing) a label with a slash, a backslash or a NUL, one that starts with a dot, `latest` in any case, and two labels that differ only by case.
384
+
385
+ The diff says what changes between two served versions for a client built against the first:
386
+
387
+ ```ruby
388
+ result = GraphQL::Armory::Versioning::Diff.(AppSchema, from: "2024-06-01", to: "2025-01-01")
389
+ result.breaking? # true when a client of 2024-06-01 can break on 2025-01-01
390
+ puts result
391
+ ```
392
+
393
+ ```sh
394
+ bin/rake "graphql_armory:versions:diff[AppSchema,2024-06-01,2025-01-01]" # prints it; exit 1 when something breaks
395
+ ```
396
+
397
+ ```ruby
398
+ # spec/graphql/versions_spec.rb, for a version line that should only grow
399
+ require "graphql/armory/versioning/rspec"
400
+
401
+ RSpec.describe AppSchema do
402
+ it { is_expected.to have_no_breaking_changes(from: "2024-06-01", to: "2025-01-01") }
403
+ end
404
+ ```
405
+
406
+ ```
407
+ From 2024-06-01 to 2025-01-01:
408
+
409
+ Breaking (2):
410
+ Invoice.amount: field removed
411
+ Query.invoices.limit: type changed from Int to Int!
412
+
413
+ Dangerous (1):
414
+ Status.VOID: value added
415
+
416
+ Safe (1):
417
+ Invoice.tags: field added
418
+ ```
419
+
420
+ | Bucket | What it lists |
421
+ |---|---|
422
+ | Breaking: a query that worked against `from` can fail against `to` | A type, field, argument, input field, enum value or union member removed; a required argument or input field added (non-null with no default), or a default removed from a non-null one; a type changed (see nullability below); an interface no longer implemented; a type's kind changed (a scalar that became an object) |
423
+ | Dangerous: queries still validate, but a client may meet something it does not expect | An enum value, union member or implemented interface added; a deprecation added; a default value added, changed or removed; an output made non-null |
424
+ | Safe | A type, field, optional argument or optional input field added; an input made nullable; a deprecation removed or its reason changed; a description changed |
425
+
426
+ Nullability follows the GraphQL spec's rules for each direction. An output that becomes nullable (`String!` to `String`) is breaking: the client gets a `null` it never handled. An output that becomes non-null (`String` to `String!`) is dangerous: two fields with one response name must have the same shape to merge, so a query selecting it beside a nullable field of the same name on another type stops validating. An input that becomes non-null (`Int` to `Int!`) is breaking; one that becomes nullable (`Int!` to `Int`) is safe, since a `Int!` variable is still allowed where `Int` is expected. The same rules apply inside lists.
427
+
428
+ Types are compared by name, as a client sees them. A renamed type is a type removed, a type added, and each field or argument that used it changed, all breaking for the old name's clients. Type twins with one name are compared field by field, and field, argument and enum value twins by their type, default and deprecation, so a twin that changes nothing a client sees lists nothing. Each version is read through `Versioning.types` with `armory_full_schema: true`; pass `context:` when your own `visible?` methods need a user. Directives are not compared. A built-in scalar (`Int`, `Float`, ...) that no member of the newer version uses is listed as a type removed, since a query declaring a variable of that type would no longer validate. Two definitions of one name that one version both sees raise graphql-ruby's `DuplicateNamesError`, as in a query: run the audit first.
429
+
430
+ `Versioning::Controls::Evolution.example(visibility:)` is a schema with one change of each kind from 2024-06-01 to 2025-01-01, and `Evolution::BREAKING`, `DANGEROUS` and `SAFE` list what the diff answers for it.
431
+
432
+ ### Testing with versioning
433
+
434
+ `Versioning::Controls` holds a small schema to test against: `Controls::Versions.example` (`2024-01-15` retired, `2024-06-01` and `2025-01-01` served), `Controls::Schema.example(visibility: :visibility | :warden, default:, base:, sinks:)` with a field of each lifecycle, `Controls::Root.example(resolved)` that records what resolved, and `Controls::Context.example(version:)`. For the other members: `Controls::Argument.example` and `Controls::EnumValue.example` (base classes with `Base`), `Controls::Status.example` and `Controls::InvoiceFilter.example` (an enum and an input object with a value or field of each lifecycle), `Controls::MembersQuery.example` (`invoices(filter:, first:, page:)` and `status`) and `Controls::MembersRoot.example(resolved, status:)`. The decision itself is `Versioning::Gate`: `Gate.build(versions:, default:, retired:)` takes `use`'s settings, `gate.(context)` answers a `Decision` and records its signal, and `gate.resolve(label)` answers it recording nothing. `Gate.new`, with nothing wired, refuses every request. `Gate::Substitute` answers the decision you tell it (`answer(decision)`) and records the contexts and labels it was asked about.
435
+
436
+ ## Reference
437
+
438
+ `use GraphQL::Armory::Versioning, versions:, default:, retired: [], sinks: [], on_sink_error: nil, own_checks: {}, profiles: false`
439
+
440
+ - `versions:` an Array of non-empty String labels, oldest first, with no duplicate; ascending when every label is a date (`YYYY-MM-DD`).
441
+ - `retired:` an Array: the oldest labels of `versions:`, leaving at least one served.
442
+ - `default:` `:latest`, a served label, or `:refuse`. Required.
443
+ - `sinks:` an Array of callables taking `(signal, data)` or `(signal, data, context)`; `on_sink_error:` a callable `(error, signal)`. See [Telemetry](telemetry.md).
444
+ - `own_checks:` a Hash mapping each class or module that defines a `visible?` of yours ahead of the plugin's to `{ visible?: "the reason you keep it" }`, for the audit (`:own_check`). Nothing else is accepted: `{ Types::BaseObject => { visible?: "hides drafts from guests, calls super" } }`. A schema's own `self.visible?` is declared under the schema class.
445
+ - `profiles:` `true` or `false` (the default): run each query under its version's named `Schema::Visibility` profile. `true` raises `ArgumentError` on graphql before 2.4.10. `GraphQL::Armory::Versioning.profiles(schema)` answers the Hash for `use GraphQL::Schema::Visibility, profiles:` (a new one each call, `{ :"armory_v_<label>" => { armory_version:, armory_versions: } }` per served version), and raises `ConfigurationError` on a schema whose `use` did not say `profiles: true`. Under it, these raise `ConfigurationError` when a query is built, before anything runs: a context or `visibility_profile:` naming another profile, a version changed after the profile was picked, a profile that is not the version's (the schema's `Schema::Visibility` lacks `Versioning.profiles`), and a schema without `Schema::Visibility`. A query your code builds with `use_visibility_profile: false` runs under the Warden, still versioned. `schema.armory_versioning_profiles.enabled?` tells whether it is on.
446
+ - Anything else raises `Versioning::ConfigurationError` when the schema loads.
447
+ - Works with your own `query_class`, set before `use`, after it, or in a child schema: the plugin subclasses it. `use` it once per schema hierarchy: a second `use`, on the same schema or a child, raises.
448
+
449
+ `include GraphQL::Armory::Versioning::Base` goes in a field, argument or enum value class (a subclass of `GraphQL::Schema::Field`, `Argument` or `EnumValue`); anywhere else it raises `ArgumentError`. It adds `added:` and `removed:` to `field`, `argument` and `value`, each a non-empty String: anything else, or the same label for both, raises `ConfigurationError` when the member is defined. A label `versions:` does not hold, or a `removed:` before its `added:`, raises `ConfigurationError`, naming the member, every time a query asks about it: types load before `use`, so it cannot be checked earlier. `member.armory_lifecycle.to_h` answers `{ added:, removed: }`.
450
+
451
+ `deprecated:` (a non-empty String naming a version) goes with `deprecation_reason:` on `field` in an object, interface, resolver or mutation class, on `argument` in a field, input object, resolver or mutation class, and on `value` in an enum class, when that class includes `Base`. It defines the member twice, as `removed: D` with no reason and `added: D` with the reason, each keeping the other keywords; the enum value's second definition gets `value_method: false`. It raises `ConfigurationError` when the member is defined: without `deprecation_reason:`, with the label of `added:` or `removed:`, in a class without `Base` (the member's class reports it), on the arguments of a `GraphQL::Schema::Subscription` class, and on a `resolver:` (or `mutation:`, `subscription:`) field whose resolver class has its own `deprecation_reason`, which graphql-ruby would give the field in every version. A `deprecated:` outside `added:`..`removed:` in `versions:` raises the first time a query asks, like any lifecycle out of order.
452
+
453
+ `include GraphQL::Armory::Versioning::Base` in an object, interface (after `include GraphQL::Schema::Interface`), union, enum, scalar, input object, resolver or mutation class adds the class method `lifecycle added:, removed:`, with the same rules; `Type.armory_lifecycle.to_h` answers it, inherited as described in "How to use it". `GraphQL::Armory::Versioning.types(schema, label, context: {})` answers a `Versioning::View`; `View.new` with nothing answers nothing.
454
+
455
+ These raise `ConfigurationError` when a query of the schema is built (a `GraphQL::Query` built by hand included), and from `Versioning.types`; a root or `extra_types` changed later is checked again:
456
+
457
+ - Two definitions of one argument name or enum value on a subscription field's arguments, or in an input object or enum they take: graphql-ruby's `trigger` reads those arguments with no version, where two definitions raise. `deprecated:` makes two definitions, so it is refused there too.
458
+
459
+ - A lifecycle on the query, mutation or subscription root type: a version without it would have no root. Version its fields.
460
+ - A type with a lifecycle in `extra_types`: `__type` and the SDL read `extra_types` without asking `visible?`.
461
+
462
+ These raise `ConfigurationError` when the class is defined, before anything is added, because graphql-ruby would ignore the keywords, apply them partly or add memberships without them: `implements` or `possible_types` with `added:` or `removed:` when the abstract type's `type_membership_class` does not include `Base`, when a member is named by a String, when the implemented interface implements other interfaces (implement each one directly), or when the class implementing is an interface. A membership class with `Base` also refuses a lifecycle between types that do not both include `Base` (an object class, and an interface or union), since the checks below run from those types.
463
+
464
+ This raises `ConfigurationError` when the argument is asked, and, under the legacy Warden, when any query of the schema is built, whatever the argument's class: an argument that `loads:` an interface or union with a versioned membership, because graphql-ruby checks the loaded object against the members with no context. Load the object types instead.
465
+
466
+ These raise `ConfigurationError` the first time a query asks about the type, because graphql-ruby would serve the membership or the interface's definitions where it does not live:
467
+
468
+ - An object or union with a versioned membership and another membership to the same abstract type or object (a parent's, or a repeated one), or, for an object, to an interface that includes the versioned one.
469
+ - A field, argument or enum value defined more than once on one owner, with lifecycles that overlap in the query's version.
470
+ - An object that implements an interface with a lifecycle whose field another of its interfaces or a parent class declares differently. Declare the field on the object, or rename one.
471
+
472
+
473
+ These raise `ConfigurationError`, naming the member, the first time a query asks about it, because graphql-ruby would otherwise serve the member outside its version:
474
+
475
+ - A type that lives in a version where one of its versioned ancestors does not.
476
+
477
+ - A `default_value:` that names an input field or enum value the query's version does not have, by its own lifecycle or its type's. Introspection would print it, and the field would run without it. Use a default that every version of the argument has. An enum value renamed across versions with the same Ruby `value:` counts as one: the default is fine in every version where either name lives. The check runs only on argument classes that include `Base`: an argument class without it can still print, in an older version's SDL, a default naming a value added later; the audit reports it (`:default_value`).
478
+ - A versioned argument or enum value with the same name as one its parent class defines (an input object, enum, resolver or mutation subclass, or a field's own argument over its resolver's). graphql-ruby serves the parent's definition where the local one is hidden. Version the parent's definition, or rename it.
479
+ - On a directive used in the SDL (any location such as `FIELD_DEFINITION` or `OBJECT`, on the directive or on a subclass of it), an argument with a lifecycle, or one whose type reaches a versioned type, enum value or input field: the SDL prints applied directive arguments as the latest version sees them, without asking `visible?`. Arguments of directives used only in queries (`FIELD`, `QUERY` and the rest) can be versioned and can use versioned enums; graphql-ruby's built-in directives cannot be versioned.
480
+
481
+ `GraphQL::Armory::Versioning::Diff.(schema, from:, to:, context: {})` is `Diff.build(schema, context:).(from:, to:)` and answers a `Diff::Result`: `changes`, `breaking`, `dangerous` and `safe` (each a list of `Diff::Change` with `bucket`, `path` and `message`, breaking first, sorted by path), `breaking?`, `from`, `to`, and `to_s`. A label `versions:` does not hold raises `UnknownVersionError`; a retired one, or a schema that never installed the plugin, `ConfigurationError`. `Diff.new` has no schema and raises `ConfigurationError` instead of answering no changes; `Diff.new(reader)` reads each version through `reader.view(label)`. `Diff::Reader::Substitute` answers the views it is told (`answer(label, view)`) and records `labels`; `Diff::Substitute.new(result)` answers `result` and records `calls`.
482
+
483
+ `GraphQL::Armory::Versioning::Dump.(schema, dir, context: {})` is `Dump.build(dir).(schema, context:)` and answers the paths it wrote: each served version oldest first, then `latest.graphql`. It creates `dir` when missing. An empty `dir`, or a file in it that is a symbolic link, raises `ConfigurationError`. Each version it prints goes through `to_definition`, which records one `:version_served` signal per version: filter dumps out of rollout metrics. `Dump.new` writes nothing and answers `[]`; `Dump.new(directory)` writes through `directory.write(name, text)`. `Dump::Directory::Substitute` records `written` (file name => SDL) and, told `fail_with(error)`, raises it.
484
+
485
+ Rake tasks (Rails loads them; elsewhere `load "tasks/graphql_armory.rake"` from the gem):
486
+
487
+ - `graphql_armory:versions:dump[Schema,dir]`: writes the files (`dir` defaults to `schema`) and prints each path; exit 1 on a missing or unknown schema or a `ConfigurationError`.
488
+ - `graphql_armory:versions:diff[Schema,from,to]`: prints the diff; exit 1 when something breaks, on a missing argument, an unknown schema or a version it cannot compare.
489
+
490
+ Context key: `armory_version` (a String naming a served version).
491
+
492
+ Errors: `Versioning::ConfigurationError` (a server-side setup mistake), `UnknownVersionError` (a label the list does not hold, a `ConfigurationError`), `RefusedVersionError` with `code`, and `MissingVersionError` (one of those, for a missing version).
493
+
494
+ ## Guarantees and limits
495
+
496
+ **What is guaranteed.** A client that names version V gets V's schema on every path that reads the query's schema: selection and aliases, fragments, validation and its suggestions, introspection (`__schema`, `__type`, `fields`), `to_definition` and `Schema.validate` with V in the context, on the interpreter and on `Execution::Next`, and each query of a multiplex sees its own version. A refused version runs nothing, even with `validate: false`, and even when the query brings its own `static_validator:` (the plugin replaces it; a [persisted queries](persisted-queries.md) refusal is kept, since it runs nothing either). The version lives in the query's context: one query's version cannot reach another's. What it covers is types, fields, arguments (input fields and directive arguments included), enum values and memberships (`implements`, `possible_types`).
497
+
498
+ **Limits.**
499
+
500
+ - **Lookups with no context see every field and argument.** With two definitions of one name, they raise graphql-ruby's `DuplicateNamesError` instead (`Types::Invoice.get_field("total")`, and the default values graphql-ruby checks when the schema boots). `Types::Invoice.get_field("amount")`, `Types::Invoice.fields`, `get_argument`, a union's `possible_types`, an object's `interfaces` and other lookups without a query answer the member whatever its lifecycle: graphql-ruby answers them without asking `visible?`. Enum values asked without a query (`Types::Status.values`) and, under `Schema::Visibility`, `AppSchema.get_type` answer the latest version. Read a version's schema through `Versioning.types(schema, label)` or `to_definition(context: { armory_version: ... })`.
501
+ - **Subscription triggers** build their query with `context: {}` unless you pass one, so the `default:` policy applies to finding the field: under `default: :refuse` a trigger that reaches a versioned field raises `MissingVersionError`, and under `:latest` it cannot name a field removed in the latest version. Pass `context: { armory_version: ... }` to every `trigger`, naming a version that has the field and the arguments you pass. Its arguments are another matter: graphql-ruby reads them with no query, where every definition counts, fills in the default of every argument and input field whatever its version, and then builds the topic with the trigger's version. So a subscription argument with a default that one served version does not have breaks that version's triggers (a trigger naming it raises, and a trigger naming another version never reaches its subscribers), whatever context you pass. Keep subscription arguments with a default in every version; the audit reports the ones that differ (`:subscription_trigger`). Each subscriber's update runs with the context it stored, so its own version applies there.
502
+ - **Queries built outside `query_class`.** A `Query::Partial` (`run_partials`) sees its parent's version, because it reads its parent's schema, and so does the second context `Schema::Visibility`'s `migration_errors: true` builds for a query; neither records a signal. A `GraphQL::Query` your code builds itself skips `query_class`: it is decided from its own `armory_version` the first time a versioned field is asked about, and records no signal. A refused version raises `RefusedVersionError` there instead of answering a validation error, and such a query that touches no field of a class that includes `Base` runs.
503
+ - **`validate: false` with a field outside the version** raises graphql-ruby's own error ("No field definition found", or a `NoMethodError` on graphql 2.4) quoting the client's selection; nothing resolves.
504
+ - **The legacy `Schema::Warden` falls back.** When a type's own definition of a field is hidden in V, the Warden serves a parent class's or an interface's definition of the same name instead. `Schema::Visibility` never does: use it. Under either, the audit reports the shape (`:fallback`): version the parent's or the interface's definition with the type's. For arguments and enum values, where graphql-ruby falls back on every path, the plugin refuses the shape instead (see Reference).
505
+ - **Data outside the version fails the response.** A resolver that returns an enum value the client's version does not have raises graphql-ruby's `GraphQL::Schema::Enum::UnresolvedValueError`, and a `resolve_type` that answers a type outside it raises `GraphQL::UnresolvedTypeError`, the twin of another version included; neither is turned into `null`. Filter or map the data in the resolver with `GraphQL::Armory::Versioning.version(context)`:
506
+
507
+ ```ruby
508
+ def docs # Doc's member Contract arrived in 2025-01-01
509
+ docs = Doc.all
510
+ docs = docs.reject { |doc| doc.is_a?(Contract) } if GraphQL::Armory::Versioning.version(context) < "2025-01-01"
511
+ docs
512
+ end
513
+ ```
514
+
515
+ The plugin decides the schema, never the data.
516
+ - **Type twins whose lifecycles overlap** are not refused by the plugin: graphql-ruby raises its `DuplicateNamesError` wherever the name is looked up in a version both live in (introspection, the SDL, fragments, variables), but fields that return each class still run there. Keep their lifecycles apart.
517
+ - **Type twins outside a query.** Lookups with no context answer one twin: `AppSchema.get_type("Money")` and Warden's `AppSchema.types` the one the latest version sees (none, when no twin lives there). A `LateBoundType` is refused only when the field, argument or type naming it includes `Base`. Two mutations sharing a `graphql_name` generate two payload and input types sharing one too, so they are type twins as well, each with its mutation's lifecycle. Under the legacy Warden, a union's members are matched by name, so a union listing one twin makes the other findable by name. Authorization's check of an interface's implementers (an implementer that bypasses the interface's rule) reads every twin in every version: a bypass on one twin denies the interface's fields in all of them.
518
+ - **A type reachable only through types outside the version** is not listed, introspected or selectable there, but its name can still be found: a fragment or variable naming it gets a different error than an unknown name. Version such types together.
519
+ - **A type reachable only through a hidden membership** stays findable by name (`__type`, `__schema.types`, the SDL, `Versioning.types`) under `Schema::Visibility`, while the legacy Warden drops it. Give it its own `lifecycle`.
520
+ - **An interface or union left with no member in V.** The legacy Warden hides an interface with no visible implementer, with every field that returns it; `Schema::Visibility` keeps it, and an empty union prints as an invalid `union X` in the SDL. Give the abstract type the lifecycle its members have.
521
+ - **A required argument hidden in V is not required in V.** A client of a version where the argument does not exist cannot send it, so the field runs without it: the resolver needs a Ruby default for it. An input field hidden in V is absent from the input object, and a `validates required: { one_of: }` whose every option is hidden in V fails every request in V.
522
+ - **Default values that graphql-ruby checks at boot.** graphql-ruby validates every `default_value:` against the latest version when the schema loads, so a default naming an enum value removed in the latest version raises its `InvalidDefaultValueError` at boot, whatever the argument's own lifecycle. An argument defined after the schema loaded (code reloading) escapes that check, and then `to_definition` raises in every version: the audit reports it (`:default_value`).
523
+ - **An enum with every value hidden in V** raises graphql-ruby's `MissingValuesError` on any query that reaches it in V, introspection included. Give the enum the lifecycle instead.
524
+ - **Named `Schema::Visibility` profiles** work only as the plugin's own, with `profiles: true` (graphql 2.4.10 or later; earlier versions build a named profile from the first query's context and keep it for every query, so `use` raises `ArgumentError` there). They hold the version and nothing else: Authorization's `visible_when:` raises under them, and your own `visible?` is asked once per version, so it must not read the user (the audit's `:profiles`). Without `profiles: true`, a query under a named profile raises `ConfigurationError`. Dynamic profiles (`dynamic: true`, or no profiles) work either way.
525
+ - **`Schema::AlwaysVisible`** never asks `visible?`, so nothing would be versioned: the first query and `Versioning.types` raise `ConfigurationError` instead, even when it was added after `use`.
526
+ - **Your own `visible?`** on a base class, or the schema's `self.visible?`, must call `super`, or the plugin is skipped for those members. The audit cannot read whether it does: it reports each one (`:own_check`) until you declare it in `own_checks:`.
527
+ - **Two refusing plugins.** With persisted queries, a request both refuse gets the persisted query's code. Either way nothing runs.
528
+ - **Signals.** `to_definition`, `Schema.validate` and subscription triggers go through `query_class` too, so each records one signal.
529
+ - **Cost.** A member without a lifecycle costs nothing (an argument's default value is read once). A versioned member costs a few Hash lookups each time graphql-ruby asks `visible?` (once per member per query, or once per member per version with `profiles: true`). See [Performance](#performance).
530
+
531
+ ## Performance
532
+
533
+ `benchmark/versioning.rb`: 40 object types of 25 fields; in the versioned setups half the fields have a lifecycle and 10 types have a field defined twice. The query selects all 25 fields of 8 types, each over a list of 100 items (20,000 fields resolved), as a client of the third of four versions. graphql 2.6.14, Ruby 3.3, Docker on Windows (one laptop, background load: read the differences, not the absolute numbers). Time per query and objects allocated:
534
+
535
+ | Engine | plain | warden | visibility | profiles |
536
+ |---|---|---|---|---|
537
+ | interpreter | 70.0 ms, 6,655 | 76.4 ms, 26,239 | 70.3 ms, 27,109 | 67.9 ms, 25,568 |
538
+ | next | 12.3 ms, 25,008 | 15.5 ms, 27,200 | 16.1 ms, 28,070 | 14.6 ms, 26,529 |
539
+
540
+ `plain` has no versioning and uses `Schema::Visibility`; `warden` and `visibility` ask `visible?` once per member per query; `profiles` is `profiles: true` with `preload: true`. On the interpreter the four are within the measurement's error (±7%). On `Execution::Next`, versioning costs about 3.5 ms per query on this query, and `profiles: true` takes about 1.5 ms of it away: what remains is not visibility but graphql-ruby running `authorized?` on each of the 800 objects, because `Base` on an object type adds one (to refuse a type outside the version that `loads:` reached). Run it yourself: `docker compose run --rm test bundle exec ruby benchmark/versioning.rb` (add `GRAPHQL_ARMORY_ENGINE=next` for Next, and `PROFILE=1 PROFILE_SETUP=profiles` passed with `docker compose run -e` to see where the time goes).
@@ -12,8 +12,13 @@ module GraphQL
12
12
  end
13
13
 
14
14
  # `at:` is what the problem is about (a type, a field, an argument or a method), for its file and line.
15
+ # Versioning's `deprecated:` writes two definitions of a member from one line: a problem the second one repeats
16
+ # is listed once.
15
17
  def problem(kind, path, message, at: nil)
16
- @problems << Report::Problem.new(kind, path, message, Location.of(at))
18
+ problem = Report::Problem.new(kind, path, message, Location.of(at))
19
+ return if at.respond_to?(:armory_deprecated_of) && at.armory_deprecated_of && @problems.include?(problem)
20
+
21
+ @problems << problem
17
22
  end
18
23
 
19
24
  def problem?(kind, path = nil)
@@ -21,6 +21,7 @@ module GraphQL
21
21
  end
22
22
 
23
23
  def call(schema)
24
+ @schema = schema
24
25
  @definitions.each do |type|
25
26
  next if Definitions.introspection?(type)
26
27
 
@@ -47,14 +48,19 @@ module GraphQL
47
48
  levels.flat_map { |level| level.public_send(reader).keys }.uniq.each do |name|
48
49
  definitions = levels.filter_map { |level| [level.public_send(reader)[name]].flatten if level.public_send(reader).key?(name) }
49
50
  definitions.flatten.each { |member| @override_paths[member] ||= "#{prefix}.#{member.public_send(name_reader)}" }
50
- falling = definitions.last.size == 1 ? definitions.flatten[0...-1] : definitions.flatten
51
+ falling = definitions.last.size == 1 || twins?(definitions.last) ? definitions[0...-1].flatten : definitions.flatten
51
52
  falling.each { |member| @falling_back[member] ||= "#{prefix}.#{member.public_send(name_reader)}" }
52
- definitions.select { |level| level.size > 1 }.flatten.each { |member| @duplicates << member }
53
+ definitions.select { |level| level.size > 1 && !twins?(level) }.flatten.each { |member| @duplicates << member }
53
54
  end
54
55
  end
55
56
 
56
57
  # graphql-ruby skips the fallback to a lower level only when the request carries a visibility profile. A static
57
58
  # profile runs with a plain Hash, so only `Schema::Visibility` with no profiles, not turned off below, rules it out.
59
+ # Versioning's twins: definitions on one level that no served version sees together are one member.
60
+ def twins?(level)
61
+ level.size > 1 && defined?(Versioning) && Versioning.respond_to?(:disjoint?) && Versioning.disjoint?(@schema, level)
62
+ end
63
+
58
64
  def visibility_without_fallback?(schema)
59
65
  visibility = schema.visibility
60
66
  return false unless visibility