From 110493c8d13ef2f594dcd450fcc9b799e2c3f1dd Mon Sep 17 00:00:00 2001 From: J J Walwyn Date: Mon, 10 Oct 2022 09:25:08 +0100 Subject: [PATCH] Patch to fix caching on datasource for mongoose models Currently apollo-datasource-mongodb semi-supports mongoose. This is the second patch required to allow caching to work correctly - otherwise bjson throws circular reference errors due to how mongoose hydrates objects. By changing the mongoose integration to always return lean we can fix the conversion. Known Issue: https://github.com/GraphQLGuide/apollo-datasource-mongodb/issues/74 Mongoose Lean: https://mongoosejs.com/docs/tutorials/lean.html --- .../apollo-datasource-mongodb+0.5.4.patch | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/__patches__/apollo-datasource-mongodb+0.5.4.patch b/src/__patches__/apollo-datasource-mongodb+0.5.4.patch index c4d53a7..dfc1083 100644 --- a/src/__patches__/apollo-datasource-mongodb+0.5.4.patch +++ b/src/__patches__/apollo-datasource-mongodb+0.5.4.patch @@ -1,7 +1,21 @@ +diff --git a/node_modules/apollo-datasource-mongodb/dist/cache.js b/node_modules/apollo-datasource-mongodb/dist/cache.js +index a89c686..443ba9c 100644 +--- a/node_modules/apollo-datasource-mongodb/dist/cache.js ++++ b/node_modules/apollo-datasource-mongodb/dist/cache.js +@@ -134,7 +134,7 @@ const createCachingMethods = ({ + $or: filterArray + }; + (0, _helpers.log)('filter: ', filter); +- const findPromise = model ? model.find(filter).exec() : collection.find(filter).toArray(); ++ const findPromise = model ? model.find(filter).lean().exec() : collection.find(filter).toArray(); + const results = await findPromise; + (0, _helpers.log)('results: ', results); + const orderedDocs = orderDocs(fieldsArray, results); diff --git a/node_modules/apollo-datasource-mongodb/index.d.ts b/node_modules/apollo-datasource-mongodb/index.d.ts -index f06b4dd..6c31e07 100644 +index f06b4dd..f486087 100644 --- a/node_modules/apollo-datasource-mongodb/index.d.ts -@@ -39,7 +40,7 @@ declare module 'apollo-datasource-mongodb' { ++++ b/node_modules/apollo-datasource-mongodb/index.d.ts +@@ -39,7 +39,7 @@ declare module 'apollo-datasource-mongodb' { protected collection: Collection protected model: Model