Skip to content

Commit 83a5587

Browse files
authored
Merge pull request #851 from etandel/feature/dataloader-docs
Improve dataloader doc
2 parents 89ba0da + 21dbaa9 commit 83a5587

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

docs/execution/dataloader.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ Create loaders by providing a batch loading function.
2525
return Promise.resolve([get_user(id=key) for key in keys])
2626
2727
28-
A batch loading function accepts an list of keys, and returns a ``Promise``
29-
which resolves to an list of ``values``.
28+
A batch loading function accepts a list of keys, and returns a ``Promise``
29+
which resolves to a list of ``values``.
3030

3131
Then load individual values from the loader. ``DataLoader`` will coalesce all
3232
individual loads which occur within a single frame of execution (executed once
3333
the wrapping promise is resolved) and then call your batch function with all
3434
requested keys.
3535

3636

37-
3837
.. code:: python
3938
4039
user_loader = UserLoader()
@@ -47,6 +46,19 @@ requested keys.
4746
A naive application may have issued *four* round-trips to a backend for the
4847
required information, but with ``DataLoader`` this application will make at most *two*.
4948

49+
Note that loaded values are one-to-one with the keys and must have the same
50+
order. This means that if you load all values from a single query, you must
51+
make sure that you then order the query result for the results to match the keys:
52+
53+
54+
.. code:: python
55+
56+
class UserLoader(DataLoader):
57+
def batch_load_fn(self, keys):
58+
users = {user.id: user for user in User.objects.filter(id__in=keys)}
59+
return Promise.resolve([users.get(user_id) for user_id in keys])
60+
61+
5062
``DataLoader`` allows you to decouple unrelated parts of your application without
5163
sacrificing the performance of batch data-loading. While the loader presents
5264
an API that loads individual values, all concurrent requests will be coalesced

0 commit comments

Comments
 (0)