Resolving internal link references in Sanity queries with Astro
Looking at your query, I can see the issue with how you're trying to resolve internal link references in your Portable Text content. The problem is in the structure of your projection - you have an unnecessary nested check that's preventing proper dereferencing.
Here's the corrected query:
*[_type == "page" && slug == "what-is-veterinary-physiotherapy"]{
...,
content[]{
...,
markDefs[]{
...,
_type == "internalLink" => {
"slug": @.reference->slug
}
}
}
}What changed:
- Removed the nested
_type == "reference"check - this was causing the issue - Simplified to just
@.reference->slugto properly dereference
How it works:
- The
@symbol refers to the current element in themarkDefsarray (yourinternalLinkobject) .reference->follows the reference field using GROQ's dereference operator (->)sluggrabs the slug field from the referenced document
Important note: If your slug is stored as an object with a current property (which is common in Sanity), you'll need:
*[_type == "page" && slug == "what-is-veterinary-physiotherapy"]{
...,
content[]{
...,
markDefs[]{
...,
_type == "internalLink" => {
"slug": @.reference->slug.current
}
}
}
}This pattern is documented in Sanity's guide on joining references in Portable Text. The arrow operator (->) is the key to dereferencing and fetching data from related documents in GROQ queries.
If you need to fetch additional fields from the referenced document (like title, or the entire slug object), you can expand the projection:
markDefs[]{
...,
_type == "internalLink" => {
"slug": @.reference->slug.current,
"title": @.reference->title
}
}Show original thread11 replies
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.