Skip to content

Commit 24d44e3

Browse files
committed
Recursively get all the products to fix the fact the product page is only showing the first 250 products
1 parent 8231375 commit 24d44e3

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

web/api-modules/products/use-cases/get-products.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ const toProduct = (product) => ({
1111
}))
1212
});
1313

14-
async function findProducts(client) {
14+
async function findProducts(client, after) {
1515
const response = await client.request(`
16-
{
17-
products(first: 250, sortKey: TITLE) {
16+
query findProducts($after: String) {
17+
products(first: 250, after: $after, sortKey: TITLE) {
1818
edges {
1919
node {
2020
id
@@ -59,19 +59,29 @@ async function findProducts(client) {
5959
}
6060
}
6161
}
62-
}
62+
},
63+
pageInfo {
64+
hasPreviousPage
65+
hasNextPage
66+
startCursor
67+
endCursor
68+
}
6369
}
6470
}
65-
`);
71+
`, { variables: { after } });
6672

6773
if (response.errors) {
6874
console.error('Failed to load Products', JSON.stringify(response.errors));
6975
throw new Error('Failed to load Products');
7076
}
7177

72-
return response?.data?.products?.edges.map(({ node: product }) =>
73-
toProduct(product)
74-
);
78+
const thisPage = response?.data?.products?.edges.map(({ node: product }) =>
79+
toProduct(product));
80+
81+
if (response?.data?.products?.pageInfo.hasNextPage) {
82+
const rest = await findProducts(client, response?.data?.products?.pageInfo.endCursor);
83+
return [...thisPage, ...rest];
84+
} return thisPage;
7585
}
7686

7787
const getProducts = async ({ session }) => {

0 commit comments

Comments
 (0)