Skip to content

Examples

Complete queries for a collection page, a product page, a seller catalog and paginated reviews.

Each example builds a full page with one or two requests. All of them send x-Store-Domain and pass dynamic values in variables.

Collection page

The collection and its first page of products, with facets, in one request. The same $slug feeds both selections:

CollectionPage.graphql
query CollectionPage($slug: String!, $page: Int = 1) {
  collection(slug: $slug) {
    id
    name
    description
    imageUrl
    productCount
  }
  products(input: { collections: [$slug], page: $page, pageSize: 24 }) {
    pageIndex
    totalPages
    totalCount
    products {
      id
      name
      slug
      imageUrl
      price
      compareAtPrice
      isAvailable
    }
    filters {
      id
      label
      type
      values { value label count selected }
    }
  }
}
Variables
{
  "slug": "summer",
  "page": 1
}

To apply a facet, add values such as option:color:blue or attribute:material:cotton to input.filters. The rules are in products.

Product page

Details, related products and the review summary come back in one request:

ProductPage.graphql
query ProductPage($slug: String!) {
  store { name currency }
  product(slug: $slug) {
    id
    name
    description
    imageUrl
    price
    compareAtPrice
    isAvailable
    images { url name order }
    variants {
      id
      sku
      price
      compareAtPrice
      isAvailable
      options { key name value }
    }
    seller { id name slug }
    reviewSummary { averageRating reviewCount }
    related(limit: 4) {
      id
      name
      slug
      imageUrl
      price
      isAvailable
    }
  }
}

Reviews need the product id. Request them in a second call, after you render the details:

ProductReviews.graphql
query ProductReviews($productId: String!, $cursor: String) {
  productReviews(
    productId: $productId
    limit: 10
    cursor: $cursor
    sortBy: "createdOn"
  ) {
    data {
      id
      rating
      title
      content
      reviewerName
      isVerifiedPurchase
      createdOn
    }
    hasNextPage
    nextCursor
    totalCount
  }
}

Seller catalog

The store, the seller profile and the seller's newest products in one request:

SellerCatalog.graphql
query SellerCatalog($slug: String!, $pageSize: Int = 12) {
  store {
    id
    name
    currency
  }
  seller(slug: $slug) {
    id
    name
    slug
    logo
    banner
    tagline
    rating { average totalReviews }
    deliveryPolicy
  }
  products(input: { sellerSlugs: [$slug], pageSize: $pageSize, sort: NEWEST }) {
    totalCount
    products {
      id
      name
      slug
      imageUrl
      price
      isAvailable
    }
  }
}
Variables
{
  "slug": "acme",
  "pageSize": 12
}

Paginate reviews

The first request sends cursor: null. Each following request sends back the nextCursor it received, unchanged:

reviews.js
const document = `
  query ReviewPage($productId: String!, $cursor: String) {
    productReviews(
      productId: $productId
      limit: 20
      cursor: $cursor
      sortBy: "createdOn"
    ) {
      data {
        id
        rating
        title
        content
        reviewerName
        createdOn
      }
      hasNextPage
      nextCursor
    }
  }
`;

async function loadReviewPage(productId, cursor = null) {
  const response = await fetch("https://storefront.ecomiq.pe/graphql", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-Store-Domain": "my-store",
    },
    body: JSON.stringify({
      query: document,
      variables: { productId, cursor },
    }),
  });

  const result = await response.json();

  if (!response.ok || result.errors?.length) {
    throw new Error(result.errors?.[0]?.message ?? `HTTP ${response.status}`);
  }

  return result.data.productReviews;
}

const firstPage = await loadReviewPage("456");

if (firstPage.hasNextPage) {
  const secondPage = await loadReviewPage("456", firstPage.nextCursor);
  console.log(secondPage.data);
}

If you change rating, search or the sort order, start over with cursor: null.

With @ecomiq/core

The SDK validates data with Zod:

collection-page.ts
import { createClient } from "@ecomiq/core";
import { z } from "zod";

const CollectionPageData = z.object({
  collection: z.object({
    id: z.string(),
    name: z.string(),
    productCount: z.number(),
  }),
  products: z.object({
    totalCount: z.number(),
    products: z.array(
      z.object({
        id: z.string(),
        name: z.string(),
        slug: z.string(),
        price: z.number(),
      }),
    ),
  }),
});

const api = createClient({ domain: "my-store" });

const data = await api.graphql(
  `
    query CollectionPage($slug: String!) {
      collection(slug: $slug) { id name productCount }
      products(input: { collections: [$slug], pageSize: 24 }) {
        totalCount
        products { id name slug price }
      }
    }
  `,
  CollectionPageData,
  { slug: "summer" },
);

Next steps

Last updated:

On this page