Use configuration parameters for a Gatsby source plugin

Share this video with your friends

Send Tweet
Published 2 years ago
Updated a year ago

It's possible to pass configuration parameters to your Gatsby plugin. One use case is just allow the plugin to be used multiple times creating different queries that will be available for your site.

To do this you need to refactor the origin plugin code to accept this parameters and update the gatsby-config.js file to set them there.

Matías Hernández: [0:00] It's possible to pass parameters to the plugin from the gatsby-config configuration. One example of use of this is just running the plugin multiple times. To do this, we add the parameters to the function sourceNode as the last argument. We add podcastID, authToken, and name.

[0:19] Now, we'll refactor. I will pull in to use this new parameters. We change the token and the ID podcast variables, and use name as part of the NODE_TYPE to create different queries. Now, on the onCreateNode method, we must modify the conditional log to accept a new parameter name.

[0:40] After that, we add some check on the sourceNode to enforce the required parameters, and we just throw an Error in case that doesn't exist. Finally, we can just change our gatsby-config file to add this parameters.

[0:54] In summary, a source plugin can accept configuration parameters that can be used, for example, to identify the type of node and then using the plugin multiple times.