Skip to content

Commit 3d38af2

Browse files
nickyfaheyGatsbyJS Bot
authored andcommitted
fix(gatsby): create page dependencies from contextual node model methods even if no path is passed (#18650)
1 parent 6841250 commit 3d38af2

File tree

2 files changed

+86
-14
lines changed

2 files changed

+86
-14
lines changed

packages/gatsby/src/schema/__tests__/node-model.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ describe(`NodeModel`, () => {
9797
})
9898
})
9999

100+
it(`creates page dependency when called with context`, () => {
101+
nodeModel.withContext({ path: `/` }).getNodeById({ id: `person2` })
102+
expect(createPageDependency).toHaveBeenCalledTimes(1)
103+
expect(createPageDependency).toHaveBeenCalledWith({
104+
path: `/`,
105+
nodeId: `person2`,
106+
})
107+
})
108+
100109
it(`returns null when no id provided`, () => {
101110
expect(nodeModel.getNodeById()).toBeNull()
102111
expect(nodeModel.getNodeById({})).toBeNull()
@@ -171,6 +180,21 @@ describe(`NodeModel`, () => {
171180
})
172181
})
173182

183+
it(`creates page dependencies when called with context`, () => {
184+
nodeModel
185+
.withContext({ path: `/` })
186+
.getNodesByIds({ ids: [`person3`, `post3`] })
187+
expect(createPageDependency).toHaveBeenCalledTimes(2)
188+
expect(createPageDependency).toHaveBeenCalledWith({
189+
path: `/`,
190+
nodeId: `person3`,
191+
})
192+
expect(createPageDependency).toHaveBeenCalledWith({
193+
path: `/`,
194+
nodeId: `post3`,
195+
})
196+
})
197+
174198
it(`returns empty array when no ids provided`, () => {
175199
expect(nodeModel.getNodesByIds()).toEqual([])
176200
expect(nodeModel.getNodesByIds({})).toEqual([])
@@ -223,6 +247,18 @@ describe(`NodeModel`, () => {
223247
expect(createPageDependency).toHaveBeenCalledTimes(9)
224248
})
225249

250+
it(`creates page dependencies when called with context and connection type`, () => {
251+
nodeModel
252+
.withContext({ path: `/` })
253+
.getAllNodes({ type: `Post` }, { connectionType: `Post` })
254+
expect(createPageDependency).toHaveBeenCalledTimes(1)
255+
})
256+
257+
it(`does not create page dependencies when called with context without connection type`, () => {
258+
nodeModel.withContext({ path: `/` }).getAllNodes()
259+
expect(createPageDependency).toHaveBeenCalledTimes(0)
260+
})
261+
226262
it(`returns empty array when no nodes of type found`, () => {
227263
const result = nodeModel.getAllNodes({ type: `Astronauts` })
228264
expect(result).toEqual([])
@@ -285,6 +321,24 @@ describe(`NodeModel`, () => {
285321
})
286322
})
287323

324+
it(`creates page dependencies when called with context`, async () => {
325+
const type = `Post`
326+
const query = { filter: { frontmatter: { published: { eq: false } } } }
327+
const firstOnly = false
328+
await nodeModel
329+
.withContext({ path: `/` })
330+
.runQuery({ query, firstOnly, type })
331+
expect(createPageDependency).toHaveBeenCalledTimes(2)
332+
expect(createPageDependency).toHaveBeenCalledWith({
333+
path: `/`,
334+
nodeId: `post1`,
335+
})
336+
expect(createPageDependency).toHaveBeenCalledWith({
337+
path: `/`,
338+
nodeId: `post3`,
339+
})
340+
})
341+
288342
it(`creates page dependencies with connection type`, async () => {
289343
const type = `Post`
290344
const query = { filter: { frontmatter: { published: { eq: false } } } }

packages/gatsby/src/schema/node-model.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -410,21 +410,41 @@ class ContextualNodeModel {
410410
})
411411
}
412412

413-
getNodeById(...args) {
414-
return this.nodeModel.getNodeById(...args)
413+
_getFullDependencies(pageDependencies) {
414+
return {
415+
path: this.context.path,
416+
...(pageDependencies || {}),
417+
}
415418
}
416419

417-
getNodesByIds(...args) {
418-
return this.nodeModel.getNodesByIds(...args)
420+
getNodeById(args, pageDependencies) {
421+
return this.nodeModel.getNodeById(
422+
args,
423+
this._getFullDependencies(pageDependencies)
424+
)
419425
}
420426

421-
getAllNodes(...args) {
422-
return this.nodeModel.getAllNodes(...args)
427+
getNodesByIds(args, pageDependencies) {
428+
return this.nodeModel.getNodesByIds(
429+
args,
430+
this._getFullDependencies(pageDependencies)
431+
)
423432
}
424433

425-
runQuery(...args) {
426-
return this.nodeModel.runQuery(...args)
434+
getAllNodes(args, pageDependencies) {
435+
const fullDependencies = pageDependencies
436+
? this._getFullDependencies(pageDependencies)
437+
: null
438+
return this.nodeModel.getAllNodes(args, fullDependencies)
427439
}
440+
441+
runQuery(args, pageDependencies) {
442+
return this.nodeModel.runQuery(
443+
args,
444+
this._getFullDependencies(pageDependencies)
445+
)
446+
}
447+
428448
prepareNodes(...args) {
429449
return this.nodeModel.prepareNodes(...args)
430450
}
@@ -446,12 +466,10 @@ class ContextualNodeModel {
446466
}
447467

448468
trackPageDependencies(result, pageDependencies) {
449-
const fullDependencies = {
450-
path: this.context.path,
451-
...(pageDependencies || {}),
452-
}
453-
454-
return this.nodeModel.trackPageDependencies(result, fullDependencies)
469+
return this.nodeModel.trackPageDependencies(
470+
result,
471+
this._getFullDependencies(pageDependencies)
472+
)
455473
}
456474
}
457475

0 commit comments

Comments
 (0)