Skip to content

Commit 3f5e22a

Browse files
committed
pro-1.24.6
1 parent 9e6dcd8 commit 3f5e22a

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CHANGELOG-pro.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
### Bug Fix
1010

11+
# 1.24.6 (24 May 2023)
12+
13+
- Defer: Add `incremental: true` for new proposed wire format, add example for working with GraphQL-Batch #4477
14+
1115
# 1.24.5 (24 May 2023)
1216

1317
- Stable relation connection: Quote table names and column names in selects and orders #4485

guides/defer/setup.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Before using `@defer` in queries, you have to:
1414
- Update `graphql` and `graphql-pro` gems
1515
- Add `@defer` to your GraphQL schema
1616
- Update your HTTP handlers (eg, Rails controllers) to send streaming responses
17+
- Optionally, customize `@defer` to work with GraphQL-Batch
1718

1819
You can also see a [full Rails & Apollo-Client demo](https://github.com/rmosolgo/graphql_defer_example).
1920

@@ -87,7 +88,7 @@ The initial result is _also_ present in the deferrals, so you can treat it just
8788
Each deferred patch has a few methods for building a response:
8889

8990
- `.to_h` returns a hash with `path:`, `data:`, and/or `errors:`. (There is no `path:` for the root result.)
90-
- `.to_http_multipart` returns a string which works with Apollo client's `@defer` support.
91+
- `.to_http_multipart(incremental: true)` returns a string which works with Apollo client's `@defer` support. (Use `incremental: true` to format patches for the forthcoming spec.)
9192
- `.path` returns the path to this patch in the response
9293
- `.data` returns successfully-resolved results of the patch
9394
- `.errors` returns an array of errors, if there were any
@@ -112,7 +113,7 @@ class GraphqlController < ApplicationController
112113
# Required for Rack 2.2+, see https://github.com/rack/rack/issues/1619
113114
response.headers['Last-Modified'] = Time.now.httpdate
114115
# Use built-in `stream_http_multipart` with Apollo-Client & ActionController::Live
115-
deferred.stream_http_multipart(response)
116+
deferred.stream_http_multipart(response, incremental: true)
116117
else
117118
# Return a plain, non-deferred result
118119
render json: result
@@ -126,6 +127,43 @@ end
126127

127128
You can also investigate a [full Rails & Apollo-Client demo](https://github.com/rmosolgo/graphql_defer_example)
128129

130+
## With GraphQL-Batch
131+
132+
`GraphQL-Batch` is a third-party data loading library that wraps GraphQL-Ruby execution. Deferred resolution happens outside the normal execution flow, so to work with GraphQL-Batch, you have to customize `GraphQL::Pro::Defer` a bit. Also, you'll need GraphQL-Pro `v1.24.6` or later. Here's a custom `Defer` implementation:
133+
134+
```ruby
135+
# app/graphql/directives/defer.rb
136+
module Directives
137+
# Modify the library's `@defer` implementation to work with GraphQL-Batch
138+
class Defer < GraphQL::Pro::Defer
139+
def self.resolve(obj, arguments, context, &block)
140+
# While the query is running, store the batch executor to re-use later
141+
context[:graphql_batch_executor] ||= GraphQL::Batch::Executor.current
142+
super
143+
end
144+
145+
class Deferral < GraphQL::Pro::Defer::Deferral
146+
def resolve
147+
# Before calling the deferred execution,
148+
# set GraphQL-Batch back up:
149+
prev_executor = GraphQL::Batch::Executor.current
150+
GraphQL::Batch::Executor.current ||= @context[:graphql_batch_executor]
151+
super
152+
ensure
153+
# Clean up afterward:
154+
GraphQL::Batch::Executor.current = prev_executor
155+
end
156+
end
157+
end
158+
end
159+
```
160+
161+
And update your schema to use your custom defer implementation:
162+
163+
```ruby
164+
# Use our GraphQL-Batch-compatible defer:
165+
use Directives::Defer
166+
```
129167
## Next Steps
130168

131169
Read about {% internal_link "client usage", "/defer/usage" %} of `@defer`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
60c791368edeecb40481e573b129851090fc11f3b5f2b6a6a05ae1f7a64ff53e113addc456a4df469b9bcdf84199bc52381df6fc5cf03d65028023f5e3520be9

0 commit comments

Comments
 (0)