Skip to content

Commit ccc3082

Browse files
authored
fix: querying nodes by id and operator different than eq (#9101)
* make special case condition for querying by id more strict * add basic tests for run-sift * fix typo
1 parent 95c1eac commit ccc3082

File tree

2 files changed

+121
-3
lines changed

2 files changed

+121
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const runSift = require(`../run-sift`)
2+
const {
3+
GraphQLObjectType,
4+
GraphQLNonNull,
5+
GraphQLID,
6+
GraphQLString,
7+
} = require(`graphql`)
8+
const { connectionFromArray } = require(`graphql-skip-limit`)
9+
10+
jest.mock(`../node-tracking`, () => {
11+
return {
12+
trackInlineObjectsInRootNode: () => jest.fn(),
13+
}
14+
})
15+
16+
jest.mock(`../../redux/actions/add-page-dependency`, () => {
17+
return {
18+
createPageDependency: jest.fn(),
19+
}
20+
})
21+
22+
const mockNodes = [
23+
{
24+
id: `id_1`,
25+
string: `foo`,
26+
},
27+
{
28+
id: `id_2`,
29+
string: `bar`,
30+
},
31+
{
32+
id: `id_3`,
33+
string: `baz`,
34+
},
35+
]
36+
37+
jest.mock(`../../redux`, () => {
38+
return {
39+
getNode: id => mockNodes.find(node => node.id === id),
40+
}
41+
})
42+
43+
describe(`run-sift`, () => {
44+
const typeName = `test`
45+
const type = new GraphQLObjectType({
46+
name: typeName,
47+
fields: () => {
48+
return {
49+
id: new GraphQLNonNull(GraphQLID),
50+
string: GraphQLString,
51+
}
52+
},
53+
})
54+
const nodes = mockNodes
55+
56+
describe(`filters by just id correctly`, () => {
57+
it(`eq operator`, async () => {
58+
const args = {
59+
filter: {
60+
id: { eq: `id_2` },
61+
},
62+
}
63+
64+
const resultSingular = await runSift({
65+
type,
66+
nodes,
67+
typeName,
68+
args,
69+
connection: false,
70+
})
71+
72+
const resultConnection = await runSift({
73+
type,
74+
nodes,
75+
typeName,
76+
args,
77+
connection: true,
78+
})
79+
delete resultConnection.totalCount
80+
81+
expect(resultSingular).toEqual(nodes[1])
82+
expect(resultConnection).toEqual(connectionFromArray([nodes[1]], args))
83+
})
84+
85+
it(`non-eq operator`, async () => {
86+
const args = {
87+
filter: {
88+
id: { ne: `id_2` },
89+
},
90+
}
91+
92+
const resultSingular = await runSift({
93+
type,
94+
nodes,
95+
typeName,
96+
args,
97+
connection: false,
98+
})
99+
100+
const resultConnection = await runSift({
101+
type,
102+
nodes,
103+
typeName,
104+
args,
105+
connection: true,
106+
})
107+
delete resultConnection.totalCount
108+
109+
expect(resultSingular).toEqual(nodes[0])
110+
expect(resultConnection).toEqual(
111+
connectionFromArray([nodes[0], nodes[2]], args)
112+
)
113+
})
114+
})
115+
})

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,14 @@ module.exports = ({
143143
})
144144
}
145145

146-
// If the the query only has a filter for an "id", then we'll just grab
147-
// that ID and return it.
146+
// If the the query for single node only has a filter for an "id"
147+
// using "eq" operator, then we'll just grab that ID and return it.
148148
if (
149+
!connection &&
149150
Object.keys(fieldsToSift).length === 1 &&
150-
Object.keys(fieldsToSift)[0] === `id`
151+
Object.keys(fieldsToSift)[0] === `id` &&
152+
Object.keys(siftArgs[0].id).length === 1 &&
153+
Object.keys(siftArgs[0].id)[0] === `$eq`
151154
) {
152155
const nodePromise = resolveRecursive(
153156
getNode(siftArgs[0].id[`$eq`]),

0 commit comments

Comments
 (0)