Building large Gatsby themes can result in a large array of configuration options to manage as a theme author. We can also find ourselves wanting the ability to let other themes or users' sites to add additional configuration instances (ex: get more blog posts from this folder and process them as usual).
Let's take a look at an example of this pattern. First, set
up our configuration node type in the Gatsby system using
sourceNodes we'll talk the options configured with
defaults and inject them into the node system as a node.
Note that in this case we've specified a specific known ID we can query. It is best practice to use
createNodeIdfor ids like this and we're doing it this way in this post for some clarity.
Because we've specified a specific ID, and because of
Gatsby's implementation of node ownership, our theme
effectively owns this object and we can be confident it
hasn't been modified. Using
useStaticQuery we can now
access this data inside our components anywhere in our
If we had some field that semantically could be included
multiple times, we could skip the specific ID and query
allNotesConfig with our own merge algorithm. Let's take a
look at a hypothetical
someOption field that is typed as
Querying this gives us a set of nested arrays.