Discussion on using GROQ vs GraphQL for Sanity.io, with GROQ being recommended for its capabilities.
Happy Friday! đ
GraphQL definitely has solid support in Sanity, but GROQ is the first-class, native query language and will generally give you a smoother experience. Here's the breakdown:
Why GROQ is "first-class"
- Native integration: GROQ was built specifically for Sanity and works seamlessly with all features
- More flexible: GROQ handles complex content structures like Portable Text better than GraphQL
- Real-time features: The Vision plugin gives you an integrated playground right in your Studio
- Type generation: Full TypeScript support via Sanity TypeGen
- Perspectives support: Full support for drafts, published content, and content releases
GraphQL limitations to know about
While Sanity does support GraphQL, there are some constraints:
- Schema strictness: Your Sanity schema needs to be more rigid for GraphQL. Anonymous object types have to be "lifted" to the top-level scope with explicit names.
- No subscriptions: The GraphQL API doesn't support subscriptions
- Manual deployment: You need to run
sanity graphql deployvia CLI whenever your schema changesâit doesn't auto-update - Perspectives: While GraphQL supports the
published,raw,drafts, and release perspectives via query parameters, it's not as seamlessly integrated as with GROQ - No mutations: GraphQL is read-only; you'll still use Sanity's Mutation API for writes
When GraphQL makes sense
- You're already invested in the GraphQL ecosystem
- You need strict typing at the API level
- Your team is more comfortable with GraphQL than learning GROQ
- You're using tools that specifically require GraphQL
The verdict
GROQ is definitely the recommended pathâSanity's docs even suggest giving GROQ a try before reaching for GraphQL. That said, GraphQL support is production-ready and maintained, so it's not a dealbreaker if you prefer it. You'll just have a bit more friction with schema management and miss out on some of the more flexible querying patterns.
Since it's not a dealbreaker for you either way, I'd recommend starting with GROQâit's more powerful for Sanity's use cases and has better DX integration. You can always add GraphQL later if you need it for specific integrations!
Sanity â Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.