@@ -4,7 +4,7 @@ Dataloader
44DataLoader is a generic utility to be used as part of your application's
55data fetching layer to provide a simplified and consistent API over
66various 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/> `.
88
99
1010Batching
@@ -15,31 +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 ``.
27+
3028
3129``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)
3331and then call your batch function with all requested keys.
3432
3533
3634.. code :: python
3735
3836 user_loader = UserLoader()
3937
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))
4140
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))
4343
4444
4545 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
5353.. code :: python
5454
5555 class UserLoader (DataLoader ):
56- def batch_load_fn (self , keys ):
56+ async def batch_load_fn (self , keys ):
5757 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]
5959
6060
6161 ``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
110110 best_friend = graphene.Field(lambda : User)
111111 friends = graphene.List(lambda : User)
112112
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)
115115
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