Alokai

reviews

Fetch customer reviews

Signature

declare function reviews(
	context: Context,
	searchParams?: CustomerProductReviewParams,
	customQuery?: CustomQuery,
	customHeaders?: CustomHeaders
): Promise<ApolloQueryResult<CustomerQuery>>;

Parameters

NameRequiredTypeDescription
contextRequiredContext
searchParamsOptionalCustomerProductReviewParams
customQueryOptionalCustomQuery
customHeadersOptionalCustomHeaders

Referenced Types

Examples

Simple usage:

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

// fetch reviews, customer must be logged in
const result = await sdk.magento.reviews();

// log all reviews
result?.data?.customer?.reviews?.items.forEach(review => console.log(review));

Creating a custom GraphQL query

module.exports = {
  integrations: {
    magento: {
      customQueries: {
        'customer-product-review-custom-query': ({ variables, metadata }) => ({
           variables,
           query: `
             query reviews($pageSize: Int = 10, $currentPage: Int = 1) {
               customer {
                 reviews(pageSize: $pageSize, currentPage: $currentPage) {
                   ${metadata?.fields}
                 }
               }
             }
           `
        }),
      },
    }
  }
};

Using a custom GraphQL query to reduce the amount of fields returned by the query

import { sdk } from '~/sdk.config.ts';
// reduce the amount of fields returned by the query, when compared to the default query
// fetch only text

const customQuery = {
  reviews: 'customer-product-review-custom-query',
  metadata: {
    fields: 'items { text }'
  }
};

const result = await sdk.magento.reviews({}, customQuery);

// result will only contain the text of the reviews
result?.data?.customer?.reviews?.items.forEach(review => console.log(review.text));

On this page