Sanity GROQ vs GraphQL: which query language to use
Compare GROQ and GraphQL in Sanity: zero-deploy schema changes, inline joins, Sanity TypeGen with defineQuery, and when GraphQL still makes sense.
- sanity 6.24.0
- next-sanity 13.5.1
Sanity supports both GROQ (its native query language) and GraphQL. For almost every Next.js or Astro site, pick GROQ: it requires zero deploy step when your schema changes, supports arbitrary projections and joins in one request, and works out of the box with next-sanity and TypeGen.
GROQ vs GraphQL in Sanity
| Aspect | GROQ | GraphQL |
|---|---|---|
| Schema deploy step | None — queries work immediately | Must run sanity graphql deploy after every schema change |
| Joins & references | Inline with -> (author->{name, image}) | Typed nested fields |
| Filtering & slicing | Rich expressions, counts, coalescing (coalesce()) | Limited to generated filter types |
| TypeScript types | sanity typegen generate infers exact query result types | GraphQL Code Generator |
| Best fit | 95% of websites, Next.js, Astro, blogs, SaaS | Teams already running a federated GraphQL gateway (Apollo/Mesh) |
What a real GROQ query looks like
import { defineQuery } from "next-sanity"; export const POSTS_QUERY = defineQuery(` *[_type == "post" && defined(slug.current)] | order(publishedAt desc) [0...12] { _id, title, "slug": slug.current, publishedAt, "author": author->{ name, role } }`);Wrapping the query in defineQuery lets npx sanity typegen generate produce the exact TypeScript return type automatically — giving you the main benefit people historically picked GraphQL for, without maintaining a separate GraphQL schema deployment.
When GraphQL still makes sense
- Your company already stitches multiple backends behind an Apollo or WunderGraph gateway and wants Sanity exposed as one more subgraph.
- Multiple non-JS clients (iOS, Android) already share a strict GraphQL schema contract.
Questions
Is GROQ cheaper than GraphQL on Sanity's API quota?
Both count as API requests against the same quota, but GROQ often uses fewer requests because you can fetch unrelated document types (e.g. site settings + navigation + page content) in a single object projection. Estimate your monthly bill with our Sanity pricing calculator.
Where can I find starter Sanity schemas?
Copy or install our free Sanity content models for posts, authors, FAQs, events and jobs.
Related articles
- SanitySanity published changes not showing on your Next.js sitePublished in Sanity Studio but your live site still shows old content? Fix Next.js caching with revalidateTag webhooks, useCdn: false, dataset mismatches, and drafts.
- SanityRender Sanity Portable Text in Next.js with Tailwind CSS v4Render Sanity Portable Text in Next.js Server Components with @portabletext/react and Tailwind CSS v4: headings, links, custom blocks, and typography.
- SanityHow to deploy Sanity Studio (hosted or self-hosted)npx sanity deploy puts the Studio on *.sanity.studio for free. Set studioHost for unattended deploys, use a deploy token in CI, or host it yourself.
Still stuck? We answer every message ourselves. Write to us