
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI can see the issue you're running into! The problem is actually simpler than you might think. Your GROQ query syntax is correct, but the conditional projection with => isn't adding fields to your result because there are no markDefs objects matching your condition or the reference isn't resolving properly.
Let's debug this step by step:
The conditional projection syntax _type == "internalLink" => { "slug": @.reference->slug } is correct according to the official guide you're following. The => operator expands the result into the parent projection when the condition is true.
However, there are several common reasons why you're seeing no results:
The most common issue: you might not have any markDefs with _type == "internalLink" in your content.
Test this first:
*[_type == "post"][0].body[].markDefs[]This will show you the raw structure of your markDefs. Look for objects where _type == "internalLink" exists. If you don't see any, you need to add some internal links in your editor first.
Your schema might use a different field name than reference. Check your annotation schema definition - it could be item, document, ref, or something else.
In Sanity, slug fields are typically objects like {current: "my-slug"}, not strings. Try:
*[_type == "post"]{
...,
body[]{
...,
markDefs[]{
...,
_type == "internalLink" => {
"slug": @.reference->slug.current
}
}
}
}If the referenced document was deleted, @.reference-> returns null. Test with:
*[_type == "post"]{
body[]{
markDefs[]{
...,
_type == "internalLink" => {
"refExists": defined(@.reference->_id),
"slug": @.reference->slug.current
}
}
}
}When you tried this:
markDefs[]{
...,
_type == "internalLink" => {
"something": "test"
}
}If this didn't add anything, it means no markDefs objects have _type == "internalLink". The conditional only executes when the condition is true. If there are no matching objects, nothing gets added.
The => operator works like this: "When this condition is true, expand this object into the parent projection." It doesn't create a new object scope that needs spreading - it merges the result directly into the parent.
So your original query structure is correct:
markDefs[]{
..., // All existing fields
_type == "internalLink" => { // IF this is true, THEN...
"slug": @.reference->slug.current // ...add this field
}
}Start with the debugging queries above to see what's actually in your markDefs, and you'll quickly find where the disconnect is. Most likely, you either don't have any internal link annotations yet, or the field names don't match what you expect.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store