#Linked products with pagination

1 messages · Page 1 of 1 (latest)

covert trail
#

Hey

Is it possible to fetch linked products with a certain brand with pagination as it is possible to do if I would be fetching only products or brands.

The solution I have now is not very optimized as it fetches all the products linked with a certain brand instead of just fetching what is needed and then im doing the pagination manually.

const brandId = req.validatedQuery.id || "";
  const productLimit = req.validatedQuery.limit || 15;
  const productOffset = req.validatedQuery.offset || 0;

//getting all the linked products
 const { data: brands } = await query.graph({
        entity: "brand",
        fields: ["*", "products.*"],
        filters: { id: brandId },
      });

// paginate products
      const brand = brands[0];
      const products = brand?.products || [];
      const paginatedProducts = products.slice(
        productOffset,
        productOffset + productLimit
      );
hard forge
covert trail
#

@hard forge thx I was checking this and i didnt find a way to paginate related products. Even though it says "Apply Filters and Pagination on Linked Records" but it is to paginate the links (in my case product-brand) but there was not a way to fetch 1 brand and paginate the products linked to that brand

hard forge
#

I already used it for my case. What is your problem with making it?

covert trail
#

@hard forge it does not seem that you would be fetching any items linked to "my_custom".
My problem is that i can not figure out how to fetch paginated linked items

const { data: brands } = await query.graph({
    entity: "brand", <- MODULE
    fields: ["*", "products.*"], <- FIELDS FROM BRAND TO GET AND LINKED PRODUCTS WHICH I WANT TO PAGINATE
    filters: { id: brandId }, <- ID OF THE BRAND TO GET
});

Now im receiving all the products and if i add metadata (like in your example) it is going to be for that 1 product which im fetching so the count would be 1

hard forge
#

Sorry, my mistake. Why are you querying data with the ‘product’ entity and filtering by brandId?

const { data: products } = await query.graph({
entity: “product”,
fields: [“”, “brand.“],
filters: { brand_id: brandId },
});

covert trail
#

@hard forge i havent thought of this 🙂 I have to try and see if it works 😄 thx