Skip to content

Commit

Permalink
fix: make unit tests pass. Solution for deleted entries still needs work
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Jan 6, 2022
1 parent bb9fd37 commit 0be1d7b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
11 changes: 8 additions & 3 deletions packages/gatsby-source-contentful/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,14 @@ describe(`gatsby-node`, () => {
const referenceKey = `${currentContentType.name.toLowerCase()}___NODE`
const reference = references.get(linkId)
const linkedNode = getNode(linkId)
reference[referenceKey] =
reference[referenceKey] || linkedNode[referenceKey] || []
reference[referenceKey].push(nodeId)
reference[referenceKey] = [
...(reference[referenceKey] ||
linkedNode[referenceKey] ||
[]),
]
if (!reference[referenceKey].includes(nodeId)) {
reference[referenceKey].push(nodeId)
}
references.set(linkId, reference)
}
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe(`contentful extend node type`, () => {
spaceId: `k8iqpp6u0ior`,
createdAt: `2021-03-22T10:10:34.647Z`,
updatedAt: `2021-03-22T10:10:34.647Z`,
file: { contentType: `image/png` },
contentType: `image/png`,
title: `Contentful Logo PNG`,
description: ``,
node_locale: `en-US`,
Expand Down
36 changes: 28 additions & 8 deletions packages/gatsby-source-contentful/src/source-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ export async function sourceNodes(
})
})

const deletedIds = [
...currentSyncData.deletedAssets.map(n => n.sys.id),
...currentSyncData.deletedEntries.map(n => n.sys.id),
]

// Update existing entry nodes that weren't updated but that need reverse
// links added.
existingNodes
Expand All @@ -333,15 +338,30 @@ export async function sourceNodes(
const id = generateReferenceId(n)
if (foreignReferenceMap[id]) {
foreignReferenceMap[id].forEach(foreignReference => {
const nodeId = makeId({
...foreignReference,
defaultLocale,
currentLocale: n.sys.locale,
})

// Add reverse links
if (n[foreignReference.name]) {
n[foreignReference.name].push(foreignReference.id)
// It might already be there so we'll uniquify after pushing.
n[foreignReference.name] = _.uniq(n[foreignReference.name])
} else {
// If is one foreign reference, there can always be many.
// Best to be safe and put it in an array to start with.
n[foreignReference.name] = [foreignReference.id]
if (!n[foreignReference.name]) {
n[foreignReference.name] = [nodeId]
return
}

// Very ugly way to remove links to deleted entries
n[foreignReference.name].forEach((id, index) => {
deletedIds.forEach(deletedId => {
if (id.indexOf(deletedId) !== -1) {
n[foreignReference.name].splice(index, 1)
}
})
})

// Link entries if not linked yet
if (!n[foreignReference.name].includes(nodeId)) {
n[foreignReference.name].push(nodeId)
}
})
}
Expand Down

0 comments on commit 0be1d7b

Please sign in to comment.