Member-only story

Gatsby.js — Conditional Queries and Create Pages

John Au-Yeung
3 min readFeb 8, 2021

--

Photo by Olga Tutunaru on Unsplash

Gatsby is a static web site framework that’s based on React.

We can use it to create static websites from external data sources and more.

In this article, we’ll look at how to create a site with Gatsby.

Conditionals

We can add conditional operations with our GraphQL queries.

For example, we can with:

query GetBlogPosts($withDate: Boolean = false) {
allMarkdownRemark(limit: 3, skip: 1) {
edges {
node {
frontmatter {
title
date @include(if: $withDate)
}
}
}
}
}

We use the $withDate boolean value in our if operator.

Creating and Modifying Pages

We can use GraphQL queries to create pages in the gatsby-node.js file.

For example, we can write:

gatsby-node.js

exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql(
`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
frontmatter {
path
}
}
}
}
}
`
)
if…

--

--

No responses yet