gatsby-source-prismic
Version:
Gatsby source plugin for building websites using Prismic as a data source
31 lines (28 loc) • 1.03 kB
text/typescript
import * as gatsby from "gatsby";
import * as RTE from "fp-ts/ReaderTaskEither";
import * as I from "fp-ts/Identity";
import { pipe } from "fp-ts/function";
import { IdentifiableRecord } from "gatsby-node-helpers";
import { Dependencies } from "../types";
/**
* Creates a node using the environment's `createNode` function.
*
* By using this function, the record's `id` field must be globally unique. If
* the record's ID may conflict with another's within the application's scope,
* even if it is of a different type, use the standard `createNodeOfType`
* function instead.
*
* @param record - Record with an `id` field with which to create a node.
* @param type - Type of the record.
*/
export const createGloballyUniqueNodeOfType = (
record: IdentifiableRecord,
type: string | string[],
): RTE.ReaderTaskEither<Dependencies, never, gatsby.NodeInput> =>
RTE.asks((deps) =>
pipe(
record,
deps.nodeHelpers.createNodeFactory(type, { idIsGloballyUnique: true }),
I.chainFirst(deps.createNode),
),
);