Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(5313-dataloaders-not-found): use SimpleInventory collection to fe… #5314

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"] } });
});