Skip to content

Commit

Permalink
feat: accept non-string node IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Feb 20, 2021
1 parent e6d5f3c commit c481230
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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),
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit c481230

Please # to comment.