Sourcing from the Filesystem
This guide will walk you through sourcing data from the filesystem.
This guide assumes that you have a Gatsby project set up. If you need to set up a project, please reference the Quick Start Guide.
It will also be useful if you are familiar with GraphiQL, a tool that helps you structure your queries correctly.
gatsby-source-filesystem is the Gatsby plugin for creating File nodes from the file system.
Install the plugin at the root of your Gatsby project:
Then add it to your project’s
gatsby-config.js file, and restart the Gatsby development server.
Open up GraphiQL.
If you bring up the autocomplete window, you’ll see:
Hit Enter on
allFile then type Ctrl + Enter to run a
id from the query and bring up the autocomplete again (Ctrl +
Try adding a number of fields to your query, pressing Ctrl + Enter each time to re-run the query. You’ll see something like this:
The result is an array of File “nodes” (node is a fancy name for an object in a “graph”). Each File object has the fields you queried for.
If you have multiple sets of data, you can query specific ones by specifying the
name property from the config object in the
gatsby-config.js file. In this case,
name is set to
You can then update your query using
sourceInstanceName and the value of the
name property in a filter like so.
Once files have been sourced, various “transformer” plugins in the Gatsby ecosystem can then be used to transform File nodes into various other types of data. For example, a JSON file can be sourced using
gatsby-source-filesystem, and then the resulting File nodes can be transformed into JSON nodes using