Skip to content

Commit 7120e5a

Browse files
alvinthenKyleAMathews
authored andcommitted
Enable filtering on linked nodes (#3600)
Add support to filter on linked nodes. Close #3190
1 parent 96a418a commit 7120e5a

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,4 +482,33 @@ describe(`GraphQL Input args`, () => {
482482

483483
expect(result).toMatchSnapshot()
484484
})
485+
486+
it(`filters on linked fields`, async () => {
487+
const { store, getNodes } = require(`../../redux`)
488+
const linkedNodeType = new GraphQLObjectType({
489+
name: `Bar`,
490+
fields: {
491+
id: { type: GraphQLString },
492+
},
493+
})
494+
let types = [{ name: `Bar`, nodeObjectType: linkedNodeType }]
495+
496+
store.dispatch({
497+
type: `CREATE_NODE`,
498+
payload: { id: `baz`, internal: { type: `Bar` } },
499+
})
500+
501+
let result = await queryResult(
502+
[...getNodes(), { linked___NODE: `baz`, foo: `bar` }],
503+
`
504+
{
505+
allNode(filter: { linked: { id: { eq: "baz" } } }) {
506+
edges { node { linked { id } } }
507+
}
508+
}
509+
`,
510+
{ types }
511+
)
512+
expect(result.data.allNode.edges[0].node.linked.id).toEqual(`baz`)
513+
})
485514
})

packages/gatsby/src/schema/infer-graphql-input-fields.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const {
1919
isEmptyObjectOrArray,
2020
} = require(`./data-tree-utils`)
2121

22+
const { findLinkedNode } = require(`./infer-graphql-type`)
23+
2224
import type {
2325
GraphQLInputFieldConfig,
2426
GraphQLInputFieldConfigMap,
@@ -202,7 +204,10 @@ export function inferInputObjectStructureFromNodes({
202204
if (isRoot && EXCLUDE_KEYS[key]) return
203205

204206
// Input arguments on linked fields aren't currently supported
205-
if (_.includes(key, `___NODE`)) return
207+
if (_.includes(key, `___NODE`)) {
208+
value = findLinkedNode(value)
209+
;[key] = key.split(`___`)
210+
}
206211

207212
let field = inferGraphQLInputFields({
208213
nodes,

packages/gatsby/src/schema/infer-graphql-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ function inferFromMapping(
252252
}
253253
}
254254

255-
function findLinkedNode(value, linkedField, path) {
255+
export function findLinkedNode(value, linkedField, path) {
256256
let linkedNode
257257
// If the field doesn't link to the id, use that for searching.
258258
if (linkedField) {

0 commit comments

Comments
 (0)