Skip to main content

Querying Data in Pages with GraphQL

Gatsby’s graphql tag enables page components to retrieve data via a GraphQL query.

In this guide, you will learn how to use the graphql tag in your pages, as well as go a little deeper into how the graphql tag works.

If you’re curious, you can also read more about why Gatsby uses GraphQL.

How to use the graphql tag in pages

Gatsby uses the concept of a page query, which is a query for a specific page in a site. It is unique in that it can take query variables unlike Gatsby’s static queries.

Add description to siteMetadata

The first step in displaying the description will be ensuring you have one to begin with.

Mark up basic index page

A simple index page (src/pages/index.js) can be marked up like so:

Add the graphql query

The first thing to do is import graphql from Gatsby. At the top of index.js add:

Below the HomePage component declaration, export a new constant called query. The name of the constant isn’t important, as Gatsby looks for an exported graphql string from the file rather than a specific variable. Note that you can only have one page query per file.

Then, set the const variable’s value to be a graphql tagged template with the query between two backticks:

The first part of writing the GraphQL query is including the operation (in this case ”query”) along with a name.

From browsing GraphiQL, you’ll find that one of the fields that you can query on is site, which in turn has its own siteMetadata fields that correspond to the data provided in gatsby-config.js.

Putting this together, the completed query looks like:

Provide data to the <HomePage /> component

To start, update the HomePage component to destructure data from props.

The data prop contains the results of the GraphQL query, and matches the shape you would expect. With this in mind, the updated HomePage markup looks like:

After restarting gatsby develop, your home page will now display “This is where I write my thoughts.” from the description set in gatsby-config.js!

How does the graphql tag work?

graphql is a tag function. Behind the scenes Gatsby handles these tags in a particular way:

The short answer

During the Gatsby build process, GraphQL queries are pulled out of the original source for parsing.

The longer answer

The longer answer is a little more involved: Gatsby borrows a technique from Relay that converts your source code into an abstract syntax tree (AST) during the build step. file-parser.js and query-compiler.js pick out your graphql-tagged templates and effectively remove them from the original source code.

More information about how queries work is included in the Gatsby Internals section of the docs.

This means that the graphql tag isn’t executed the way that JavaScript code is typically handled. For example, you cannot use expression interpolation with Gatsby’s graphql tag. However, it’s possible to pass variables into page queries with the context object when creating pages.

How to add query variables to a page query

Variables can be added to page queries (but not static queries) through the context object that is an argument of the createPage API.

Consider the following query:

The MdxBlogPost query will return an MDX node in a site where gatsby-plugin-mdx is installed and .mdx files have been sourced with gatsby-source-filesystem, so long as it matches the argument passed in for a title equaling (eq) the string "Using a Theme".

In addition to hardcoding an argument directly into the page query, you can pass in a variable. The query can be changed to include a variable like this:

When a page is created dynamically from this blog post template in gatsby-node.js, you can provide an object as part of the page’s context. Keys in the context object that match up with arguments in the page query (in this case: "title"), will be used as variables. Variables are prefaced with $, so passing a title property will become $title in the query.

For more information, check out the docs on creating pages programmatically.


Edit this page on GitHub
Docs
Tutorials
Plugins
Blog
Showcase