Alokai
You are reading the documentation for SDK v2, which is no longer the latest version. Switch to current

getProductReviews

Method fetching filtered and paginated product reviews.

This method sends a GET request to the getReview endpoint of the Vue Storefront API Middleware. In turn, it fetches a reviews from commercetools by sending a request to its GraphQL API. The default GraphQL query used by this method can be found here.

Signature

export declare function getProductReviews<Res extends PartialGetProductReviewsResponse = GetProductReviewsResponse>(
	params: GetProductReviewsParams,
	options?: MethodOptions<CustomQuery<"reviews">>
): Promise<Res>;

Parameters

NameRequiredTypeDescription
paramsRequiredGetProductReviewsParamsParameter object which can be used with this method. Refer to its type definition to learn about possible properties.
optionsOptionalMethodOptions<CustomQuery<"reviews">>Options that can be passed to additionally configure the request or customize the logic in a plugin.

Returns

Returns a representation of the GetProductReviewsResponse.

Referenced Types

Examples

Fetching the first page of product reviews. As the getReview endpoint doesn't use cookie values internally, it's cacheable without passing additional values.

import { sdk } from '~/sdk.config.ts';

const { reviews } = await sdk.commerce.getProductReviews({
  productId: '891c95f8-7bf4-4945-9ab5-00906a5f76ba',
  limit: 20
});

Fetching the third page of product reviews sliced by 10 reviews.

import { sdk } from '~/sdk.config.ts';

const { reviews } = await sdk.commerce.getProductReviews({
  productId: '891c95f8-7bf4-4945-9ab5-00906a5f76ba',
  limit: 10,
  offset: 20,
});

Fetching the first page of product reviews filtered by rating.

import { sdk } from '~/sdk.config.ts';

const { reviews } = await sdk.commerce.getProductReviews({
  limit: 20,
  where: 'rating = 1'
});

Creating a custom GraphQL query for fetching product reviews.

module.exports = {
  integrations: {
    ct: {
      // ...
      customQueries: {
        'get-product-reviews-custom-query': ({ variables, metadata }) => ({
          variables,
          query: `
            query reviews(
              $limit: Int
              $offset: Int
              $sort: [String!]
            ) {
              reviews(
                limit: $limit
                offset: $offset
                sort: $sort
              ) {
                offset
                count
                total
                results {
                  ${metadata}
                }
              }
            }
          `
        }),
      }
    }
  }
};

Fetching product reviews and using the customQuery parameter to leverage the custom GraphQL query from the previous example. Notice how we are using the type parameter to overwrite the default response interface.

import { sdk } from '~/sdk.config.ts';
import { GetProductReviewsResponse } from '@vsf-enterprise/commercetools-types';

type CustomReview = {
  id: string;
  rating: number;
  createdAt: string;
  lastModifiedAt: string;
}

type CustomGetProductReviewsResponse = {
  reviews: Omit<GetProductReviewsResponse, 'reviews'> & {
    results: Array<CustomReview>
  }
}

const { reviews } = await sdk.commerce.getProductReviews<CustomGetProductReviewsResponse>(undefined, {
  customQuery: {
    reviews: 'get-product-reviews-custom-query',
    metadata: `
      id
      rating
      createdAt
      lastModifiedAt
    `
  }
});

On this page