Join us live Sept. 27 – How Sanity and Vercel powered Morning Brew's transformation –>

Fetch the draft or published version of a document

By Daniel Favand

One query to fetch the draft it it exists, and fall back to the published version of a document.

GROQ query

coalesce(
  *[_id == 'drafts.' + $documentId][0], 
  *[_id == $documentId][0]
)

This query uses the coalesce GROQ function to resolve the draft version of a document, and if it doesn't exist, fall back to the published version.

Be sure to supply a documentId as a parameter for the query.

Contributor

Other schemas by author