How to make a front-end GraphQL query on a page in GatsbyJS

A step by step tutorial on making a front-end GraphQL query in GatsbyJS. Github repository included.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 11/12/2022 at 21:04
Last Updated on 02/15/2023 at 13:57
A flower that represents GraphQL next to one that represents GatsbyJS. Beneath them sits the text "Front-end GraphQL Page Queries".

A step by step tutorial on making a front-end GraphQL query in GatsbyJS. Github repository included.

Please note that GraphQL page queries only work on pages that are programmatically created or which sit within the Pages folder under your src folder within your GatsbyJS project.

The following tutorial walks you through how to make a front-end GraphQL page query in GatsbyJS. The tutorial can be reproduced using our open source starter project, available in the link below.

Download Opensource Starter Project

If you are curious on how to make GraphQL backend queries, programmatically create pages in Gatsby or to create and make use of reusable front-end GraphQL queries, please consult the tutorials below.

How to programmatically create pages using the GraphQL & GatsbyHow to create & use backend GraphQL fragments in GatsbyHow to create & use front-end GraphQL fragments in Gatsby

Tutorial

Please note that this tutorial makes use of our Open Source project. We recommend checking out the tutorial/backend-fragments branch and carrying out the steps outlined below.

git clone git@github.com:delasign/gatsbyjs-typescript-starter.git

An example of this tutorial with a frontend fragment can be found at the tutorial/frontend-fragments branch as well as in the link below.

How to create & use GraphQL fragments in Gatsby

Step One: Import GraphQL from Gatsby

A screenshot of VSCode showing how to import GraphQL. The code to do this is found below.

At the top of your page, import graphql from gatsby using the following line.

import { graphql } from "gatsby";

Step Two: Construct & Export the Query

A screenshot of VSCode showing the GraphQL page query that we made. The sample code is found below.

Please note that GraphQL page queries only work on pages that are programmatically created or which sit within the Pages folder under your src folder within your GatsbyJS project.

At the bottom of your page, construct and export your graphql query.

If you wish to use data passed down from the backend within your query, use a $. For example, we programatically generated pages using the GatsbyJS backend and passed down the slug which we used to narrow down our query by implementing it using $slug.

Sample code of the full tutorial is found at the gist at the bottom of the page.

How to filter for a Craft CMS site in a GraphQL QueryHow to programmatically create pages using the GraphQL & GatsbyJS

Step Three: Consume data

A screenshot of the how the GraphQL Page Query data is made available to GatsbyJS.

The data from your GraphQL page query will be made available in the props under data.

We recommend that you add this to your props typescript type to avoid errors and printing it onto the console or on the page to verify

The code for the full solution is provided below.

Code for our solution

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.

SubscribeInquireView All PostsView All ServicesView All Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe