Trouble getting the slug from an internal link in a Sanity query
I can see what's happening! The issue is with where you're placing the dereference operator (->). In your query, you have:
internalLink->{
"slug": slug.current
}This syntax is correct and should work. However, if you're still getting back just _type: reference and _ref, it likely means the dereference isn't happening at all. Here are the most common causes:
1. The reference might not exist at the internalLink level
Looking at your structure, the reference might actually be nested inside the link object. Try moving the -> operator:
link->{
"slug": slug.current
}Or if internalLink is directly inside link:
link {
internalLink->{
"slug": slug.current
}
}2. Make sure you're spreading the button object correctly
Your full query should look like this:
*[_type == "page" && _id == "c57c9277-0669-4bf3-8cba-f6fc16a90955"]{
_id,
_createdAt,
title,
"slug": slug.current,
pageBuilder[]{
_key,
_type,
...,
cta{
preHeading,
heading,
description,
button {
text,
link {
internalLink->{
"slug": slug.current
}
}
}
}
}
}3. Check your schema structure
The reference access operator only works on fields that are actually defined as references in your schema (with type: 'reference'). Make sure your internalLink field is defined as a reference type, not just a regular object.
Quick debugging tip: Try querying just the button/link part to see the raw structure:
*[_type == "page" && _id == "c57c9277-0669-4bf3-8cba-f6fc16a90955"].pageBuilder[].cta.button.linkThis will show you exactly what structure you're working with and where the reference actually lives. Once you see the raw data, you'll know exactly where to place the -> operator.
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.