Skip to content

Commit 6d8b663

Browse files
stefanprobstsidharthachatterjee
authored andcommitted
fix(gatsby): Fix special case id:eq queries for abstract types (#16114)
* Fix special case id:eq queries for abstract types * Fix run-sift tests
1 parent ce56b9d commit 6d8b663

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

packages/gatsby/src/redux/__tests__/run-sift.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,14 @@ describe(`run-sift`, () => {
118118
gqlType,
119119
queryArgs,
120120
firstOnly: true,
121+
nodeTypeNames: [gqlType.name],
121122
})
122123

123124
const resultMany = await runSift({
124125
gqlType,
125126
queryArgs,
126127
firstOnly: false,
128+
nodeTypeNames: [gqlType.name],
127129
})
128130

129131
expect(resultSingular).toEqual([nodes[1]])
@@ -141,12 +143,14 @@ describe(`run-sift`, () => {
141143
gqlType,
142144
queryArgs,
143145
firstOnly: true,
146+
nodeTypeNames: [gqlType.name],
144147
})
145148

146149
const resultMany = await runSift({
147150
gqlType,
148151
queryArgs,
149152
firstOnly: false,
153+
nodeTypeNames: [gqlType.name],
150154
})
151155

152156
// `id-1` node is not of queried type, so results should be empty
@@ -165,12 +169,14 @@ describe(`run-sift`, () => {
165169
gqlType,
166170
queryArgs,
167171
firstOnly: true,
172+
nodeTypeNames: [gqlType.name],
168173
})
169174

170175
const resultMany = await runSift({
171176
gqlType,
172177
queryArgs,
173178
firstOnly: false,
179+
nodeTypeNames: [gqlType.name],
174180
})
175181

176182
expect(resultSingular).toEqual([nodes[2]])
@@ -199,6 +205,7 @@ describe(`run-sift`, () => {
199205
gqlType,
200206
queryArgs,
201207
firstOnly: true,
208+
nodeTypeNames: [gqlType.name],
202209
})
203210

204211
expect(results[0].id).toBe(`id_4`)

packages/gatsby/src/redux/run-sift.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function handleMany(siftArgs, nodes, sort) {
107107
module.exports = (args: Object) => {
108108
const { getNode, getNodesByType } = require(`../db/nodes`)
109109

110-
const { queryArgs, gqlType, firstOnly = false } = args
110+
const { queryArgs, gqlType, firstOnly = false, nodeTypeNames } = args
111111

112112
// If nodes weren't provided, then load them from the DB
113113
const nodes = args.nodes || getNodesByType(gqlType.name)
@@ -121,7 +121,10 @@ module.exports = (args: Object) => {
121121
if (isEqId(firstOnly, fieldsToSift, siftFilter)) {
122122
const node = getNode(siftFilter[0].id[`$eq`])
123123

124-
if (!node || (node.internal && node.internal.type !== gqlType.name)) {
124+
if (
125+
!node ||
126+
(node.internal && !nodeTypeNames.includes(node.internal.type))
127+
) {
125128
return []
126129
}
127130

packages/gatsby/src/schema/extensions/__tests__/interfaces.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,29 @@ describe(`Queryable Node interfaces`, () => {
339339
`\`id\` of type \`ID!\`. Check the type definition of \`WrongInterface\`.`
340340
)
341341
})
342+
343+
it(`works with special case id: { eq: $id } queries`, async () => {
344+
const query = `
345+
{
346+
testInterface(id: { eq: "test1" }) {
347+
id
348+
}
349+
test(id: { eq: "test1" }) {
350+
id
351+
}
352+
}
353+
`
354+
const results = await runQuery(query)
355+
const expected = {
356+
testInterface: {
357+
id: `test1`,
358+
},
359+
test: {
360+
id: `test1`,
361+
},
362+
}
363+
expect(results).toEqual(expected)
364+
})
342365
})
343366

344367
const buildSchema = async () => {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,23 @@ class LocalNodeModel {
171171
// We provide nodes in case of abstract types, because `run-sift` should
172172
// only need to know about node types in the store.
173173
let nodes
174+
let nodeTypeNames
174175
if (isAbstractType(gqlType)) {
175-
const nodeTypeNames = toNodeTypeNames(this.schema, gqlType)
176+
nodeTypeNames = toNodeTypeNames(this.schema, gqlType)
176177
nodes = nodeTypeNames.reduce(
177178
(acc, typeName) => acc.concat(this.nodeStore.getNodesByType(typeName)),
178179
[]
179180
)
181+
} else {
182+
nodeTypeNames = [gqlType.name]
180183
}
181184

182185
const queryResult = await this.nodeStore.runQuery({
183186
queryArgs: query,
184187
firstOnly,
185188
gqlType,
186189
nodes,
190+
nodeTypeNames,
187191
})
188192

189193
let result = queryResult

0 commit comments

Comments
 (0)