Created
December 3, 2020 04:30
-
-
Save Zagrebelin/4d82c50222b5f68916dfc6528ed79c7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimplyDataloader(DataLoader): | |
def batch_load_fn(self, keys): | |
""" | |
batch_load_fn accept list of keys and should return list of promises. | |
assert len(keys) == len(promises) | |
It calls only one time, with all the keys from the .load(key) calls. | |
""" | |
# heavy calculations will be here. | |
ret = 5 | |
return Promise.resolve([ret]) | |
class Object(graphene.ObjectType): | |
multiply_2 = graphene.Int() | |
multiply_3 = graphene.Int() | |
@classmethod | |
def resolve_multiply_2(cls, root, info, *args, **kwargs): | |
def process_data(data): | |
return data * 2 | |
return SimplyDataloader().load('').then(process_data) | |
@classmethod | |
def resolve_multiply_3(cls, root, info, *args, **kwargs): | |
def process_data(data): | |
return data * 3 | |
return SimplyDataloader().load('').then(process_data) | |
# graphene3 will be supporting more fluent async\await interface: | |
@classmethod | |
async def resolve_multiply_3(cls, root, info, *args, **kwargs): | |
data = await SimplyDataloader().load('') | |
returm data * 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment