DataLoader batch_load_fn + load_many not batching in graphene
See original GitHub issueThis test case doesn’t appear to work when using graphene:
https://github.com/syrusakbary/promise/blob/master/tests/test_dataloader.py#L32
The batch load function is called with one key at a time when calling dataloader.load_many([6, 7])
. I wasn’t able to make that test case break by running it many times or with large (100,000) array sizes, which is why I suspect it’s due to an interaction with graphene. It works in v2.0.2
(the pip install promise
version), but not promise/master
.
Here is roughly the code that I’m running:
class HeroLoader(DataLoader):
def batch_load_fn(self, friend_ids):
import logging; logging.error(friend_ids) ########### Console log #2 here
url = '{}/?id={}'.format(
Hero.endpoint,
','.join([str(id) for id in friend_ids])
)
response = requests.get(url)
results = response.json()['results']
return Promise.resolve(results)
class Hero(graphene.ObjectType):
endpoint = '{}/heroes'.format(HOST)
data_loader = HeroLoader()
id = graphene.Int()
name = graphene.String(name='name')
friend_ids = graphene.List(graphene.Int)
friends = graphene.List(partial(lambda: Hero))
def resolve_friends(self, args, context, info):
import logging; logging.error(self.friend_ids) ######### Console log #1 here
heroes_json = self.data_loader.load_many(self.friend_ids)
return heroes_json.then(do_stuff_with_promise......)
The console output of an infringing request looks like this (and stepping through with a debugger yields the same results):
ERROR:root:[6, 7]
ERROR:root:[6]
ERROR:root:[7]
The expected output is
ERROR:root:[6, 7]
ERROR:root:[6, 7]
If you want a test that works with one version of promise, but not the other:
- Use this repo and branch: https://github.com/curiousest/graphql-to-rest/tree/failing-promises
py.test --capture=no -k 'test_batch_requests' -s
Issue Analytics
- State:
- Created 6 years ago
- Comments:10
Top Results From Across the Web
Why does batching with a dataloader not work in a test?
I am using the dataloader library for node.js on a project where I create a graphQL API with Apollo Server. Code. function getClasses(args,...
Read more >dataloader | Yarn - Package Manager
A data loading utility to reduce requests to a backend via batching and caching ... DataLoader is a generic utility to be used...
Read more >Dataloader - Graphene-Python
DataLoader is a generic utility to be used as part of your application's data ... Batching is not an advanced feature, it's DataLoader's...
Read more >Use DataLoader to Batch And Cache Arbitrary MongoDB ...
We implement arbitrary query caching and batching using ... this to support batching any arbitrary MongoDB query—not just queries by ...
Read more >dataloader - npm
This mechanism of batching and caching data requests is certainly not unique to Node.js or JavaScript, it is also the primary motivation for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Try below. It’s copied from our code base with irrelevant parts stripped, so has not been tested.
It uses aiodataloader
Hope this helps.
@tazimmerman unfortunately, I don’t know of any workarounds when using threads.
We moved to using aiodataloader with graphene-django, and starting a new asyncio event loop for every request. I’m not sure if that’s the right way to do it, but batching is working.