Fetching draft documents with DocumentFilter is_draft:true outside GraphQL playground

5 replies
Last updated: Apr 28, 2020
Is there anyone that successfully have fetched data with DocumentFilter
is_draft: true
from graphql api outside the graphql playground?
AI Update

Yes, there are people who have successfully fetched data with DocumentFilter is_draft: true from the GraphQL API outside the playground, but they've encountered a significant limitation with nested draft documents.

Based on community discussions, the is_draft filter works well for top-level documents:

allPage(limit: 1, where: {_: { is_draft: $isDraft }, slug: { current: { eq: $slug } } }) {
  title
  slug { current }
}

However, the major issue is with referenced documents. When you have nested documents (references) in your content, the GraphQL API returns the published versions of those references, not the drafts - even when the parent document is fetched as a draft.

The GraphQL documentation confirms that filters like is_draft are available under the _ field, but it doesn't provide a way to filter nested references in GraphQL queries.

The Solution: Use GROQ Instead

Many developers who hit this limitation have switched to GROQ for draft previews. With GROQ, you can fetch draft versions of nested references using the coalesce() function:

*[_type == "page" && slug.current == $slug && _id in path($idMatch)][0] {
  _id,
  title,
  slug,
  'placeholderContent': placeholderContent[] {
    ...coalesce(
      *[_id in ["drafts." + ^._ref]][0],
      *[_id in [^._ref]][0]
    )
  }
}

This pattern checks for the draft version first (drafts.** prefix), then falls back to the published version if no draft exists.

Alternative: Use Perspectives

Another approach is to use Sanity's Perspectives feature with the GraphQL API. With the previewDrafts perspective, you can query content as if drafts were published:

# Set perspective parameter to 'previewDrafts' in your API request
# https://YOUR_PROJECT_ID.api.sanity.io/vX/graphql/YOUR_DATASET/default?perspective=previewDrafts

However, note that with API version 2025-02-19, the default perspective changed from raw to published, so you'll need to explicitly set the perspective parameter.

Bottom line: While the GraphQL is_draft filter works for top-level documents, it has limitations with nested references. For comprehensive draft preview functionality including nested content, GROQ is currently the most robust solution, though using the previewDrafts perspective with GraphQL may also work depending on your use case.

Show original thread
5 replies
Are you sure the requests are authenticated?
I pass a
Authorization
header with bearer tokenlike this


const response = await <http://axios.post|axios.post>(`${process.env.GRAPHQL_URL}`, {
		query: print(query),
		variables: variables,
		headers: {
			'Content-Type': 'application/json',
			'Authorization': `Bearer ${process.env.GRAPHQL_TOKEN}`
		}
	});
What kind of token is the GRAPHQL_TOKEN?
it's token of type read, but I think the issue is in my code, I've noticed that postman have support for graphql &amp; I could successfully do the request in there, so trying to figure out a way around with axios
Ok,change the code to the following did the trick, thanks anyway!

const response = await axios({
		method: 'post',
		url: `${process.env.GRAPHQL_URL}`,
		headers: {
			'Content-Type': 'application/json',
			Authorization: `Bearer ${process.env.GRAPHQL_TOKEN}`
		},
		data: {
			query: print(query),
			variables: variables,
		}
	});

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?