You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/defer/setup.md
+40-2Lines changed: 40 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ Before using `@defer` in queries, you have to:
14
14
- Update `graphql` and `graphql-pro` gems
15
15
- Add `@defer` to your GraphQL schema
16
16
- Update your HTTP handlers (eg, Rails controllers) to send streaming responses
17
+
- Optionally, customize `@defer` to work with GraphQL-Batch
17
18
18
19
You can also see a [full Rails & Apollo-Client demo](https://github.com/rmosolgo/graphql_defer_example).
19
20
@@ -87,7 +88,7 @@ The initial result is _also_ present in the deferrals, so you can treat it just
87
88
Each deferred patch has a few methods for building a response:
88
89
89
90
-`.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.)
91
92
-`.path` returns the path to this patch in the response
92
93
-`.data` returns successfully-resolved results of the patch
93
94
-`.errors` returns an array of errors, if there were any
@@ -112,7 +113,7 @@ class GraphqlController < ApplicationController
112
113
# Required for Rack 2.2+, see https://github.com/rack/rack/issues/1619
You can also investigate a [full Rails & Apollo-Client demo](https://github.com/rmosolgo/graphql_defer_example)
128
129
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
+
moduleDirectives
137
+
# Modify the library's `@defer` implementation to work with GraphQL-Batch
138
+
classDefer < GraphQL::Pro::Defer
139
+
defself.resolve(obj, arguments, context, &block)
140
+
# While the query is running, store the batch executor to re-use later
0 commit comments