"How to Build Open-Source Projects That Last in 2026?" Watch our webinar to discover how to build sustainable open-source projects.

How to Retrieve Drupal Nodes in Draft State in Gatsby Development Mode

Discover a powerful technique to include draft nodes in Gatsby projects when integrating with Drupal, ensuring an accurate preview of unpublished content.

By Jesus Manuel Olivas, September, 17, 2020
how-to-retrieve-drupal-nodes-in-draft-state-in-gatsby-development-mode preview

When working with Gatsby and Drupal, it's essential to ensure that your website's content appears exactly as intended, even when dealing with unpublished draft content. But how can you achieve this without compromising your development process? 
In this blog post, we'll unveil a simple yet powerful technique to fetch and display draft nodes using Gatsby seamlessly. 

Using NODE_ENV to Filter Nodes

Using this simple code example, you can include nodes on Draft state only in development "gatsby develop". 

exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
const status = process.env.NODE_ENV === 'production' ? '["1"]' : '["0", "1"]'
return graphql(`
{
drupal {
articles: nodeQuery(
filter: {
conditions: [
{ operator: IN, field: "status", value: ${status} }
{ operator: EQUAL, field: "type", value: ["article"] }
]
}
sort: [{ field: "created", direction: DESC }]
) {
entities {
entityId
entityLabel
... on Drupal_Node {
status
path {
alias
}
}
}
}
}
}
`).then((result) => {
if (result.errors) {
throw result.errors
}
const { articles } = result.data.drupal
articles.entities.forEach((article) => {
createPage({
path: article.path.alias,
component: path.resolve(`./src/templates/article.js`),
context: {
slug: article.path.alias,
id: article.entityId,
},
})
})
})
}

This code sets a value to the status constant based on the process.env.NODE_ENV value and then uses that value to fetch Published nodes on production and Draft + Published nodes while running in development mode.

Wrapping Up

The objective of fetching only published content on production is to hide those nodes while executing the build process and avoid deploying unpublished content.

Efficiently managing content visibility during the development process is crucial for any Gatsby and Drupal project. Using our code example, you can effortlessly include draft nodes only when running in development mode, ensuring a crystal-clear preview of your unpublished content. With this technique, you'll control what is displayed in production while seamlessly testing and refining your website's user experience. Unlock the power of Gatsby and Drupal while keeping unpublished content under wraps, and propel your development workflow to new heights.

Related Content

Decorative

AI vs. Automation: What’s the Difference and How to Choose the Right Layer

By David Cespedes, July, 11, 2026
What is the difference between AI and automation, and how do you decide which layer belongs where? Here’s our practitioner’s decision framework from enterprise pipeline implementations.
Decorative

What Is Business Process Automation and How to Define What to Automate First

By David Cespedes, June, 17, 2026
Business process automation works when you define the right processes first. We discuss how to identify automation candidates and avoid the most common failure modes from our enterprise experience.

This website uses cookies

We use Google Analytics and Hotjar (session recordings and heatmaps) to understand how visitors use our site and improve their experience. These cookies are only activated after you consent. Privacy Policy