getProductReviews.ts 722 B

12345678910111213141516171819202122232425262728
  1. import { GET_PRODUCT_REVIEWS } from "@/graphql";
  2. import { cachedProductRequest } from "./useCache";
  3. export async function getProductReviews(productId: string) {
  4. try {
  5. const variables = { product_id: Number(productId), first: 10 };
  6. const {data:response} = await cachedProductRequest<any>(
  7. productId,
  8. GET_PRODUCT_REVIEWS,
  9. variables
  10. );
  11. return response?.productReviews?.edges || [];
  12. } catch (error) {
  13. if (error instanceof Error) {
  14. console.error("Error fetching product reviews:", {
  15. message: error.message,
  16. productId,
  17. graphQLErrors: (error as unknown as Record<string, unknown>)
  18. .graphQLErrors,
  19. });
  20. }
  21. return [];
  22. }
  23. }