Skip to content

Commit

Permalink
Merge pull request #5314 from alex-haproff/fix-5313-dataloaders-not-f…
Browse files Browse the repository at this point in the history
…ound

fix(5313-dataloaders-not-found): use SimpleInventory collection to fe…
  • Loading branch information
willopez authored Jul 15, 2019
2 parents f4f09d9 + 10b266d commit 82fabec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ import isEqual from "lodash/isEqual";
*/
export default async function inventoryForProductConfigurations(context, input) {
const { productConfigurations } = input;
const { collections, dataLoaders } = context;

const productVariantIds = productConfigurations.map(({ productVariantId }) => productVariantId);

const inventoryDocs = await context.dataLoaders.SimpleInventoryByProductVariantId.loadMany(productVariantIds);
const inventoryDocs = dataLoaders
? await dataLoaders.SimpleInventoryByProductVariantId.loadMany(productVariantIds)
: await collections.SimpleInventory
.find({
"productConfiguration.productVariantId": { $in: productVariantIds }
})
.limit(productConfigurations.length) // optimize query speed
.toArray();

return productConfigurations.map((productConfiguration) => {
const inventoryDoc = inventoryDocs.find((doc) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ const context = {
}
])
}
},
collections: {
SimpleInventory: {
find: jest.fn(() => ({
toArray() {
return [];
},
limit() {
return this;
}
}))
}
}
};

Expand All @@ -35,3 +47,22 @@ test("calls SimpleInventoryByProductVariantId dataloader with correct product va
await inventoryForProductConfigurations(context, input);
expect(context.dataLoaders.SimpleInventoryByProductVariantId.loadMany).toHaveBeenCalledWith(["variant-2", "variant-1"]);
});

test("calls SimpleInventory.find() on Mongo Collection when DataLoader is not registered", async () => {
const input = {
productConfigurations: [
{
productVariantId: "variant-2"
},
{
productVariantId: "variant-1"
}
]
};

const newContext = { ...context };
delete newContext.dataLoaders;

await inventoryForProductConfigurations(newContext, input);
expect(context.collections.SimpleInventory.find).toHaveBeenCalledWith({ "productConfiguration.productVariantId": { $in: ["variant-2", "variant-1"] } });
});

0 comments on commit 82fabec

Please sign in to comment.