Alokai

getCategories

Fetch categories.

In turn, it retrieves a list of categories from commercetools by sending a request to its GraphQL API.

By default, it uses the createMyOrderFromCartDefaultQuery GraphQL query

Signature

declare function getCategories(
	context: CommercetoolsIntegrationContext,
	params: CategoryWhereSearch,
	customQuery?: CustomQuery
): Promise<GetCategoryResponse>;

Parameters

NameRequiredTypeDescription
contextRequiredCommercetoolsIntegrationContext
paramsRequiredCategoryWhereSearch
customQueryOptionalCustomQuery

Returns

Returns a representation of the GetCategoriesResponse.

Referenced Types

Examples

Fetching categories. As the getCategory endpoint doesn't use cookie values internally, it's cacheable without passing additional values.

const { categories } = await sdk.commerce.getCategories();

Fetching a single category by catId.

const { categories } = await sdk.commerce.getCategories({
 catId: '12345678-1234-1234-1234-123456789012'
});

Fetching a single category by key.


const { categories } = await sdk.commerce.getCategories({
  key: 'c3'
});

Fetching a single category by slug.

const { categories } = await sdk.commerce.getCategories({
 slug: 'men'
});

Fetching multiple categories by slugs.

const { categories } = await sdk.commerce.getCategories({
 slugs: ['men', 'men-shoes']
});

Fetching multiple categories by catIds.

const { categories } = await sdk.commerce.getCategories({
 catIds: [
   '12345678-1234-1234-1234-123456789012',
   '12345678-1234-1234-1234-123456789013'
 ]
});

Creating a custom GraphQL query for the method in middleware.config.ts.

export const integrations = {
    ct: {
      // ...
      customQueries: {
        'get-categories-custom-query': ({ variables, metadata }) => ({
           variables,
           query: `
             query categories($where: String, $sort: [String!], $limit: Int, $offset: Int, $acceptLanguage: [Locale!]) {
               categories(where: $where, sort: $sort, limit: $limit, offset: $offset) {
                 results {
                   ${metadata}
                 }
               }
             }
           `;
        })
      }
    }
  }
};

Fetching categories and using the customQuery parameter to leverage the custom GraphQL query from the previous example.

const { categories } = await sdk.commerce.getCategories(null, {
  categories: 'get-categories-custom-query',
  metadata: 'id,name(acceptLaguage: $acceptLanguage)'
});

On this page