-
-
Notifications
You must be signed in to change notification settings - Fork 96
Description
Hi and thank you for this awesome library!
I have some issue on FindMany queries. Got a custom field with a resovler which makes another query... so for 200 items found by the FindMany resolver, there will be another 200 queries. This is kinda slow.
I tried wrapping the FindMany resolver to calculate there those custom fields. Using this approach encountered some problems:
- Custom fields get overwritten after being set on wrapResolve in resultPromise.then
- Managed to stop the overwrite by adding them in model schema, not in addFields(), but if resultPromise.then is called with an async function which queries the database, the returned payload doesn't contain the changes
The code for 2nd issue looks like this:
const bulkWrap = ((next) => async (rp) => {
const resultPromise = next(rp);
resultPromise.then(async (payload) => {
const ids = payload.map((item) => item._id.toString());
const res = await Agent.aggregate(... some pipeline).exec();
payload = payload.map((item) => {
item._doc.agentCount = 10;// res[item._id.toString()] ? res[item._id.toString()].count : 0;
return item;
});
return payload;
});
return resultPromise;
});
schemaComposer.Query.addNestedFields({
[`${name}.FindMany`]: TC.getResolver('findMany').wrapResolve(bulkWrap),
});
If I log the payload before return, it prints correctly in the console, but the returned value from the endpoint is still 0(the field is Number). Somehow the 'then' function runs, but payload is not modified accordingly.
Can you advise on how to fill those custom fields at once by using an aggregate database query instead of running a separate query for every document?
Thanks!