Write Complex Graph Queries
Why complex queries matter
Section titled “Why complex queries matter”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.
Multi-condition filtering
Section titled “Multi-condition filtering”Combine multiple conditions in a where clause. By default, all conditions use AND logic — every condition must match.
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
}
} query MultiCategoryArticles {
ArticlePage(
where: {
_or: [
{ Category: { eq: "Technology" } }
{ Category: { eq: "Science" } }
{ Category: { eq: "Engineering" } }
]
}
limit: 20
) {
items {
Headline
Category
}
total
}
} Combining AND and OR
Section titled “Combining AND and OR”Nest _or inside an AND context or combine multiple _or groups for advanced conditions.
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
}
}
} Sorting results
Section titled “Sorting results”Sort by one or more fields using orderBy. Combine with filters to create ordered listings.
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-based pagination
Section titled “Cursor-based pagination”Cursor pagination is the recommended approach for paginating through results. It performs consistently regardless of how deep into the result set you navigate.
query FirstPage {
ArticlePage(
where: { _metadata: { status: { eq: "Published" } } }
orderBy: { _metadata: { published: DESC } }
limit: 10
) {
items {
Headline
Author
_metadata { published url { default } }
}
cursor
total
}
} 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
}
} 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;
} Nested content queries
Section titled “Nested content queries”Query content within content areas, referenced content, and parent-child relationships.
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 }
}
}
}
}
} query ArticleWithRelated {
ArticlePage(
where: {
_metadata: {
key: { eq: "abc-123-def" }
}
}
) {
items {
Headline
ArticleBody { html }
RelatedArticles {
... on ArticlePage {
Headline
_metadata { url { default } }
}
}
}
}
} Locale-aware queries
Section titled “Locale-aware queries”Fetch content in specific languages or across all locales.
query SwedishArticles {
ArticlePage(
locale: sv
where: { _metadata: { status: { eq: "Published" } } }
limit: 10
) {
items {
Headline
_metadata { locale }
}
}
} query AllLocaleArticles {
ArticlePage(
locale: ALL
where: { Headline: { exists: true } }
limit: 20
) {
items {
Headline
_metadata {
locale
url { default }
}
}
}
} Query aliases
Section titled “Query aliases”Use aliases to fetch multiple result sets in a single request, reducing network roundtrips.
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
}
} Try your own queries
Section titled “Try your own queries”Click "Run Query" to execute