From 7120e5ad4337ac903a683da999c96c51a6029d44 Mon Sep 17 00:00:00 2001 From: Yao Bin Date: Sat, 20 Jan 2018 00:02:59 +0800 Subject: [PATCH] Enable filtering on linked nodes (#3600) Add support to filter on linked nodes. Close #3190 --- .../infer-graphql-input-type-test.js | 29 +++++++++++++++++++ .../src/schema/infer-graphql-input-fields.js | 7 ++++- .../gatsby/src/schema/infer-graphql-type.js | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js index dd11b0e4448d9..aac4e6c5dd031 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js @@ -482,4 +482,33 @@ describe(`GraphQL Input args`, () => { expect(result).toMatchSnapshot() }) + + it(`filters on linked fields`, async () => { + const { store, getNodes } = require(`../../redux`) + const linkedNodeType = new GraphQLObjectType({ + name: `Bar`, + fields: { + id: { type: GraphQLString }, + }, + }) + let types = [{ name: `Bar`, nodeObjectType: linkedNodeType }] + + store.dispatch({ + type: `CREATE_NODE`, + payload: { id: `baz`, internal: { type: `Bar` } }, + }) + + let result = await queryResult( + [...getNodes(), { linked___NODE: `baz`, foo: `bar` }], + ` + { + allNode(filter: { linked: { id: { eq: "baz" } } }) { + edges { node { linked { id } } } + } + } + `, + { types } + ) + expect(result.data.allNode.edges[0].node.linked.id).toEqual(`baz`) + }) }) diff --git a/packages/gatsby/src/schema/infer-graphql-input-fields.js b/packages/gatsby/src/schema/infer-graphql-input-fields.js index fea138023e433..df3f7d381e464 100644 --- a/packages/gatsby/src/schema/infer-graphql-input-fields.js +++ b/packages/gatsby/src/schema/infer-graphql-input-fields.js @@ -19,6 +19,8 @@ const { isEmptyObjectOrArray, } = require(`./data-tree-utils`) +const { findLinkedNode } = require(`./infer-graphql-type`) + import type { GraphQLInputFieldConfig, GraphQLInputFieldConfigMap, @@ -202,7 +204,10 @@ export function inferInputObjectStructureFromNodes({ if (isRoot && EXCLUDE_KEYS[key]) return // Input arguments on linked fields aren't currently supported - if (_.includes(key, `___NODE`)) return + if (_.includes(key, `___NODE`)) { + value = findLinkedNode(value) + ;[key] = key.split(`___`) + } let field = inferGraphQLInputFields({ nodes, diff --git a/packages/gatsby/src/schema/infer-graphql-type.js b/packages/gatsby/src/schema/infer-graphql-type.js index 22a400c24031d..34b5bf75db102 100644 --- a/packages/gatsby/src/schema/infer-graphql-type.js +++ b/packages/gatsby/src/schema/infer-graphql-type.js @@ -252,7 +252,7 @@ function inferFromMapping( } } -function findLinkedNode(value, linkedField, path) { +export function findLinkedNode(value, linkedField, path) { let linkedNode // If the field doesn't link to the id, use that for searching. if (linkedField) {