πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

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