Skip to content

Commit fd3fed3

Browse files
authored
Update README.md
1 parent 8477164 commit fd3fed3

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ Is easy to optimize this GraphQL query with `dataloader` because assumedly the v
105105
{
106106
users(limit: 5) {
107107
name
108-
friends(limit: 5) {
108+
friends1: friends(limit: 5) {
109109
name
110110
}
111-
friends(limit: 5, offset: 5) {
111+
friends2: friends(limit: 5, offset: 5) {
112112
name
113113
}
114114
}
@@ -136,7 +136,38 @@ This package offers an alternative to the `dataloader` batching strategy. This p
136136
}
137137
```
138138

139-
Here we would only have *4* executions instead of 155. One for the root field, one for the first `friends` field, one for the second `friends` field, and so on. This is a powerful alternative to `dataloader` in a case where `dataloader` falls short.
139+
Here we would only have *4* executions instead of 156. One for the root field, one for the first `friends` field, one for the second `friends` field, and so on. This is a powerful alternative to `dataloader` in a case where `dataloader` falls short.
140+
141+
## How?
142+
143+
A batch resolver will run once per GraphQL *field*. So if we assume that you are using a batch resolver on your `friends` field and a frontend engineer writes a query like this:
144+
145+
```graphql
146+
{
147+
users(limit: 5) {
148+
name
149+
friends(limit: 5) {
150+
name
151+
friends(limit: 5) {
152+
name
153+
friends(limit: 5) {
154+
name
155+
}
156+
}
157+
}
158+
}
159+
}
160+
```
161+
162+
Every `friends(limit: 5)` field will run exactly one time. How does this work? A GraphQL.js resolver has the following signature:
163+
164+
```js
165+
(source, args, context, info) => fieldValue
166+
```
167+
168+
To batch together calls to this function by field, `graphql-resolve-batch` defers the resolution until the next tick while synchronously bucketing `source` values together using the field GraphQL.js AST information from `info`. On the next tick the function you passed into `createBatchResolver` is called with all of the sources that were bucketed in the last tick.
169+
170+
The implementation is very similar to the `dataloader` implementation. Except `graphql-resolve-batch` takes a more opionated approach to how batching should be implemented in GraphQL whereas `dataloader` is less opionated in how it batches executions together.
140171

141172
## When do I use `dataloader` and when do I use `graphql-resolve-batch`?
142173

0 commit comments

Comments
 (0)