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 special case id:eq queries for abstract types #16114

Merged
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
7 changes: 7 additions & 0 deletions packages/gatsby/src/redux/__tests__/run-sift.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ describe(`run-sift`, () => {
gqlType,
queryArgs,
firstOnly: true,
nodeTypeNames: [gqlType.name],
})

const resultMany = await runSift({
gqlType,
queryArgs,
firstOnly: false,
nodeTypeNames: [gqlType.name],
})

expect(resultSingular).toEqual([nodes[1]])
Expand All @@ -141,12 +143,14 @@ describe(`run-sift`, () => {
gqlType,
queryArgs,
firstOnly: true,
nodeTypeNames: [gqlType.name],
})

const resultMany = await runSift({
gqlType,
queryArgs,
firstOnly: false,
nodeTypeNames: [gqlType.name],
})

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

const resultMany = await runSift({
gqlType,
queryArgs,
firstOnly: false,
nodeTypeNames: [gqlType.name],
})

expect(resultSingular).toEqual([nodes[2]])
Expand Down Expand Up @@ -199,6 +205,7 @@ describe(`run-sift`, () => {
gqlType,
queryArgs,
firstOnly: true,
nodeTypeNames: [gqlType.name],
})

expect(results[0].id).toBe(`id_4`)
Expand Down
7 changes: 5 additions & 2 deletions packages/gatsby/src/redux/run-sift.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function handleMany(siftArgs, nodes, sort) {
module.exports = (args: Object) => {
const { getNode, getNodesByType } = require(`../db/nodes`)

const { queryArgs, gqlType, firstOnly = false } = args
const { queryArgs, gqlType, firstOnly = false, nodeTypeNames } = args

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

if (!node || (node.internal && node.internal.type !== gqlType.name)) {
if (
!node ||
(node.internal && !nodeTypeNames.includes(node.internal.type))
) {
return []
}

Expand Down
23 changes: 23 additions & 0 deletions packages/gatsby/src/schema/extensions/__tests__/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,29 @@ describe(`Queryable Node interfaces`, () => {
`\`id\` of type \`ID!\`. Check the type definition of \`WrongInterface\`.`
)
})

it(`works with special case id: { eq: $id } queries`, async () => {
const query = `
{
testInterface(id: { eq: "test1" }) {
id
}
test(id: { eq: "test1" }) {
id
}
}
`
const results = await runQuery(query)
const expected = {
testInterface: {
id: `test1`,
},
test: {
id: `test1`,
},
}
expect(results).toEqual(expected)
})
})

const buildSchema = async () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby/src/schema/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,23 @@ class LocalNodeModel {
// We provide nodes in case of abstract types, because `run-sift` should
// only need to know about node types in the store.
let nodes
let nodeTypeNames
if (isAbstractType(gqlType)) {
const nodeTypeNames = toNodeTypeNames(this.schema, gqlType)
nodeTypeNames = toNodeTypeNames(this.schema, gqlType)
nodes = nodeTypeNames.reduce(
(acc, typeName) => acc.concat(this.nodeStore.getNodesByType(typeName)),
[]
)
} else {
nodeTypeNames = [gqlType.name]
}

const queryResult = await this.nodeStore.runQuery({
queryArgs: query,
firstOnly,
gqlType,
nodes,
nodeTypeNames,
})

let result = queryResult
Expand Down