Skip to content

Commit 3801669

Browse files
committed
Update dataloader docs
1 parent 3e43052 commit 3801669

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

docs/execution/dataloader.rst

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Dataloader
44
DataLoader is a generic utility to be used as part of your application's
55
data fetching layer to provide a simplified and consistent API over
66
various remote data sources such as databases or web services via batching
7-
and caching.
7+
and caching. It is provided by a seperate package `aiodataloader <https://pypi.org/project/aiodataloader/>`.
88

99

1010
Batching
@@ -15,32 +15,31 @@ Create loaders by providing a batch loading function.
1515

1616
.. code:: python
1717
18-
from promise import Promise
19-
from promise.dataloader import DataLoader
18+
from aiodataloader import DataLoader
2019
2120
class UserLoader(DataLoader):
22-
def batch_load_fn(self, keys):
23-
# Here we return a promise that will result on the
24-
# corresponding user for each key in keys
25-
return Promise.resolve([get_user(id=key) for key in keys])
21+
async def batch_load_fn(self, keys):
22+
# Here we call a function to return a user for each key in keys
23+
return [get_user(id=key) for key in keys]
2624
2725
28-
A batch loading function accepts a list of keys, and returns a ``Promise``
29-
which resolves to a list of ``values``.
26+
A batch loading async function accepts a list of keys, and returns a list of ``values``.
3027

3128
Then load individual values from the loader. ``DataLoader`` will coalesce all
3229
individual loads which occur within a single frame of execution (executed once
33-
the wrapping promise is resolved) and then call your batch function with all
30+
the wrapping event loop is resolved) and then call your batch function with all
3431
requested keys.
3532

3633

3734
.. code:: python
3835
3936
user_loader = UserLoader()
4037
41-
user_loader.load(1).then(lambda user: user_loader.load(user.best_friend_id))
38+
user1 = await user_loader.load(1)
39+
user1_best_friend = await user_loader.load(user1.best_friend_id))
4240
43-
user_loader.load(2).then(lambda user: user_loader.load(user.best_friend_id))
41+
user2 = await user_loader.load(2)
42+
user2_best_friend = await user_loader.load(user2.best_friend_id))
4443
4544
4645
A naive application may have issued *four* round-trips to a backend for the
@@ -54,9 +53,9 @@ make sure that you then order the query result for the results to match the keys
5453
.. code:: python
5554
5655
class UserLoader(DataLoader):
57-
def batch_load_fn(self, keys):
56+
async def batch_load_fn(self, keys):
5857
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])
58+
return [users.get(user_id) for user_id in keys]
6059
6160
6261
``DataLoader`` allows you to decouple unrelated parts of your application without
@@ -111,8 +110,8 @@ leaner code and at most 4 database requests, and possibly fewer if there are cac
111110
best_friend = graphene.Field(lambda: User)
112111
friends = graphene.List(lambda: User)
113112
114-
def resolve_best_friend(root, info):
115-
return user_loader.load(root.best_friend_id)
113+
async def resolve_best_friend(root, info):
114+
return await user_loader.load(root.best_friend_id)
116115
117-
def resolve_friends(root, info):
118-
return user_loader.load_many(root.friend_ids)
116+
async def resolve_friends(root, info):
117+
return await user_loader.load_many(root.friend_ids)

0 commit comments

Comments
 (0)