# gatsby-node-helpers

> Gatsby node helper functions to aid node creation.

Latest version **1.2.1** (published 2021-02-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install gatsby-node-helpers
pnpm add gatsby-node-helpers
yarn add gatsby-node-helpers
bun add gatsby-node-helpers
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2021-02-22 |
| First published | 2017-11-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 57.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 57 |
| Author | Angelo Ashmore |
| Maintainers | angeloashmore |
| Keywords | gatsby, gatsby-plugin |

## Links

- npm: https://www.npmjs.com/package/gatsby-node-helpers
- Repository: https://github.com/angeloashmore/gatsby-node-helpers
- npm.io page: https://npm.io/package/gatsby-node-helpers

## Dependencies (2)

- [camel-case](https://npm.io/package/camel-case.md) ^4.1.2
- [pascal-case](https://npm.io/package/pascal-case.md) ^3.1.2

## Recent versions

- 1.2.1 (latest) — 2021-02-22
- 1.2.0 — 2021-02-22
- 1.1.0 — 2021-02-21
- 1.0.3 — 2021-01-06
- 1.0.2 — 2020-12-29
- 1.0.1 — 2020-12-29
- 1.0.0 — 2020-12-29
- 1.0.0-next.1 — 2020-12-29
- 1.0.0-next.0 — 2020-12-29
- 0.3.0 — 2018-05-13
- 0.2.0 — 2018-05-02
- 0.1.4 — 2018-03-16
- 0.1.3 — 2017-11-24
- 0.1.2 — 2017-11-24
- 0.1.1 — 2017-11-24
- … 1 more at https://npm.io/package/gatsby-node-helpers/versions

## README

# gatsby-node-helpers

Gatsby node helper functions to aid node creation. To be used when creating
[Gatsby source plugins](https://www.gatsbyjs.org/docs/create-source-plugin/).

- Automatically adds Gatsby's required node fields such as `contentDigest`
- Namespaces fields conflicting with Gatsby's reserved fields
- Creates portable functions for generating compliant type names and IDs

## Status

[![npm version](https://img.shields.io/npm/v/gatsby-node-helpers?style=flat-square)](https://www.npmjs.com/package/gatsby-node-helpers)
[![Build Status](https://img.shields.io/github/workflow/status/angeloashmore/gatsby-node-helpers/CI?style=flat-square)](https://github.com/angeloashmore/gatsby-node-helpers/actions?query=workflow%3ACI)

## Installation

```
npm install --save gatsby-node-helpers
```

## Quick Guide

Import the named module:

```typescript
import { createNodeHelpers } from 'gatsby-node-helpers'
```

Then call `createNodeHelpers` with options. You will need to pass functions
available in Gatsby's Node APIs, such as `sourceNodes` and
`createSchemaCustomization`.

The following example shows usage in `gatsby-node.js`'s `sourceNodes` API, but
it can be used elsewhere provided the appropriate helper functions are
available.

```typescript
// gatsby-node.ts

import * as gatsby from 'gatsby'
import { createNodeHelpers } from 'gatsby-node-helpers'

export const sourceNodes: gatsby.GatsbyNode['sourceNodes'] = async (
  gatsbyArgs: gatsby.SourceNodesArgs,
  pluginOptions: gatsby.PluginOptions,
) => {
  const { createNodeId, createContentDigest } = gatsbyArgs

  const nodeHelpers = createNodeHelpers({
    typePrefix: 'Shopify',
    createNodeId,
    createContentDigest,
  })
}
```

The result of `createNodeHelpers` includes a factory function named
`createNodeFactory` that should be used to prepare an object just before calling
Gatsby's `createNode`.

The created function will automatically assign Gatsby's required fields, like
`internal` and `contentDigest`, while renaming any conflicting fields.

```typescript
const nodeHelpers = createNodeHelpers({
  typePrefix: 'Shopify',
  createNodeId: gatsbyArgs.createNodeId,
  createContentDigest: gatsbyArgs.createContentDigest,
})

const ProductNode = nodeHelpers.createNodeFactory('Product')
const ProductVariantNode = nodeHelpers.createNodeFactory('ProductVariant')
```

In the above example, we can now pass Product objects to `ProductNode` to
prepare the object for Gatsby's `createNode`.

```typescript
// gatsby-node.ts

import * as gatsby from 'gatsby'
import { createNodeHelpers } from 'gatsby-node-helpers'

export const sourceNodes: gatsby.GatsbyNode['sourceNodes'] = async (
  gatsbyArgs: gatsby.SourceNodesArgs,
  pluginOptions: gatsby.PluginOptions,
) => {
  const { actions, createNodeId, createContentDigest } = gatsbyArgs
  const { createNodes } = actions

  const nodeHelpers = createNodeHelpers({
    typePrefix: 'Shopify',
    createNodeId,
    createContentDigest,
  })

  const ProductNode = nodeHelpers.createNodeFactory('Product')
  const ProductVariantNode = nodeHelpers.createNodeFactory('ProductVariant')

  // `getAllProducts` is an API function that returns all Shopify products.
  const products = await getAllProducts()

  for (const product of products) {
    const node = ProductNode(product)

    // `node` now contains all the fields required by `createNode`.

    createNode(node)
  }
}
```

## API

All functions and types are documented in the source files using
[TSDoc](https://github.com/microsoft/tsdoc) to provide documentation directly in
your editor.

If you editor does not have TSDoc integration, you can read all documentation by
viewing the source files.

---
_Source: https://npm.io/package/gatsby-node-helpers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
