Closed nicolasalliaume closed 4 years ago
This is actually the correct behavior! You have a list of inputs that look like ['a', 'b', 'c', 'b']
and GraphQL expects to resolve objects in that order. So what you need to do is:
createBatchResolver(async workEntries => {
const tasks = await Task.find(...).exec();
const taskById = new Map(tasks.map(task => [task._id, task]));
return workEntries.map(workEntry => taskById.get(workEntry.task));
})
🎉 That worked like charm, thank you @calebmer!
Note to someone looking at this: I needed to use .toString() on ObjectIds so they're equal when adding and getting to/from the Map.
Hi 👋, I'm new to GraphQL and ended up here after trying to optimize how the resolver fetches embedded documents (I'm using mongodb+mongoose).
Currently I have the following schema:
There're 1000+ WorkEntry records, and each linked to a Task, which in turns is linked to a Project.
Now, I'm trying to batch the resolution of tasks since I'd like to avoid one mongodb query per WorkEntry to fetch the Task and then another query to fetch the Project.
I tried adding a resolver for WorkEntry as follows:
But this is throwing an error saying it must return the same number of values. Now, this would be correct in my case since I have an "N to 1" relationship (multiple WorkEntry documents may be linked to the same task).
Is there any workaround for this? I'm I completely misunderstanding the purpose of this module?
Thanks!