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

Enable filtering on linked nodes #3600

Merged
merged 1 commit into from
Jan 19, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
})
})
7 changes: 6 additions & 1 deletion packages/gatsby/src/schema/infer-graphql-input-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const {
isEmptyObjectOrArray,
} = require(`./data-tree-utils`)

const { findLinkedNode } = require(`./infer-graphql-type`)

import type {
GraphQLInputFieldConfig,
GraphQLInputFieldConfigMap,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/infer-graphql-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down