Skip to content

Instantly share code, notes, and snippets.

@Zagrebelin
Created December 3, 2020 04:30
Show Gist options
  • Select an option

  • Save Zagrebelin/4d82c50222b5f68916dfc6528ed79c7f to your computer and use it in GitHub Desktop.

Select an option

Save Zagrebelin/4d82c50222b5f68916dfc6528ed79c7f to your computer and use it in GitHub Desktop.

Revisions

  1. Zagrebelin created this gist Dec 3, 2020.
    36 changes: 36 additions & 0 deletions dataloader.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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