Skip to content

Commit

Permalink
fix(gatsby): create page dependencies from contextual node model meth…
Browse files Browse the repository at this point in the history
…ods even if no path is passed (#18650)
  • Loading branch information
nickyfahey authored and GatsbyJS Bot committed Oct 15, 2019
1 parent 6841250 commit 3d38af2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 14 deletions.
54 changes: 54 additions & 0 deletions packages/gatsby/src/schema/__tests__/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ describe(`NodeModel`, () => {
})
})

it(`creates page dependency when called with context`, () => {
nodeModel.withContext({ path: `/` }).getNodeById({ id: `person2` })
expect(createPageDependency).toHaveBeenCalledTimes(1)
expect(createPageDependency).toHaveBeenCalledWith({
path: `/`,
nodeId: `person2`,
})
})

it(`returns null when no id provided`, () => {
expect(nodeModel.getNodeById()).toBeNull()
expect(nodeModel.getNodeById({})).toBeNull()
Expand Down Expand Up @@ -171,6 +180,21 @@ describe(`NodeModel`, () => {
})
})

it(`creates page dependencies when called with context`, () => {
nodeModel
.withContext({ path: `/` })
.getNodesByIds({ ids: [`person3`, `post3`] })
expect(createPageDependency).toHaveBeenCalledTimes(2)
expect(createPageDependency).toHaveBeenCalledWith({
path: `/`,
nodeId: `person3`,
})
expect(createPageDependency).toHaveBeenCalledWith({
path: `/`,
nodeId: `post3`,
})
})

it(`returns empty array when no ids provided`, () => {
expect(nodeModel.getNodesByIds()).toEqual([])
expect(nodeModel.getNodesByIds({})).toEqual([])
Expand Down Expand Up @@ -223,6 +247,18 @@ describe(`NodeModel`, () => {
expect(createPageDependency).toHaveBeenCalledTimes(9)
})

it(`creates page dependencies when called with context and connection type`, () => {
nodeModel
.withContext({ path: `/` })
.getAllNodes({ type: `Post` }, { connectionType: `Post` })
expect(createPageDependency).toHaveBeenCalledTimes(1)
})

it(`does not create page dependencies when called with context without connection type`, () => {
nodeModel.withContext({ path: `/` }).getAllNodes()
expect(createPageDependency).toHaveBeenCalledTimes(0)
})

it(`returns empty array when no nodes of type found`, () => {
const result = nodeModel.getAllNodes({ type: `Astronauts` })
expect(result).toEqual([])
Expand Down Expand Up @@ -285,6 +321,24 @@ describe(`NodeModel`, () => {
})
})

it(`creates page dependencies when called with context`, async () => {
const type = `Post`
const query = { filter: { frontmatter: { published: { eq: false } } } }
const firstOnly = false
await nodeModel
.withContext({ path: `/` })
.runQuery({ query, firstOnly, type })
expect(createPageDependency).toHaveBeenCalledTimes(2)
expect(createPageDependency).toHaveBeenCalledWith({
path: `/`,
nodeId: `post1`,
})
expect(createPageDependency).toHaveBeenCalledWith({
path: `/`,
nodeId: `post3`,
})
})

it(`creates page dependencies with connection type`, async () => {
const type = `Post`
const query = { filter: { frontmatter: { published: { eq: false } } } }
Expand Down
46 changes: 32 additions & 14 deletions packages/gatsby/src/schema/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,21 +410,41 @@ class ContextualNodeModel {
})
}

getNodeById(...args) {
return this.nodeModel.getNodeById(...args)
_getFullDependencies(pageDependencies) {
return {
path: this.context.path,
...(pageDependencies || {}),
}
}

getNodesByIds(...args) {
return this.nodeModel.getNodesByIds(...args)
getNodeById(args, pageDependencies) {
return this.nodeModel.getNodeById(
args,
this._getFullDependencies(pageDependencies)
)
}

getAllNodes(...args) {
return this.nodeModel.getAllNodes(...args)
getNodesByIds(args, pageDependencies) {
return this.nodeModel.getNodesByIds(
args,
this._getFullDependencies(pageDependencies)
)
}

runQuery(...args) {
return this.nodeModel.runQuery(...args)
getAllNodes(args, pageDependencies) {
const fullDependencies = pageDependencies
? this._getFullDependencies(pageDependencies)
: null
return this.nodeModel.getAllNodes(args, fullDependencies)
}

runQuery(args, pageDependencies) {
return this.nodeModel.runQuery(
args,
this._getFullDependencies(pageDependencies)
)
}

prepareNodes(...args) {
return this.nodeModel.prepareNodes(...args)
}
Expand All @@ -446,12 +466,10 @@ class ContextualNodeModel {
}

trackPageDependencies(result, pageDependencies) {
const fullDependencies = {
path: this.context.path,
...(pageDependencies || {}),
}

return this.nodeModel.trackPageDependencies(result, fullDependencies)
return this.nodeModel.trackPageDependencies(
result,
this._getFullDependencies(pageDependencies)
)
}
}

Expand Down

0 comments on commit 3d38af2

Please sign in to comment.