mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-25 11:03:58 +03:00
Update dataloader docs
This commit is contained in:
parent
3e4305259b
commit
380166989d
|
@ -4,7 +4,7 @@ Dataloader
|
|||
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
|
||||
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
|
||||
|
@ -15,22 +15,19 @@ Create loaders by providing a batch loading function.
|
|||
|
||||
.. code:: python
|
||||
|
||||
from promise import Promise
|
||||
from promise.dataloader import DataLoader
|
||||
from aiodataloader import DataLoader
|
||||
|
||||
class UserLoader(DataLoader):
|
||||
def batch_load_fn(self, keys):
|
||||
# Here we return a promise that will result on the
|
||||
# corresponding user for each key in keys
|
||||
return Promise.resolve([get_user(id=key) for key in keys])
|
||||
async def batch_load_fn(self, keys):
|
||||
# Here we call a function to return a user for each key in keys
|
||||
return [get_user(id=key) for key in keys]
|
||||
|
||||
|
||||
A batch loading function accepts a list of keys, and returns a ``Promise``
|
||||
which resolves to a list of ``values``.
|
||||
A batch loading async function accepts a list of keys, and returns a list of ``values``.
|
||||
|
||||
Then load individual values from the loader. ``DataLoader`` will coalesce all
|
||||
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.
|
||||
|
||||
|
||||
|
@ -38,9 +35,11 @@ requested keys.
|
|||
|
||||
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
|
||||
|
@ -54,9 +53,9 @@ make sure that you then order the query result for the results to match the keys
|
|||
.. code:: python
|
||||
|
||||
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)}
|
||||
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
|
||||
|
@ -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)
|
||||
friends = graphene.List(lambda: User)
|
||||
|
||||
def resolve_best_friend(root, info):
|
||||
return user_loader.load(root.best_friend_id)
|
||||
async def resolve_best_friend(root, info):
|
||||
return await user_loader.load(root.best_friend_id)
|
||||
|
||||
def resolve_friends(root, info):
|
||||
return user_loader.load_many(root.friend_ids)
|
||||
async def resolve_friends(root, info):
|
||||
return await user_loader.load_many(root.friend_ids)
|
||||
|
|
Loading…
Reference in New Issue
Block a user