📎 Webclip
How YOU can learn to extend Gatsby further by authoring Plugins
The article says Gatsby plugins extend the framework by adding or transforming data during the build process. It separates source plugins, which fetch data and create nodes, from transformer plugins, which convert existing content into nodes that Gatsby can query.
Reading notes#
- Source plugins run early in the build process and add fetched data to Gatsby’s in-memory data graph as nodes.
- Transformer plugins act on content already in the graph and convert one content type into another, such as JSON or YAML into nodes.
- A plugin can live inside the project or as a separate Node.js library, but Gatsby must be told about it through
gatsby-config.js. - The source plugin demo creates
gatsby-source-swapiwithgatsby-node.jsandpackage.json. - That plugin defines
sourceNodes(), fetches planets fromhttps://swapi.dev/api/planets, and creates nodes withcreateNodeId(),createContentDigest(), andactions.createNode(). - The plugin is registered in
gatsby-config.js, then tested withgatsby developand inspected inhttp://localhost:8000/___graphql. - The transformer demo builds
gatsby-transformer-csvas a separate Node.js project. - It uses
gatsby-source-filesystemto scansrc/csv, thenonCreateNode()filters nodes whose media type istext/csv. - The plugin reads file content with
loadNodeContent(node), parses CSV rows into objects, creates child nodes, and links them to the file node withcreateParentChildLink(). - During development, the article recommends
gatsby cleanbefore rerunninggatsby developso cached data does not hide code changes. - The final result is that CSV file fields become available in GraphQL queries through Gatsby’s node graph.
