Alokai

cmsBlocks

Fetch cms blocks.

Signature

declare function cmsBlocks(
	context: Context,
	identifiers: string,
	customQuery?: CustomQuery,
	customHeaders?: CustomHeaders
): Promise<ApolloQueryResult<CmsBlocksQuery>>;

Parameters

NameRequiredTypeDescription
contextRequiredContext
identifiersRequiredstring
customQueryOptionalCustomQuery
customHeadersOptionalCustomHeaders

Referenced Types

Examples

Simple usage:

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

// fetch few cms blocks by their identifiers
const { data } = await sdk.magento.cmsBlocks({
  identifiers: ['id1', 'id2']
});

// result will contain cms blocks with the specified identifiers
data.cmsBlocks.items.forEach(block => console.log(block.identifier));

Creating a custom GraphQL query

module.exports = {
  integrations: {
    magento: {
      customQueries: {
        'cms-blocks-custom-query': ({ variables, metadata }) => ({
           variables,
           query: `
             query cmsBlock($identifiers: [String]) {
               cmsBlocks(identifiers: $identifiers) {
                 items {
                   ${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 title
const customQuery = {
  cmsBlocks: 'cms-blocks-custom-query',
  metadata: {
    fields: 'title'
  }
};

const { data } = await sdk.magento.cmsBlocks({
  identifiers: ['id1', 'id2']
}, customQuery);

// data will contain only block titles

On this page