addProductReview
Method adding new product review.
This method sends a POST request to the addReview endpoint of the Vue Storefront API Middleware. In turn, it adds a review in commercetools by sending a request to its GraphQL API. The default GraphQL query used by this method can be found here. You might also want to see getProductReviews and deleteProductReview methods.
Signature
export declare function addProductReview<Res extends PartialAddReviewResponse = AddReviewResponse>(
params: AddProductReviewParams,
options?: MethodOptions<CustomQuery<"addReview">>
): Promise<Res>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
params | Required | AddProductReviewParams | Parameter object which can be used with this method. Refer to its type definition to learn about possible properties. |
options | Optional | MethodOptions<CustomQuery<"addReview">> | Options that can be passed to additionally configure the request or customize the logic in a plugin. |
Returns
Returns a representation of the AddReviewResponse.
Referenced Types
Examples
Adding a product review.
import { sdk } from '~/sdk.config.ts';
const { review } = await sdk.commerce.addProductReview({
productId: '891c95f8-7bf4-4945-9ab5-00906a5f76ba',
draft: {
authorName: 'John Doe',
text: 'Best product ever',
rating: 5
}
});Adding a product review with title.
import { sdk } from '~/sdk.config.ts';
const { review } = await sdk.commerce.addProductReview({
productId: '891c95f8-7bf4-4945-9ab5-00906a5f76ba',
draft: {
authorName: 'John Doe',
title: 'Incredible',
text: 'Best product ever',
rating: 5
}
});Adding a product rating.
import { sdk } from '~/sdk.config.ts';
const { review } = await sdk.commerce.addProductReview({
productId: '891c95f8-7bf4-4945-9ab5-00906a5f76ba',
draft: {
rating: 5
}
});Creating a custom GraphQL query for adding product reviews.
module.exports = {
integrations: {
ct: {
// ...
customQueries: {
'add-product-review-custom-query': ({ variables, metadata }) => ({
variables,
query: `
mutation createReview($draft: ReviewDraft!) {
review: createReview(draft: $draft) {
${metadata}
}
}
`
}),
}
}
}
};Adding a product review 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. Thanks to that we obtain an access to the ReviewDraft title property.
import { sdk } from '~/sdk.config.ts';
type CustomReview = {
id: string;
title: string;
};
type CustomAddReviewResponse = Record<'review', CustomReview>;
const { review } = await sdk.commerce.addProductReview<CustomAddReviewResponse>({
productId,
draft: {
title
}
}, {
customQuery: {
addReview: 'add-product-review-custom-query',
metadata: `
id
title
`
}
});Adding a product review with Custom Fields. Before using this feature, you have to configure a proper Custom Type in commercetools. Also, the value property must be stringified!
import { sdk } from '~/sdk.config.ts';
const { review } = await sdk.commerce.addProductReview({
productId: '891c95f8-7bf4-4945-9ab5-00906a5f76ba',
draft: {
rating: 5,
custom: {
type: {
key: 'review-custom-fields'
},
fields: [
{ name: 'authorHonorific', value: JSON.stringify('Dr') }
]
}
}
});Adding a product review with Custom Fields as above and using the customQuery parameter leveraging the custom GraphQL query from the previous example to attach them in the response.
import { sdk } from '~/sdk.config.ts';
type CustomReview = {
id: string;
rating: number;
custom: {
customFieldsRaw: {
name: string;
value: string;
}[];
};
};
type CustomAddReviewResponse = Record<'review', CustomReview>;
const { review } = await sdk.commerce.addProductReview<CustomAddReviewResponse>({
productId: '891c95f8-7bf4-4945-9ab5-00906a5f76ba',
draft: {
rating: 5,
custom: {
type: {
key: 'review-custom-fields'
},
fields: [
{ name: 'authorHonorific', value: JSON.stringify('Dr') }
]
}
}
}, {
customQuery: {
addReview: 'add-product-review-custom-query',
metadata: `
id
rating
custom {
customFieldsRaw {
name
value
}
}
`
}
});