getProductReviews
Fetch filtered and paginated product reviews.
It fetches a reviews from commercetools by sending a request to its GraphQL API. By default, it uses the getReviewDefaultQuery GraphQL query.
Signature
declare function getProductReviews(
context: CommercetoolsIntegrationContext,
params: GetReviewParams,
customQuery?: CustomQuery
): Promise<GetReviewResponse>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
context | Required | CommercetoolsIntegrationContext | |
params | Required | GetReviewParams | |
customQuery | Optional | CustomQuery |
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.
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.
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.
const { reviews } = await sdk.commerce.getProductReviews({
limit: 20,
where: 'rating = 1'
});Creating a custom GraphQL query for fetching product reviews.
export const 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';
const { reviews } = await sdk.commerce.getProductReviews(undefined, {
reviews: 'get-product-reviews-custom-query',
metadata: `
id
rating
createdAt
lastModifiedAt
`
});