How to extract URL from referenced schema in Sanity?
The issue you're experiencing is expected behavior - reference fields in Sanity only store the _ref and _type by default. To get the actual data from the referenced document (including any URL/slug fields), you need to dereference the reference in your GROQ query using the -> operator.
Here's how to resolve your linkToNews reference:
*[_type == "yourDocumentType"] {
...,
linkToNews->
}The -> operator tells GROQ to fetch the full referenced document. So if your nouvelles document has a slug field, you'll now get access to it.
If you only need specific fields from the referenced document (like just the slug for building a URL), you can be more selective:
*[_type == "yourDocumentType"] {
...,
linkToNews->{
title,
slug
}
}Or if you need to construct a full URL path:
*[_type == "yourDocumentType"] {
...,
"newsUrl": "/news/" + linkToNews->slug.current
}The reference access operator documentation has more details, but the key takeaway is: references are just pointers until you explicitly dereference them with -> in your queries.
If you're working with arrays of references, you'd do:
{
newsLinks[]->
}This pattern works whether you're querying from the CLI, using the HTTP API, or fetching data in your frontend with @sanity/client or framework integrations like next-sanity.
Show original thread4 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.