I have a document type page
which contains an array of blocks
that are the references to other object types. When i fetch the data for page
document, it returns draft data for document but not for the references that are in array.
I have written query like this which returns me both drafts and non-drafts data for document:
*[_type == "page" && slug.current == "${querySlug === '/' ? 'home' : querySlug}"] {
_id,
title,
description,
showSubNav,
subNavigation[] -> {slug, title},
blocks[] -> {
...,
heroCardBlock -> {...},
oneColumnContent -> {...},
featuredNews -> {...},
newsCategories[] -> {...},
newsList[] -> {..., selectedCategory -> {...}},
caseStudiesListSource[] -> {...},
}
}
This returns drafts and non-drafts data for page
document and drafts are available for non-referenced blocks e.g title, description, showSubNav etc but for subNavigation and blocks array which are array of references to other object types, draft data is not being returned only published data is returned in query.
My sanity desk structure is like this: Sanity Desk Structure
How can i get draft data for these references?
The dereference operator (
->
) will always return published content only. To get draft content you'd need to use a subquery (and the query must be authenticated, though if you're getting drafts of thepage
type back then that must be the case).It's probably easiest to consider a simpler example, which you can then expand to your larger query. If we consider this:
I'd expect you to get back your published
heroCardBlock
references. This revision should highlight the difference between getting back published and draft references:I don't know what
<HERO_CARD_BLOCK_TYPE>
is but you'll want to specify it to help make this query more efficient. The double caret (^.^.
) is needed to traverse twice up to the proper scope. The[0]
is needed because thedraft
subquery will return an array even though it will always only be one document; specifying this causespublished
anddraft
to be formatted the same.