Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): fix dirty check for inference metadata with related nodes #23472

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1145,12 +1145,12 @@ describe(`Type change detection`, () => {
// add/delete to such fields as mutations
let metadata = addOne({ relatedNode___NODE: `added` })
expect(metadata.dirty).toEqual(true)
expect(haveEqualFields(metadata, initialMetadata)).toEqual(true)
expect(haveEqualFields(metadata, initialMetadata)).toEqual(false)
metadata.dirty = false

metadata = deleteOne({ relatedNode___NODE: `added` }, metadata)
expect(metadata.dirty).toEqual(true)
expect(haveEqualFields(metadata, initialMetadata)).toEqual(true)
expect(haveEqualFields(metadata, initialMetadata)).toEqual(false)
})

it(`does not detect when the same node added to the relatedNode field`, () => {
Expand All @@ -1162,12 +1162,12 @@ describe(`Type change detection`, () => {
it(`detects on any change of the relatedNodeList field`, () => {
let metadata = addOne({ relatedNodeList___NODE: [`added`] })
expect(metadata.dirty).toEqual(true)
expect(haveEqualFields(metadata, initialMetadata)).toEqual(true)
expect(haveEqualFields(metadata, initialMetadata)).toEqual(false)
metadata.dirty = false

metadata = deleteOne({ relatedNodeList___NODE: [`added`] }, metadata)
expect(metadata.dirty).toEqual(true)
expect(haveEqualFields(metadata, initialMetadata)).toEqual(true)
expect(haveEqualFields(metadata, initialMetadata)).toEqual(false)
})

it(`does not detect when the same node added to the relatedNodeList field`, () => {
Expand Down
12 changes: 3 additions & 9 deletions packages/gatsby/src/schema/infer/build-example-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,11 @@ const prepareConflictExamples = (
}
const reportedValueMapper = (typeName: ValueType): unknown => {
if (typeName === `relatedNode`) {
// See FIXME in ./inference-metadata.ts
// eslint-disable-next-line
// @ts-ignore
const { nodes } = descriptor.relatedNode
const { nodes } = descriptor.relatedNode ?? { nodes: {} }
return Object.keys(nodes).find(key => nodes[key] > 0)
}
if (typeName === `relatedNodeList`) {
// See FIXME in ./inference-metadata.ts
// eslint-disable-next-line
// @ts-ignore
const { nodes } = descriptor.relatedNodeList
const { nodes } = descriptor.relatedNodeList ?? { nodes: {} }
return Object.keys(nodes).filter(key => nodes[key] > 0)
}
if (typeName === `object`) {
Expand All @@ -174,7 +168,7 @@ const prepareConflictExamples = (

if (isArrayItem) {
// Differentiate conflict examples by node they were first seen in.
// See Caveats section in the header of this file
// See Caveats section in the header of the ./inference-metadata.ts
const groups = groupBy(
conflictingTypes,
type => descriptor[type]?.first || ``
Expand Down
29 changes: 22 additions & 7 deletions packages/gatsby/src/schema/infer/inference-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ const updateValueDescriptor = (
}

const mergeObjectKeys = (
dpropsKeysA: ITypeInfoObject["dprops"] = {},
dpropsKeysB: ITypeInfoObject["dprops"] = {}
dpropsKeysA: object = {},
dpropsKeysB: object = {}
): string[] => {
const dprops = Object.keys(dpropsKeysA)
const otherProps = Object.keys(dpropsKeysB)
Expand Down Expand Up @@ -386,12 +386,27 @@ const descriptorsAreEqual = (
)
)
}
case `relatedNode`:
case `relatedNode`: {
const nodeIds = mergeObjectKeys(
descriptor?.relatedNode?.nodes,
otherDescriptor?.relatedNode?.nodes
)
return nodeIds.every(
id =>
descriptor?.relatedNode?.nodes[id] &&
otherDescriptor?.relatedNode?.nodes[id]
)
}
case `relatedNodeList`: {
// eslint-disable-next-line
// @ts-ignore
// FIXME See comment: https://github.com/gatsbyjs/gatsby/pull/23264#discussion_r410908538
return isEqual(descriptor?.nodes, otherDescriptor?.nodes)
const nodeIds = mergeObjectKeys(
descriptor?.relatedNodeList?.nodes,
otherDescriptor?.relatedNodeList?.nodes
)
return nodeIds.every(
id =>
descriptor?.relatedNodeList?.nodes[id] &&
otherDescriptor?.relatedNodeList?.nodes[id]
)
}
default:
return true
Expand Down