graph_weaver 0.7.4 → 0.7.6

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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +2 -2
  3. data/README.md +1 -0
  4. data/docs/errors.md +12 -5
  5. data/docs/generated_modules.md +134 -17
  6. data/docs/getting_started.md +182 -20
  7. data/docs/logging.md +79 -35
  8. data/docs/migrating.md +126 -0
  9. data/docs/scalars.md +50 -6
  10. data/docs/testing.md +78 -14
  11. data/docs/upgrading.md +44 -2
  12. data/examples/README.md +4 -2
  13. data/examples/github/generate.rb +22 -8
  14. data/examples/github/generated/star_mutation.rb +2 -2
  15. data/examples/github/generated/stargazers_query.rb +2 -2
  16. data/examples/github/generated/starred_query.rb +2 -2
  17. data/examples/github/run.rb +1 -0
  18. data/examples/github/setup.rb +16 -8
  19. data/graph_weaver.gemspec +15 -6
  20. data/lib/generators/graph_weaver/install_generator.rb +49 -2
  21. data/lib/graph_weaver/client.rb +0 -23
  22. data/lib/graph_weaver/codegen/aliases.rb +36 -3
  23. data/lib/graph_weaver/codegen/emit.rb +20 -15
  24. data/lib/graph_weaver/codegen/enum_type.rb +52 -11
  25. data/lib/graph_weaver/codegen/nodes.rb +75 -32
  26. data/lib/graph_weaver/codegen.rb +260 -100
  27. data/lib/graph_weaver/coerce.rb +1 -1
  28. data/lib/graph_weaver/federation.rb +1 -6
  29. data/lib/graph_weaver/graph.rb +55 -5
  30. data/lib/graph_weaver/hints.rb +20 -5
  31. data/lib/graph_weaver/in_process.rb +2 -4
  32. data/lib/graph_weaver/input_struct.rb +50 -10
  33. data/lib/graph_weaver/internal/overrides.rb +126 -14
  34. data/lib/graph_weaver/internal/subgraphs.rb +1 -10
  35. data/lib/graph_weaver/internal/test_clients.rb +29 -7
  36. data/lib/graph_weaver/internal/unused.rb +62 -18
  37. data/lib/graph_weaver/internal/values.rb +24 -7
  38. data/lib/graph_weaver/internal.rb +23 -6
  39. data/lib/graph_weaver/log_subscriber.rb +27 -17
  40. data/lib/graph_weaver/logging.rb +115 -82
  41. data/lib/graph_weaver/parsing.rb +32 -3
  42. data/lib/graph_weaver/query_module.rb +67 -12
  43. data/lib/graph_weaver/railtie.rb +7 -2
  44. data/lib/graph_weaver/rspec.rb +41 -18
  45. data/lib/graph_weaver/schema_diff.rb +24 -5
  46. data/lib/graph_weaver/schema_loader.rb +29 -17
  47. data/lib/graph_weaver/tasks.rb +98 -16
  48. data/lib/graph_weaver/testing/fake_client.rb +28 -31
  49. data/lib/graph_weaver/testing/router.rb +26 -25
  50. data/lib/graph_weaver/testing.rb +27 -8
  51. data/lib/graph_weaver/transport.rb +1 -1
  52. data/lib/graph_weaver/version.rb +1 -1
  53. data/lib/graph_weaver.rb +72 -42
  54. metadata +3 -2
data/docs/logging.md CHANGED
@@ -13,8 +13,8 @@ Pick the level, get the story:
13
13
 
14
14
  | Level | What you see |
15
15
  |-------|--------------|
16
- | `debug` | the wire: query + variables per call (long queries truncated), response status/bytes/content type, request timing, connection open/drop, dynamically parsed modules |
17
- | `info` | one line per operation in Rails (see [Instrumentation](#instrumentation)), schema introspection (with timing) and cache hits/misses, the transport a client built, generated files written and any unregistered scalars, query modules loaded, a retry's wait and attempt number — and in development, what's being watched and what a save regenerated |
16
+ | `debug` | the wire: query + variables per call (long queries truncated), response status/bytes/content type, request timing, one line per attempt (its url status and which retry it was), connection open/drop, dynamically parsed modules |
17
+ | `info` | one line per call in Rails (see [Instrumentation](#instrumentation)), schema introspection (with timing) and cache hits/misses, the transport a client built, generated files written and any unregistered scalars, query modules loaded, a retry's wait and attempt number — and in development, what's being watched and what a save regenerated |
18
18
  | `warn` | every GraphWeaver error raised — `TransportError`, `ServerError`, `QueryError`, `QueryValidationError`, `CastError` — registrations the schema being generated against can't match, a retry skipped because the operation was a mutation, and every fetch the test router answered with fabricated data |
19
19
  | `error` | development only: a `.graphql` edit that won't compile, with its file and position — the modules already loaded keep serving |
20
20
 
@@ -61,9 +61,10 @@ value, since `expected an Int, got "lots"` is the whole diagnosis.
61
61
  ## Instrumentation
62
62
 
63
63
  A logger tells a human what happened; an APM needs to time it and count it.
64
- `GraphWeaver.instrumenter` is one callable wrapping every request — over the wire
65
- *and* in-process. **In Rails you set nothing**: the railtie installs the adapter
66
- below and attaches `GraphWeaver::LogSubscriber` on top of it. Everywhere else:
64
+ `GraphWeaver.instrumenter` is one callable wrapping every call GraphWeaver
65
+ makes — over the wire *and* in-process. **In Rails you set nothing**: the
66
+ railtie installs the adapter below and attaches `GraphWeaver::LogSubscriber` on
67
+ top of it. Everywhere else:
67
68
 
68
69
  ```ruby
69
70
  GraphWeaver.instrumenter = lambda do |event, payload, &block|
@@ -76,40 +77,74 @@ An instrumenter you set yourself is never replaced — including
76
77
  and return its value; a failure propagates through it, so the hook sees the
77
78
  exception and can record it.
78
79
 
79
- The one event is `GraphWeaver::EXECUTE_EVENT` (`"execute.graph_weaver"`) one
80
- request, start to parsed response, whichever client slot served it:
80
+ There are two events, and the second happens inside the first:
81
+
82
+ | Event | One of these is |
83
+ |-------|-----------------|
84
+ | `GraphWeaver::OPERATION_EVENT` (`"operation.graph_weaver"`) | one call of a generated module's `execute`/`execute!`, start to typed result or raise — the request it made, every retry and backoff beneath it, and the cast into your structs |
85
+ | `GraphWeaver::EXECUTE_EVENT` (`"execute.graph_weaver"`) | one request, start to parsed response, whichever client slot served it — so a call a [`Retry`](transports.md#retries) took three goes at is one operation event and three of these |
86
+
87
+ **Alert on the operation event**: it is the one that says what the *caller*
88
+ got. A `CastError` is raised after the response is back, so the request has
89
+ already closed `:ok` by the time the app sees a failure. It also fires at the
90
+ module seam, which every client slot passes through — a [test mode's](testing.md)
91
+ fake, the router and a cassette all report here, so a spec can assert on
92
+ instrumentation under any `graphql:` tag.
81
93
 
82
94
  ```ruby
83
- ActiveSupport::Notifications.subscribe(GraphWeaver::EXECUTE_EVENT) do |*, payload|
95
+ ActiveSupport::Notifications.subscribe(GraphWeaver::OPERATION_EVENT) do |*, payload|
84
96
  StatsD.timing("graphql.#{payload[:operation] || "anonymous"}", payload[:duration_ms],
85
97
  tags: ["status:#{payload[:status]}", "kind:#{payload[:kind]}", "code:#{payload[:code]}"])
86
98
  end
87
99
  ```
88
100
 
89
- **A subscriber that raises takes the request down with it**the response was
101
+ Two calls don't produce one. `from_response` on its own reports nothing no
102
+ call was made. And a module generated by 0.7.5 or earlier shows this seam
103
+ only half of one, so it emits the request event alone until you regenerate:
104
+ half a call reported `:ok` is the very thing the event exists to stop.
105
+
106
+ **A subscriber that raises takes the call down with it** — the response was
90
107
  computed and is then thrown away. That is `ActiveSupport::Notifications`' own
91
108
  semantics, the same on `sql.active_record`, so rescue inside the block. A
92
109
  `GraphWeaver::LogSubscriber` subclass already does.
93
110
 
94
- ### The payload
111
+ ### The operation payload
95
112
 
96
113
  | Key | When | |
97
114
  |-----|------|--|
98
115
  | `:operation` | always | the operation name sent with the request, nil for an anonymous document — what a trace keys on (a generated module always has one) |
99
- | `:client` | always | the class that ran it: `GraphWeaver::Transport::HTTP`, `GraphWeaver::InProcess`, your own |
116
+ | `:module` | always | the generated module's name, as a String — `nil` for one with no name (a [`GraphWeaver.parse`](getting_started.md)). The fact this seam has and the request doesn't: two graphs can name the same operation, and a trace that is slow wants the file |
117
+ | `:graph` | always | the [graph](getting_started.md#more-than-one-schema) the module was declared under, as a Symbol — `nil` for a module that names none. Never inferred from the client |
100
118
  | `:kind` | always | `:query`, `:mutation` or `:subscription` — what the document runs, so a write failure rate is a payload question rather than a guess at the operation's name. The shorthand `{ ... }` document is a `:query`. The same reading decides whether [`Retry`](transports.md#retries) may repeat the request |
119
+ | `:client` | always | the client the module **resolved to**: a test mode's stand-in names itself, a graph's client names itself, and over the wire it is `GraphWeaver::Client` — the request event beneath names the transport that carried it |
120
+ | `:status` | always | `:ok` (a typed result), `:errors` (the response carried GraphQL errors — what `execute!` raises `QueryError` for), or `:failed` (the call raised: the transport, a `CastError`, anything) |
121
+ | `:duration_ms` | always | the caller's wall clock — dispatch, every retry *and its backoff*, and the cast |
122
+ | `:code` | on `:errors` | the machine-readable reason, as on the request event |
123
+ | `:error` | on `:failed` | the exception's class name |
124
+
125
+ Not on it: `:url`, `:http_status`, `:retries`. Those describe one attempt, and
126
+ a call that took three goes has no single answer for any of them — read them
127
+ off the request events nested inside.
128
+
129
+ ### The request payload
130
+
131
+ | Key | When | |
132
+ |-----|------|--|
133
+ | `:operation` | always | the operation name sent with the request, nil for an anonymous document |
134
+ | `:client` | always | the class that ran it: `GraphWeaver::Transport::HTTP`, `GraphWeaver::InProcess`, your own |
135
+ | `:kind` | always | `:query`, `:mutation` or `:subscription`, read off the document |
101
136
  | `:status` | always | `:ok`, `:errors` (a response carrying GraphQL errors), or `:failed` (it raised) |
102
- | `:duration_ms` | always | start to parsed response, for **this attempt** — under a [`Retry`](transports.md#retries) no event covers the backoff sleep, so a tracer recovers the caller's wall clock from the sibling spans instead: the first attempt's start to the last one's end |
137
+ | `:duration_ms` | always | start to parsed response, for **this attempt** — the backoff between attempts belongs to the operation event above, which is where the caller's wall clock lives |
103
138
  | `:url` | over the wire | the endpoint; nil in-process |
104
139
  | `:http_status` | over the wire | what the server answered with, success or not; nil in-process |
105
140
  | `:schema` | in-process | the schema class's name, as a String, so a payload logs as it stands |
106
141
  | `:code` | on `:errors` | the machine-readable reason, always a String or `nil` — the first `extensions.code` *any* of the errors carries, not the first error's, since a code that exists beats the absence of one at position 0. Present and `nil` when the errors carry none. Never an HTTP status: that is `:http_status` |
107
142
  | `:error` | on `:failed` | the exception's class name |
108
143
  | `:retries` | under a `Retry` | how many retries this attempt follows. Each attempt is its own event, so one retried call is three events reading 0, 1, 2 — present at 0 rather than absent, so its absence means nothing was retrying |
109
- | `:graph` | always | the [graph](getting_started.md#more-than-one-schema) the generated module was declared under, as a Symbol — `nil` for a module that names none, and for a client called directly. Never inferred from the client: a wrong graph on a request is worse than no graph |
144
+ | `:graph` | always | the same label the operation above it carries, and `nil` for a request no generated module made a client called directly. Never inferred from the client: a wrong graph on a request is worse than no graph |
110
145
 
111
- Every key is filled in before your callable's block returns, so a subscriber reads
112
- a complete payload; `ActiveSupport::Notifications` adds `:exception` and
146
+ Every key is filled in before your callable's block returns, so a subscriber
147
+ reads a complete payload; `ActiveSupport::Notifications` adds `:exception` and
113
148
  `:exception_object` of its own when the block raises.
114
149
 
115
150
  **`:graph` labels one request**, never what a *server* does while answering one:
@@ -117,33 +152,35 @@ an in-process resolver that calls out produces an event of its own. The label is
117
152
  fiber-local, so a dispatch that crosses a `Fiber` — graphql-ruby's `Dataloader`
118
153
  does — arrives with `:graph` unset. No label rather than a wrong one.
119
154
 
120
- **Never the query text or the variables**: the payload fans out to subscribers
121
- that know none of the filtering rules, so the rule here isn't "scrub it", it's
122
- that it was never there. `:url` is the one thing on the payload that *is*
123
- scrubbed, because a url can itself be a credential — and the default names apply
124
- to its query parameters even when you have emptied or narrowed
125
- `filter_parameters`. Your list widens that; it can't narrow it.
155
+ **Never the query text or the variables**, on either event: the payload fans out
156
+ to subscribers that know none of the filtering rules, so the rule here isn't
157
+ "scrub it", it's that it was never there. `:url` is the one thing on either
158
+ payload that *is* scrubbed, because a url can itself be a credential — and the
159
+ default names apply to its query parameters even when you have emptied or
160
+ narrowed `filter_parameters`. Your list widens that; it can't narrow it.
126
161
 
127
162
  ### One line per operation
128
163
 
129
- In Rails the railtie also attaches `GraphWeaver::LogSubscriber`, which turns each
130
- event into one line — the shape ActiveRecord uses for a query:
164
+ In Rails the railtie also attaches `GraphWeaver::LogSubscriber`, which turns
165
+ each operation event into one line — the shape ActiveRecord uses for a query:
131
166
 
132
167
  ```
133
168
  GraphWeaver PersonQuery (12.3ms) ok
134
169
  GraphWeaver PersonQuery (8.1ms) errors [THROTTLED]
135
170
  GraphWeaver PersonQuery (31.2ms) failed GraphWeaver::TransportError
136
- GraphWeaver PersonQuery (5.0ms) ok (retry 2)
171
+ GraphWeaver PersonQuery (44.0ms) failed GraphWeaver::CastError
137
172
  GraphWeaver billing/InvoicesQuery (12.3ms) ok
138
173
  ```
139
174
 
140
- The operation is prefixed by its graph when the request carried one, so an app
175
+ The operation is prefixed by its graph when the call carried one, so an app
141
176
  with several graphs sorts its own log and an app with one never sees the prefix.
142
177
 
143
- **The summary is info, the wire is debug.** This is the only GraphWeaver line at
144
- info, so a production log gets one per operation and nothing that can carry PII;
145
- debug adds the query, the variables and the response *beneath* it. It writes
146
- through `GraphWeaver.logger`.
178
+ **The operation is info, the attempt is debug.** The info line is one *call*, so
179
+ it says what the caller got and a production log gets one per call a retried
180
+ call included, whose attempts and their backoff are inside that one duration.
181
+ Debug adds a line per attempt in the same shape, carrying the url's status and
182
+ which try it was, beneath the query and the variables. It writes through
183
+ `GraphWeaver.logger`.
147
184
 
148
185
  Outside Rails the same line is an instrumenter of your own, writing
149
186
  `payload[:operation]`, `[:duration_ms]` and `[:status]` from an `ensure` — the
@@ -151,11 +188,15 @@ shape the two tracing examples below use.
151
188
 
152
189
  ### OpenTelemetry
153
190
 
191
+ A span per operation, with a span per attempt inside it:
192
+
154
193
  ```ruby
155
194
  tracer = OpenTelemetry.tracer_provider.tracer("graph_weaver")
156
195
 
157
- GraphWeaver.instrumenter = lambda do |_event, payload, &block|
158
- tracer.in_span("graphql #{payload[:operation] || "query"}") do |span|
196
+ GraphWeaver.instrumenter = lambda do |event, payload, &block|
197
+ name = event == GraphWeaver::OPERATION_EVENT ? "graphql #{payload[:operation] || "query"}" : "graphql request"
198
+
199
+ tracer.in_span(name) do |span|
159
200
  block.call
160
201
  ensure
161
202
  span.add_attributes(payload.compact.transform_keys { "graphql.#{_1}" }.transform_values(&:to_s))
@@ -171,7 +212,8 @@ span status itself for a *raise* only, which is the last line's reason to exist.
171
212
  **Span status is not the alerting signal; `payload[:status]` is.** A response
172
213
  carrying GraphQL errors is a 200 that returned normally, so nothing raises and a
173
214
  span left to itself is `UNSET` — an SLO built on span status alone misses every
174
- GraphQL-level failure there is. Alert on `:status` and group by `:code`.
215
+ GraphQL-level failure there is. Alert on the operation event's `:status` and
216
+ group by `:code`.
175
217
 
176
218
  **Propagating the trace outward** is a header, and a
177
219
  [header value may be a callable](transports.md#headers) resolved per request —
@@ -191,9 +233,11 @@ GraphWeaver.instrumenter = lambda do |event, payload, &block|
191
233
  end
192
234
  ```
193
235
 
194
- Datadog's Net::HTTP and Faraday contribs already trace the transport layer, so
195
- with them on you have a span for the POST. This adds the span *above* it, named
196
- for the operation the one that means anything, since every GraphQL call is a
236
+ One hook covers both events, and the event name is the span name so an
237
+ operation and its attempts nest under distinct resources. Datadog's Net::HTTP
238
+ and Faraday contribs already trace the transport layer, so with them on you have
239
+ a span for the POST as well. This adds the spans *above* it, and the one named
240
+ for the operation is the one that means anything, since every GraphQL call is a
197
241
  POST to the same url.
198
242
 
199
243
  ## Details
data/docs/migrating.md ADDED
@@ -0,0 +1,126 @@
1
+ # Migrating from graphql-client (or a hand-rolled client)
2
+
3
+ **The wire doesn't change.** The same query text goes out and the same JSON comes
4
+ back, so this is a sequence of small commits rather than a cutover: queries move
5
+ across one at a time while the old client keeps serving, and most of the spec
6
+ suite you already have keeps passing untouched.
7
+
8
+ Budget 3–4 weeks of one engineer for an app with ~40 queries and 25k lines of
9
+ code reading them — [what it costs](#what-it-costs) breaks that down. If the app
10
+ doesn't run Sorbet and has fewer than ten queries, don't migrate: the wins that
11
+ survive without `srb tc` are real, but they don't pay for a checked-in code
12
+ generator ([alternatives](alternatives.md#graphlient)).
13
+
14
+ ## From graphql-client, in Rails
15
+
16
+ **Both gems stay in the Gemfile until the last commit.** `rails g
17
+ graph_weaver:install` boots the app to read your configuration, so an initializer
18
+ that still requires graphql-client has to keep resolving — remove the gem first
19
+ and the generator dies on `cannot load such file -- graphql/client`. Running the
20
+ two side by side is the right shape anyway.
21
+
22
+ 1. **Install beside graphql-client**, pointed at the dump you already have:
23
+ `rails g graph_weaver:install db/petstore_schema.json` ([a schema dump you
24
+ already have](getting_started.md#a-schema-dump-you-already-have)). Commit that
25
+ and nothing else.
26
+ 2. **Adopt the dump.** It records no source url, so `schema:diff` and
27
+ `queries:check` have no server to ask. `rake graph_weaver:schema:refresh`
28
+ rewrites it from the client the app points at (or `URL=<your endpoint>` once)
29
+ and records the provenance. Do it now: until you do, `queries:check` is
30
+ re-reading the file it is meant to be checking against, and says so. It
31
+ rewrites the file **in place, in graph_weaver's format** — pretty-printed
32
+ introspection JSON with a `graph_weaver:` provenance key beside `data`.
33
+ graphql-client goes on reading it, since `load_schema` hands the parsed hash
34
+ to graphql-ruby and graphql-ruby takes `data` and ignores its siblings; a dump
35
+ anything *else* reads is worth checking once.
36
+ 3. **Register scalars and enums, and regenerate** — if the API declares any.
37
+ Plenty don't: one whose leaves are all `String`, `Int`, `ID` and `Boolean` has
38
+ nothing to register and skips this step whole. Registrations are baked into
39
+ generated source, so one added later reaches nothing until the next
40
+ `rake graph_weaver:generate` ([scalars](scalars.md)).
41
+ 4. **Port one query end to end** — write the `.graphql` file, generate, rewrite
42
+ its call site — and leave the specs alone. They pass untouched. That single
43
+ commit is the proof for every one after it.
44
+ 5. **Port the rest, one commit per query.** Each `Client.parse` constant stays
45
+ where it is until its last caller is gone.
46
+ 6. **Only now port the specs** to `graphql: :fake` ([testing](testing.md)). Keep
47
+ webmock until the last wire-level stub is gone, and keep one spec that really
48
+ serves HTTP.
49
+ 7. **Delete graphql-client.** Last commit.
50
+ 8. **Sigs through your own app are a separate project**, after all of the above —
51
+ [the types stop where your sigs do](getting_started.md#the-types-stop-where-your-sigs-do)
52
+ says what that work is.
53
+
54
+ Keep your own error classes. Translating `GraphWeaver::QueryError`,
55
+ `TransportError` and `ServerError` at one seam leaves a controller, its rescues
56
+ and their specs untouched ([errors](errors.md)). Keep your presenters too, and
57
+ your fixtures until step 6.
58
+
59
+ ### Why app code and specs are separate commits
60
+
61
+ Because the wire is the same, a webmock suite doesn't notice which client sent
62
+ the request. Swap the client under an untouched suite and most of it stays green
63
+ — 18 of 22 stubbed examples in one migrated Rails app, same stubs, same JSON
64
+ fixtures; the four that failed were unit specs feeding a hand-rolled `Struct` to
65
+ a presenter. That is the difference between a reviewable migration and a big-bang
66
+ one.
67
+
68
+ ## From a hand-rolled client
69
+
70
+ Same skeleton, three differences.
71
+
72
+ - **One branch, no seam.** There's no generator to keep bootable and a
73
+ hand-rolled client is sixty lines, so running two of them side by side costs
74
+ more than it saves.
75
+ - **Set the load order first**: `GraphWeaver.load_generated!` goes before your own
76
+ requires ([not Rails?](getting_started.md#not-rails)). Add `rake` to the Gemfile
77
+ while you're there.
78
+ - **Keep the old client's transport specs**, rewritten against a real
79
+ `GraphWeaver.new(url, retries: 2)` with webmock. Retries, timeouts and backoff
80
+ are the one thing fakes can't cover.
81
+
82
+ ## What you delete
83
+
84
+ - **The retry loop.** A hand-rolled one usually retries everything, mutations
85
+ included. `retries: 2` excludes mutations by default, honours `Retry-After`,
86
+ and knows 408 and 429 ([retries](transports.md#retries)) — twenty lines gone and
87
+ a correctness bug gone with them.
88
+ - **Scalar parsers.** `register_scalar("Money", Money)` is one line, and it
89
+ deletes the `"USD 12.50".split` sitting in every place that parsed one. `Date`
90
+ and `DateTime` need no registration at all ([scalars](scalars.md)).
91
+ - **Fragment unwrapping.** graphql-client masks a spread fragment's fields on the
92
+ parent, so every call site reads `Petstore::PetFields.new(data.pet).name` and
93
+ carries the parent alongside for the fields the fragment didn't cover. Generated
94
+ structs inline the fragment, so it's `pet.name`; a fragment that is a whole
95
+ selection becomes one shared Ruby type
96
+ ([hoisting](generated_modules.md#a-shared-fragment-is-one-type)).
97
+ - **JSON fixtures and stub helpers.** `graphql: :fake` fabricates a
98
+ schema-correct response and you pin the fields the example is about. Pins are
99
+ schema vocabulary (`"Pet.species" => "DOG"`), so they survive query refactors,
100
+ and they're spellchecked — a typo raises instead of leaving the example green
101
+ against random data ([testing](testing.md)).
102
+
103
+ ## The one behaviour change to plan for
104
+
105
+ **Enum drift is fatal.** A hand-rolled client hands you the raw string, so a value
106
+ the server added after you shipped falls through to whatever your code does with
107
+ an unknown one — commonly a `humanize`. A generated enum refuses instead: casting
108
+ raises and the whole response is lost, naming the value, the enum and the values
109
+ it knows about.
110
+
111
+ That is the right default for an API you own and a real risk for one you don't,
112
+ because it breaks on a day nobody deployed. Before you port a query over an enum
113
+ someone else can extend, register a fallback for it and unknown values land there
114
+ instead — [enums](scalars.md#enums-map-onto-your-own-tenum).
115
+
116
+ ## What it costs
117
+
118
+ | | how it scales | ~40 queries, 25k lines |
119
+ |---|---|---|
120
+ | `.graphql` files and generating | sublinear — the second query costs a tenth of the first | 2–3 days |
121
+ | call sites | linear, and the bulk of it: every `data["x"]["y"]` becomes `x.y`, and every defensive `nil` guard either disappears or turns out to have been wrong | ~1 day per 3–4k lines of consuming code |
122
+ | specs | linear, and pleasant — the fixture-to-pin conversion is the same edit every time | ~2 days |
123
+ | sigs through your own app | the one that decides whether the migration paid off | weeks; a separate project |
124
+
125
+ Coming off graphql-client, run the new one behind the old for the first two
126
+ weeks. The wire compatibility makes that free.
data/docs/scalars.md CHANGED
@@ -428,12 +428,17 @@ It raises naming every scalar that disagreed and which way:
428
428
  ```
429
429
  2 scalar(s) disagree with Catalog::Schema:
430
430
  Money: the server refused "12.5", the wire form serialize: writes (expected "12.50 USD")
431
- Decimal: round-trips lossily — sent 0.123456789123456789e9, got back 0.1234567891234567e9
431
+ Decimal: round-trips lossily — sent 123456789.123456789, got back 123456789.1234567
432
432
  ```
433
433
 
434
434
  The fabricated value is all it has to work with, so pin the one that matters:
435
435
  `config.overrides = { "Decimal" => "123456789.123456789" }` is how the precision
436
- case gets exercised at all — two decimal places always survive a Float. Pass the
436
+ case gets exercised at all — two decimal places always survive a Float. That is
437
+ the only door onto a pin here: `check_scalars!` takes the schema and nothing
438
+ else, and runs outside every fake, so `graphql_fake` would be a no-op. The last
439
+ leg compares with `==`, so a registered class that defines none is reported as
440
+ uncheckable rather than lossy — two identical spellings mean identity, not a
441
+ loss. Pass the
437
442
  schema **class**; a dump's scalars pass values through, so against one this checks
438
443
  only that a registration's `cast:` accepts what its own `serialize:` writes, which
439
444
  is a different question (see below).
@@ -501,17 +506,56 @@ Two safety properties do the real work:
501
506
  a member, or generation fails naming the gaps (`PetKind has no member for
502
507
  Species value(s) DOG — add them, pin with map:, or absorb with fallback:`), so
503
508
  your enum drifting from the server's is caught by `rake graph_weaver:generate`,
504
- not in production.
509
+ not in production. The other direction only *warns* — one app-wide `T::Enum` can
510
+ serve two APIs that each expose a subset — and a kwarg carrying a member this
511
+ schema declares no value for is refused by name at the call.
505
512
  - **`fallback:` for forward-compat**: `fallback: PetKind::Unknown` makes *casting*
506
513
  absorb wire values the server added after you generated, so responses keep
507
514
  flowing instead of raising. Inputs stay strict either way: a typo'd input is your
508
- bug, not drift. A union or interface absorbs the same drift with no registration
509
- a member added upstream lands in the catch-all `Other` its dispatch always
510
- carries ([generated modules](generated_modules.md#abstract-types)).
515
+ bug, not drift. You don't need an enum of your own for that:
516
+ `GraphWeaver.register_enum("Species", fallback: true)` gives the *generated* enum
517
+ the same forward-compat, [below](#values-the-server-hasnt-told-you-about-yet).
511
518
 
512
519
  The translation tables are emitted into the generated source (`SPECIES_FROM_WIRE` /
513
520
  `SPECIES_TO_WIRE`) — reviewable in the diff, no runtime registry.
514
521
 
522
+ ### Values the server hasn't told you about yet
523
+
524
+ A server adding an enum value is a deploy you weren't part of, and by default the
525
+ next response carrying it raises. `fallback: true` says take it anyway:
526
+
527
+ ```ruby
528
+ GraphWeaver.register_enum("Species", fallback: true)
529
+ ```
530
+
531
+ The generated `Species` gains one member, `Other`, and every wire value the schema
532
+ doesn't declare casts to it — the leniency a union or interface already has for
533
+ free, where a member added upstream lands in the catch-all `Other` its dispatch
534
+ always carries ([generated modules](generated_modules.md#abstract-types)).
535
+
536
+ ```ruby
537
+ pet.species # => GraphQLTypes::Species::Other, for "AXOLOTL"
538
+ ```
539
+
540
+ Three things follow from that, and they are the whole rule:
541
+
542
+ - **`Other` doesn't carry the value it absorbed.** A `T::Enum` member is a
543
+ singleton, so there is nowhere to put it; the cast writes one debug line
544
+ (`GraphQLTypes::Species absorbed "AXOLOTL" into Other`) and that is the record
545
+ — so a presenter humanising the server's spelling can't, and falls back to a
546
+ label of its own. `as_json` writes the member's own wire value, `"__other__"`,
547
+ which `from_h` reads straight back — but `render json: result` hands that
548
+ sentinel to whoever is downstream, and no server declares it.
549
+ - **Inputs stay strict.** No wire value means `Other`, so a variable carrying it
550
+ is refused rather than sent. Everything else is unchanged: a typo'd input is
551
+ your bug, not drift.
552
+ - **A schema that already declares `OTHER` keeps it.** The declared value stays
553
+ `Other`, and the fallback member is `Other2` — the same rule a union's
554
+ catch-all struct follows when a member is already named `Other`.
555
+
556
+ It rides along with `alias:` on the one registration, and like every registration
557
+ it belongs to one graph.
558
+
515
559
  ### Two spellings, one value
516
560
 
517
561
  A schema mid-rename declares both `LEGACY_MODE` and `legacy_mode` so old clients
data/docs/testing.md CHANGED
@@ -136,6 +136,15 @@ leaving the example green against random data. **Schema vocabulary, not Ruby:**
136
136
  spelled the way *that* schema spells it, so a Hasura table type is
137
137
  `"pokemon_v2_pokemon"` and not a Ruby-cased guess at it.
138
138
 
139
+ **The key is checked; the value is yours.** `"Pet.species" => "RABBIT"` is
140
+ accepted on an enum declaring no `RABBIT`, and the fake serves it as written — a
141
+ pin is the escape hatch, so nothing second-guesses what you put through it. On an
142
+ enum registered
143
+ [`fallback: true`](scalars.md#values-the-server-hasnt-told-you-about-yet) that is
144
+ how you rehearse drift, since the value lands in the catch-all member exactly as
145
+ a real server's new one would; without a fallback the cast raises where a real
146
+ response would.
147
+
139
148
  A pin **merges**, and pins a **subtree** as readily as a leaf: name the fields
140
149
  the example is about and everything else in the selection is still fabricated. A
141
150
  pinned list is exactly as long as you write it — `{}` means "another one, all
@@ -158,7 +167,8 @@ snake_cased reader, puts the value on the wire the way its
158
167
  a `T::Enum` as its value), recurses into nested objects and arrays of them, and
159
168
  **fabricates any field the object doesn't answer**. Readers are field names, not
160
169
  aliases; `__typename` comes from the key, so at a union or interface pin the
161
- concrete type (`"Person"`, never `"Named"`). A FactoryBot sequence advances on
170
+ concrete type `"Person"` or `"Person.name"`, never `"Named"` or
171
+ `"Named.name"`, and both spellings of the abstract one are refused. A FactoryBot sequence advances on
162
172
  its own counter, which is the one thing `--seed` can't reproduce.
163
173
 
164
174
  A **scalar type pin** is the one thing a scalar registered as *your own class*
@@ -217,9 +227,12 @@ end
217
227
  ```
218
228
 
219
229
  The live schema *class* is found for you (a schema dump has no resolvers, so it
220
- won't do). If two loaded classes match, or none does, it says so and asks for
221
- `config.schema = MySchema` and in Rails, remember that an autoloaded schema
222
- isn't loaded until something references it.
230
+ won't do), from the three places one can be named: `config.schema` when you set
231
+ it to a class, else the class [that graph](getting_started.md#more-than-one-schema)
232
+ names, else the class your own `GraphWeaver.client` already runs in-process.
233
+ Nothing is *scanned* for: with none of the three it refuses, and names the fix
234
+ that fits — declare the class on the graph, write `graphql_in_process(MySchema)`
235
+ in the example, or set `config.schema = MySchema` for the suite.
223
236
 
224
237
  A federated app has no one live class, so the example says which subgraph it
225
238
  means: `graphql_in_process(Reviews::Schema)`. Testing one subgraph's resolvers
@@ -253,7 +266,11 @@ block to scope it for the example that needs two identities —
253
266
  `graphql_context(admin: true) { … }` — and call it with nothing to read the
254
267
  context back. Under `graphql: :fake` it refuses: there are no resolvers to
255
268
  receive a context, and silently ignoring one would leave an example asserting on
256
- data nothing scoped. Pin the data itself instead.
269
+ data nothing scoped. Pin the data itself instead. Under `graphql: :live` it
270
+ refuses too — including in an untagged example, which is `:live` by default, and
271
+ including when your own client is an `InProcess` running your resolvers: that
272
+ client carries the context it was *built* with, and `graphql_context` reaches
273
+ only the stand-ins the mode built for this example.
257
274
 
258
275
  ## A federated graph — `graphql: :router`
259
276
 
@@ -360,8 +377,17 @@ that is a pure *client* of someone else's API gets a schema-correct server
360
377
  without writing one. A graph with no schema at all is refused, **by `:wire`'s own
361
378
  name** — the one fallback the other tags have and this one can't use is your
362
379
  client's own schema, since reading it means introspecting the endpoint `:wire`
363
- has just stubbed. Commit a dump, or set `config.schema`. A graph whose `client`
364
- posts nowhere is refused by name too.
380
+ has just stubbed. Commit a dump, or set `config.schema`. That refusal waits until
381
+ one of *that graph's* modules runs, so a graph the example never touches never
382
+ refuses it.
383
+
384
+ **A graph whose `client` posts nowhere runs above the wire** — there is no
385
+ endpoint to stub, so it is served in the client slot, exactly as
386
+ `graphql: :in_process` would serve it. That is how an app that owns resolvers
387
+ *and* calls someone else's API tests the remote half over the wire: its
388
+ in-process graph runs in-process, and every graph posting to a url is still
389
+ served at that url. An example where *no* graph posts anywhere is refused — a
390
+ `:wire` that serves nothing tests no transport.
365
391
 
366
392
  **It says which, on the logger** — the choice is the one thing this tag makes for
367
393
  you, and it is invisible from inside the example. One line per endpoint, at
@@ -377,7 +403,12 @@ set `GraphWeaver::Testing.config.schema` — the case worth catching, because an
377
403
  app that owns real resolvers otherwise goes green against fabricated data with
378
404
  nothing said. A warning rather than a refusal, because a loaded class isn't proof
379
405
  you meant it *here* — a federated suite loads every subgraph's — and a fake
380
- behind the wire is a thing to want.
406
+ behind the wire is a thing to want. A graph that ran above the wire gets a line
407
+ of its own, since the transport the example asked for never ran for it:
408
+
409
+ ```
410
+ graph_weaver: :wire has no endpoint for graph :orders — its client posts to none, so its modules run above the wire, as graphql: :in_process would
411
+ ```
381
412
 
382
413
  **A helper says what goes behind the wire.** Under the other tags a `graphql_*`
383
414
  helper takes the client slot; under `:wire` it is served instead — the client
@@ -423,7 +454,9 @@ time** — answering one means setting the client's context for the length of th
423
454
  dispatch, so the identity a request asked for is the identity it gets, whatever
424
455
  else is in flight. The lock is the client's own, so it holds however the endpoint
425
456
  is mounted. A `context:` hash is served **concurrently**: nothing writes it, so
426
- there is nothing to serialize.
457
+ there is nothing to serialize. Either way a query reads its context once, so
458
+ every subgraph it hops through runs as the identity it started with, even if
459
+ the router's context is reassigned while it is in flight.
427
460
 
428
461
  ### Making the served endpoint fail
429
462
 
@@ -546,10 +579,14 @@ rather than guessing**:
546
579
  the wire value that graph's generated cast expects. (Pins and `overrides:` stay
547
580
  suite-wide, keyed by scalar name — one `"Money"` override for the run.)
548
581
  - **`:in_process`** needs the live schema *class*, since only that has resolvers:
549
- the one that graph names, else the one your client already runs in-process,
550
- else the loaded class that defines everything the schema declaresthe same
551
- derive-verify-refuse rule that
552
- [maps subgraphs](federation.md#which-schema-serves-which-subgraph).
582
+ `config.schema` when you set it to a class, else the one that graph names, else
583
+ the one your client already runs in-process. Nothing is scanned for a loaded
584
+ class is no evidence the app meant it *here* — so past those three
585
+ `:in_process` refuses, while [`:wire`](#over-the-wire--graphql-wire) reads the
586
+ same list and serves a fake, **warning** and naming the class it saw.
587
+ (Deriving a schema from what *is* loaded is the
588
+ [subgraph rule](federation.md#which-schema-serves-which-subgraph), where the
589
+ routing table says what a match has to define.)
553
590
  - **`:router`** plans against the composed supergraph **that graph** names, else
554
591
  `config.router = { supergraph: … }`, else the committed dump when *that*
555
592
  carries `@join__*` markers, else the dump your own client was built from
@@ -617,7 +654,17 @@ one ahead of yours, so a `before` setting `config.schema`, `config.router` or
617
654
  **refused**, not ignored — a green example running against the wrong stand-in is
618
655
  the expensive outcome.
619
656
 
620
- ### Fabricated list lengths
657
+ That hook is also why **a refusal the tag itself raises can't be asserted with
658
+ `expect { }.to raise_error`**: it happens before the example body, and rspec
659
+ records it as the example's failure rather than letting anything catch it — an
660
+ `around` hook included, since `example.run` returns normally there. To assert
661
+ one, call the helper in an **untagged** example, where the same refusal is
662
+ raised in the body: `expect { graphql_in_process }.to raise_error(...)`, and the
663
+ same for `graphql_fake` and `graphql_router`. `:wire` has no helper, so its
664
+ whole-example refusal can only be read off the failure — the per-graph one it
665
+ raises when a module runs is in the body, and assertable.
666
+
667
+ ### Fabricated list lengths and nulls
621
668
 
622
669
  `list_size` is how long an **unbounded** list is — an Integer exactly that many,
623
670
  a Range randomized within it, or a Hash saying it per list. A list with a
@@ -632,6 +679,23 @@ for the rest — `config.list_size = { "Row.tags" => 3, default: 1000 }` holds t
632
679
  inner list at 3 however large the outer one grows. Or cap it in the query
633
680
  (`tags(first: 3)`), where the query is yours to change.
634
681
 
682
+ `null_chance` takes the same two shapes, and the same keys: a number from 0 to 1
683
+ for every nullable field, or a Hash saying it per field with `default:` for the
684
+ rest. So the example about one missing value says only that —
685
+ `graphql_fake(null_chance: { "Person.nickname" => 1.0 })` — instead of nilling
686
+ everything else alongside it. A misspelled key is refused and spellchecked, the
687
+ way a pin's is.
688
+
689
+ **Both are keyed by field, and the key has to be one the option can reach**: a
690
+ type name (`"Person"`) is refused rather than read as the nearest field, a
691
+ `null_chance` key naming a non-null field is refused (nothing there ever comes
692
+ back null), and a `list_size` key naming a field that isn't a list likewise —
693
+ all three are keys that would validate clean, fabricate nothing and leave the
694
+ example green. `"default"` is always the fallback, never a field, so a schema
695
+ field actually called `default` is reachable only as `"Type.default"`. And a
696
+ [pin](#pins) on the same field beats both: a pinned value is used as written,
697
+ so it is neither nulled nor resized.
698
+
635
699
  **A list field whose name ends in `errors` fabricates empty** — `userErrors`,
636
700
  `errors`, `mutationErrors`. The Relay/Shopify payload
637
701
  (`placeOrder { order userErrors }`) is the ecosystem's mutation shape, and a