Update dataloader docs

This commit is contained in:
Jonathan Kim 2020-04-26 13:22:09 +01:00
parent 3e4305259b
commit 380166989d

View File

@ -4,7 +4,7 @@ Dataloader
DataLoader is a generic utility to be used as part of your application's DataLoader is a generic utility to be used as part of your application's
data fetching layer to provide a simplified and consistent API over data fetching layer to provide a simplified and consistent API over
various remote data sources such as databases or web services via batching various remote data sources such as databases or web services via batching
and caching. and caching. It is provided by a seperate package `aiodataloader <https://pypi.org/project/aiodataloader/>`.
Batching Batching
@ -15,22 +15,19 @@ Create loaders by providing a batch loading function.
.. code:: python .. code:: python
from promise import Promise from aiodataloader import DataLoader
from promise.dataloader import DataLoader
class UserLoader(DataLoader): class UserLoader(DataLoader):
def batch_load_fn(self, keys): async def batch_load_fn(self, keys):
# Here we return a promise that will result on the # Here we call a function to return a user for each key in keys
# corresponding user for each key in keys return [get_user(id=key) for key in keys]
return Promise.resolve([get_user(id=key) for key in keys])
A batch loading function accepts a list of keys, and returns a ``Promise`` A batch loading async function accepts a list of keys, and returns a list of ``values``.
which resolves to a list of ``values``.
Then load individual values from the loader. ``DataLoader`` will coalesce all Then load individual values from the loader. ``DataLoader`` will coalesce all
individual loads which occur within a single frame of execution (executed once individual loads which occur within a single frame of execution (executed once
the wrapping promise is resolved) and then call your batch function with all the wrapping event loop is resolved) and then call your batch function with all
requested keys. requested keys.
@ -38,9 +35,11 @@ requested keys.
user_loader = UserLoader() user_loader = UserLoader()
user_loader.load(1).then(lambda user: user_loader.load(user.best_friend_id)) user1 = await user_loader.load(1)
user1_best_friend = await user_loader.load(user1.best_friend_id))
user_loader.load(2).then(lambda user: user_loader.load(user.best_friend_id)) user2 = await user_loader.load(2)
user2_best_friend = await user_loader.load(user2.best_friend_id))
A naive application may have issued *four* round-trips to a backend for the 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
.. code:: python .. code:: python
class UserLoader(DataLoader): class UserLoader(DataLoader):
def batch_load_fn(self, keys): async def batch_load_fn(self, keys):
users = {user.id: user for user in User.objects.filter(id__in=keys)} users = {user.id: user for user in User.objects.filter(id__in=keys)}
return Promise.resolve([users.get(user_id) for user_id in keys]) return [users.get(user_id) for user_id in keys]
``DataLoader`` allows you to decouple unrelated parts of your application without ``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
best_friend = graphene.Field(lambda: User) best_friend = graphene.Field(lambda: User)
friends = graphene.List(lambda: User) friends = graphene.List(lambda: User)
def resolve_best_friend(root, info): async def resolve_best_friend(root, info):
return user_loader.load(root.best_friend_id) return await user_loader.load(root.best_friend_id)
def resolve_friends(root, info): async def resolve_friends(root, info):
return user_loader.load_many(root.friend_ids) return await user_loader.load_many(root.friend_ids)