sanity-groq
Expert assistance for writing, optimizing, and debugging GROQ queries. Use when writing queries or debugging performance.
When & Why to Use This Skill
This Claude skill provides expert assistance for crafting, optimizing, and debugging GROQ queries specifically for Sanity.io. It streamlines the development process by ensuring queries are efficient, secure, and fully compatible with Sanity TypeGen, helping developers maintain high-performance headless CMS architectures through best practices like projections and parameterization.
Use Cases
- Efficient Query Construction: Generate complex GROQ queries using 'defineQuery' and explicit projections to fetch only the data required, minimizing API payload sizes.
- Performance Optimization: Analyze existing queries to identify and fix performance bottlenecks, such as naked projections or excessive deep expansions.
- Security & Debugging: Identify and refactor insecure string interpolation into safe, parameterized queries to prevent injection-like issues and improve cacheability.
- TypeGen Integration: Facilitate type-safe development by generating queries compatible with Sanity TypeGen and providing guidance on importing generated TypeScript types.
| name | sanity-groq |
|---|---|
| description | Expert assistance for writing, optimizing, and debugging GROQ queries. Use when writing queries or debugging performance. |
GROQ Query Assistant
This skill helps users write GROQ queries that are efficient, correct, and compatible with Sanity TypeGen.
Procedure
Analyze Request
- Is the user asking for a new query?
- Is the user debugging an existing query?
- Is the user asking about performance?
Drafting Queries
- Rule: Always wrap in
defineQuery(orgroqtag). - Rule: Always use the
/* groq */comment for syntax highlighting. - Rule: Always use Projections
{ ... }. Never return*(naked projection). - Example:
const QUERY = defineQuery(/* groq */ `*[_type == "post" && slug.current == $slug][0]{ title, body }`);
- Rule: Always wrap in
Optimization Checks
- Scan: Look for
*[_type == "..."]without{}. -> Warn: "Fetch only what you need." - Scan: Look for string interpolation (
slug == "${slug}"). -> Warn: "Use parameters ($slug)." - Scan: Look for deep expansion (
...). -> Warn: "Be explicit to reduce payload size."
- Scan: Look for
TypeGen Integration
- After writing the query, remind the user to run
npm run typegen(or equivalent). - Show how to import the generated type:
import { type QUERYResult } from './sanity.types'.
- After writing the query, remind the user to run