@@ -4,7 +4,7 @@ Dataloader
4
4
DataLoader is a generic utility to be used as part of your application's
5
5
data fetching layer to provide a simplified and consistent API over
6
6
various remote data sources such as databases or web services via batching
7
- and caching.
7
+ and caching. It is provided by a separate package ` aiodataloader <https://pypi.org/project/aiodataloader/> `.
8
8
9
9
10
10
Batching
@@ -15,31 +15,31 @@ Create loaders by providing a batch loading function.
15
15
16
16
.. code :: python
17
17
18
- from promise import Promise
19
- from promise.dataloader import DataLoader
18
+ from aiodataloader import DataLoader
20
19
21
20
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]
26
24
27
25
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 ``.
27
+
30
28
31
29
``DataLoader `` will coalesce all individual loads which occur within a
32
- single frame of execution (executed once the wrapping promise is resolved)
30
+ single frame of execution (executed once the wrapping event loop is resolved)
33
31
and then call your batch function with all requested keys.
34
32
35
33
36
34
.. code :: python
37
35
38
36
user_loader = UserLoader()
39
37
40
- 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))
41
40
42
- 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))
43
43
44
44
45
45
A naive application may have issued *four * round-trips to a backend for the
@@ -53,9 +53,9 @@ make sure that you then order the query result for the results to match the keys
53
53
.. code :: python
54
54
55
55
class UserLoader (DataLoader ):
56
- def batch_load_fn (self , keys ):
56
+ async def batch_load_fn (self , keys ):
57
57
users = {user.id: user for user in User.objects.filter(id__in = keys)}
58
- return Promise.resolve( [users.get(user_id) for user_id in keys])
58
+ return [users.get(user_id) for user_id in keys]
59
59
60
60
61
61
``DataLoader `` allows you to decouple unrelated parts of your application without
@@ -110,8 +110,8 @@ leaner code and at most 4 database requests, and possibly fewer if there are cac
110
110
best_friend = graphene.Field(lambda : User)
111
111
friends = graphene.List(lambda : User)
112
112
113
- def resolve_best_friend (root , info ):
114
- 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)
115
115
116
- def resolve_friends (root , info ):
117
- 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