Skip to content

Write Complex Graph Queries

⏱ 30 minutes intermediate
📜CoreGraph

Simple Graph queries fetch a list of items by type. Real-world frontends need filtered listings, combined conditions, sorted results, and efficient pagination. This guide shows you how to build queries that match production requirements.

Combine multiple conditions in a where clause. By default, all conditions use AND logic — every condition must match.

Multi-condition filtering
graphql
query FilteredArticles {
  ArticlePage(
    where: {
      Category: { eq: "Technology" }
      Author: { exists: true }
      _metadata: {
        published: { gte: "2026-01-01T00:00:00Z" }
        status: { eq: "Published" }
      }
    }
    limit: 20
  ) {
    items {
      Headline
      Author
      Category
    }
    total
  }
}
graphql
query MultiCategoryArticles {
  ArticlePage(
    where: {
      _or: [
        { Category: { eq: "Technology" } }
        { Category: { eq: "Science" } }
        { Category: { eq: "Engineering" } }
      ]
    }
    limit: 20
  ) {
    items {
      Headline
      Category
    }
    total
  }
}

Nest _or inside an AND context or combine multiple _or groups for advanced conditions.

Combined AND + OR filtering
graphql
query AdvancedFilter {
  ArticlePage(
    where: {
      _metadata: { status: { eq: "Published" } }
      _or: [
        { Category: { eq: "News" } }
        { Tags: { in: ["featured", "trending"] } }
      ]
      _not: { Author: { eq: "Anonymous" } }
    }
    limit: 20
  ) {
    items {
      Headline
      Category
      Author
      Tags
    }
  }
}

Sort by one or more fields using orderBy. Combine with filters to create ordered listings.

Multi-field sorting
graphql
query SortedArticles {
  ArticlePage(
    where: { _metadata: { status: { eq: "Published" } } }
    orderBy: {
      Category: ASC
      _metadata: { published: DESC }
    }
    limit: 20
  ) {
    items {
      Headline
      Category
      _metadata { published }
    }
  }
}

When using full-text search, sort by _ranking: RELEVANCE to order results by search relevance instead of field values.

Cursor pagination is the recommended approach for paginating through results. It performs consistently regardless of how deep into the result set you navigate.

Cursor pagination pattern
graphql
query FirstPage {
  ArticlePage(
    where: { _metadata: { status: { eq: "Published" } } }
    orderBy: { _metadata: { published: DESC } }
    limit: 10
  ) {
    items {
      Headline
      Author
      _metadata { published url { default } }
    }
    cursor
    total
  }
}
graphql
query NextPage {
  ArticlePage(
    where: { _metadata: { status: { eq: "Published" } } }
    orderBy: { _metadata: { published: DESC } }
    limit: 10
    cursor: "eyJsaW1pdCI6MTAsIm9mZnNldCI6MTB9"
  ) {
    items {
      Headline
      Author
      _metadata { published url { default } }
    }
    cursor
    total
  }
}
javascript
async function fetchAllArticles() {
  let cursor = null;
  const allItems = [];

  do {
    const variables = { limit: 50 };
    if (cursor) variables.cursor = cursor;

    const result = await graphFetch(
      `query ($limit: Int!, $cursor: String) {
        ArticlePage(limit: $limit, cursor: $cursor) {
          items { Headline Author }
          cursor
          total
        }
      }`,
      variables
    );

    allItems.push(...result.data.ArticlePage.items);
    cursor = result.data.ArticlePage.cursor;
  } while (cursor && allItems.length < 200);

  return allItems;
}

Query content within content areas, referenced content, and parent-child relationships.

Content area and reference queries
graphql
query PageWithContentArea {
  LandingPage(
    where: {
      _metadata: {
        url: { default: { eq: "/home" } }
      }
    }
  ) {
    items {
      Title
      HeroArea {
        ... on HeroBannerBlock {
          Heading
          BackgroundImage { url }
          CallToActionUrl
        }
        ... on VideoBlock {
          VideoTitle
          EmbedUrl
        }
      }
      MainContent {
        ... on TextBlock {
          Body { html }
        }
        ... on ImageBlock {
          AltText
          Image { url }
        }
      }
    }
  }
}
graphql
query ArticleWithRelated {
  ArticlePage(
    where: {
      _metadata: {
        key: { eq: "abc-123-def" }
      }
    }
  ) {
    items {
      Headline
      ArticleBody { html }
      RelatedArticles {
        ... on ArticlePage {
          Headline
          _metadata { url { default } }
        }
      }
    }
  }
}

Fetch content in specific languages or across all locales.

Locale queries
graphql
query SwedishArticles {
  ArticlePage(
    locale: sv
    where: { _metadata: { status: { eq: "Published" } } }
    limit: 10
  ) {
    items {
      Headline
      _metadata { locale }
    }
  }
}
graphql
query AllLocaleArticles {
  ArticlePage(
    locale: ALL
    where: { Headline: { exists: true } }
    limit: 20
  ) {
    items {
      Headline
      _metadata {
        locale
        url { default }
      }
    }
  }
}

Use aliases to fetch multiple result sets in a single request, reducing network roundtrips.

Query aliases
graphql
query DashboardData {
  latestNews: ArticlePage(
    where: { Category: { eq: "News" } }
    orderBy: { _metadata: { published: DESC } }
    limit: 5
  ) {
    items { Headline _metadata { published } }
  }

  featuredContent: ArticlePage(
    where: { Tags: { in: ["featured"] } }
    limit: 3
  ) {
    items { Headline Author }
  }

  totalArticles: ArticlePage {
    total
  }
}
Graph API Explorer
Query
Results
Click "Run Query" to execute