Getting slug from internal link reference in Sanity GROQ query

14 replies
Last updated: Nov 29, 2025
Can anyone help? I run this query trying to get the slug from internal link

*[_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
          },
        }
      },
    },
  }
}
But i get back I still only get back _type: reference and _ref: b2971fe5-7509-4630-8519-0c107fe1a876 with no slug? What am i doing wrong here?
AI Update

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.link

This 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.

Show original thread
14 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.

Was this answer helpful?