diff --git a/src/index.ts b/src/index.ts index bc3f84b..0ad11a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,11 +52,17 @@ interface CreateNodeHelpersParams { } /** - * A record that can be globally identified using a combination of `id` and - * `type` fields. + * A value that can be converted to a string using `toString()`. + */ +export interface Stringable { + toString(): string +} + +/** + * A record that can be globally identified using its `id` field. */ export interface IdentifiableRecord { - id: string + id: Stringable // eslint-disable-next-line @typescript-eslint/no-explicit-any [key: string]: any } @@ -144,7 +150,7 @@ export const createNodeHelpers = ({ ): gatsby.NodeInput => { const res = { ...node, - id: createNodeId(...nameParts, node.id), + id: createNodeId(...nameParts, node.id.toString()), internal: { type: createTypeName(...nameParts), contentDigest: gatsbyCreateContentDigest(node), diff --git a/test/index.test.ts b/test/index.test.ts index 441077c..ea35826 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -100,6 +100,16 @@ describe('createNodeFactory', () => { expect(nodeInput.id).toBe(`createNodeId(typePrefix TypeName ${node.id})`) }) + test('accepts non-string node IDs', () => { + const modifiedNode = { ...node, id: [1, 2, 3] } + const modifiedNodeInput = fn(modifiedNode) + + expect(modifiedNodeInput.id).toBe( + `createNodeId(typePrefix TypeName ${modifiedNode.id})`, + ) + expect(modifiedNodeInput.fieldPrefixId).toBe(modifiedNode.id) + }) + test('adds internal field with required Gatsby fields', () => { expect(nodeInput.internal).toEqual({ type: 'TypePrefixTypeName',