@@ -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 seperate package ` aiodataloader <https://pypi.org/project/aiodataloader/> `.
8
8
9
9
10
10
Batching
@@ -15,32 +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 ``.
30
27
31
28
Then load individual values from the loader. ``DataLoader `` will coalesce all
32
29
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
34
31
requested keys.
35
32
36
33
37
34
.. code :: python
38
35
39
36
user_loader = UserLoader()
40
37
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))
42
40
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))
44
43
45
44
46
45
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
54
53
.. code :: python
55
54
56
55
class UserLoader (DataLoader ):
57
- def batch_load_fn (self , keys ):
56
+ async def batch_load_fn (self , keys ):
58
57
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]
60
59
61
60
62
61
``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
111
110
best_friend = graphene.Field(lambda : User)
112
111
friends = graphene.List(lambda : User)
113
112
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)
116
115
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