Alokai

addProductReview

Add a new product review.

By default, it uses the addReviewDefaultQuery GraphQL query

Signature

declare function addProductReview(
	context: CommercetoolsIntegrationContext,
	params: AddReviewParams,
	customQuery?: CustomQuery
): Promise<AddReviewResponse>;

Parameters

NameRequiredTypeDescription
contextRequiredCommercetoolsIntegrationContext
paramsRequiredAddReviewParams
customQueryOptionalCustomQuery

Referenced Types

Examples

Adding a product review.

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.

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.

const { review } = await sdk.commerce.addProductReview({
  productId: '891c95f8-7bf4-4945-9ab5-00906a5f76ba',
  draft: {
    rating: 5
  }
});

Creating a custom GraphQL query for adding product reviews.

export const 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.

const customQuery = {
  addReview: 'add-product-review-custom-query',
  metadata: `
    id
    title
  `
};

const { review } = await sdk.commerce.addProductReview({
  productId,
  draft: {
    title
  }
}, customQuery);

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!

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.

const customQuery = {
  addReview: 'add-product-review-custom-query',
  metadata: `
    id
    rating
    custom {
      customFieldsRaw {
        name
        value
      }
    }
  `
};

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') }
      ]
    }
  }
}, customQuery);

On this page