Data in Gatsby
Welcome to Part Four of the tutorial! Halfway through! Hope things are startingto feel pretty comfortable 😀
Recap of the first half of the tutorial
So far, you’ve been learning how to use React.js—how powerful it is to be able tocreate your own components to act as custom building blocks for websites.
You’ve also explored styling components using CSS Modules.
What’s in this tutorial?
In the next four parts of the tutorial (including this one), you’ll be diving into the Gatsby data layer, which is a powerful feature of Gatsby that lets you easily build sites from Markdown, WordPress, headless CMSs, and other data sources of all flavors.
NOTE: Gatsby’s data layer is powered by GraphQL. For an in-depth tutorial onGraphQL, we recommend How to GraphQL.
Data in Gatsby
A website has four parts: HTML, CSS, JS, and data. The first half of thetutorial focused on the first three. Now let’s learn how to use data in Gatsbysites.
What is data?
A very computer science-y answer would be: data is things like "strings"
,integers (42
), objects ({ pizza: true }
), etc.
For the purpose of working in Gatsby, however, a more useful answer is“everything that lives outside a React component”.
So far, you’ve been writing text and adding images directly in components.Which is an excellent way to build many websites. But, often you want to storedata outside components and then bring the data into the component asneeded.
If you’re building a site with WordPress (so other contributorshave a nice interface for adding & maintaining content) and Gatsby, the data_for the site (pages and posts) are in WordPress and you _pull that data, asneeded, into your components.
Data can also live in file types like Markdown, CSV, etc. as well as databasesand APIs of all sorts.
Gatsby’s data layer lets you pull data from these (and any other source)directly into your components—in the shape and form you want.
Using Unstructured Data vs GraphQL
Do I have to use GraphQL and source plugins to pull data into Gatsby sites?
Absolutely not! You can use the node createPages
API to pull unstructured data into Gatsby pages directly, rather than through the GraphQL data layer. This is a great choice for small sites, while GraphQL and source plugins can help save time with more complex sites.
See the Using Gatsby without GraphQL guide to learn how to pull data into your Gatsby site using the node createPages
API and to see an example site!
When do I use unstructured data vs GraphQL?
If you’re building a small site, one efficient way to build it is to pull in unstructured data as outlined in this guide, using createPages
API, and then if the site becomes more complex later on, you move on to building more complex sites, or you’d like to transform your data, follow these steps:
- Check out the Plugin Library to see if the source plugins and/or transformer plugins you’d like to use already exist
- If they don’t exist, read the Plugin Authoring guide and consider building your own!
How Gatsby’s data layer uses GraphQL to pull data into components
There are many options for loading data into React components. One of the mostpopular and powerful of these is a technology calledGraphQL.
GraphQL was invented at Facebook to help product engineers pull needed data intocomponents.
GraphQL is a query language (the QL part of its name). If you’refamiliar with SQL, it works in a very similar way. Using a special syntax, you describethe data you want in your component and then that data is givento you.
Gatsby uses GraphQL to enable components to declare the data they need.
Create a new example site
Create another new site for this part of the tutorial. You’re going to build a Markdown blog called “Pandas Eating Lots”. It’s dedicated to showing off the best pictures and videos of pandas eating lots of food. Along the way, you’ll be dipping your toes into GraphQL and Gatsby’s Markdown support.
Open a new terminal window and run the following commands to create a new Gatsby site in a directory called tutorial-part-four
. Then navigate to the new directory:
Then install some other needed dependencies at the root of the project. You’ll use the Typography theme“Kirkham”, and you’ll try out a CSS-in-JS library, “Emotion”:
Set up a site similar to what you ended with in Part Three. This site will have a layout component and two page components:
gatsby-config.js
(must be in the root of your project, not under src)
Add the above files and then run gatsby develop
, per usual, and you should see the following:
You have another small site with a layout and two pages.
Now you can start querying 😋
Your first GraphQL query
When building sites, you’ll probably want to reuse common bits of data — like the site title for example. Look at the /about/
page. You’ll notice that you have the site title (Pandas Eating Lots
) in both the layout component (the site header) as well as in the <h1 />
of the about.js
page (the page header).
But what if you want to change the site title in the future? You’d have to search for the title across all your components and edit each instance. This is both cumbersome and error-prone, especially for larger, more complex sites. Instead, you can store the title in one location and reference that location from other files; change the title in a single place, and Gatsby will pull your updated title into files that reference it.
The place for these common bits of data is the siteMetadata
object in the gatsby-config.js
file. Add your site title to the gatsby-config.js
file:
Restart the development server.
Use a page query
Now the site title is available to be queried; Add it to the about.js
file using a page query:
It worked! 🎉
The basic GraphQL query that retrieves the title
in your about.js
changes above is:
💡 In part five, you’ll meet a tool that lets us interactively explore the data available through GraphQL, and help formulate queries like the one above.
Page queries live outside of the component definition — by convention at the end of a page component file — and are only available on page components.
Use a StaticQuery
StaticQuery is a new API introduced in Gatsby v2 that allows non-page components (like your layout.js
component), to retrieve data via GraphQL queries.Let’s use its newly introduced hook version — useStaticQuery
.
Go ahead and make some changes to your src/components/layout.js
file to use the useStaticQuery
hook and a {data.site.siteMetadata.title}
reference that uses this data. When you are done, your file will look like this:
Another success! 🎉
Why use two different queries here? These examples were quick introductions tothe query types, how they are formatted, and where they can be used. For now,keep in mind that only pages can make page queries. Non-page components, such asLayout, can use StaticQuery. Part 7 of the tutorial explains these in greaterdepth.
But let’s restore the real title.
One of the core principles of Gatsby is that creators need an immediate connection to what they’re creating (hat tip to Bret Victor). In other words, when you make any change to code you should immediately see the effect of that change. You manipulate an input of Gatsby and you see the new output showing up on the screen.
So almost everywhere, changes you make will immediately take effect. Edit the gatsby-config.js
file again, this time changing the title
back to “Pandas Eating Lots”. The change should show up very quickly in your site pages.
What’s coming next?
Next, you’ll be learning about how to pull data into your Gatsby site usingGraphQL with source plugins in part five of thetutorial.