Skip to content

Commit 6eefdee

Browse files
authored
fix(gatsby): fix filtering by sub-fields of the field with custom resolver (#26644)
1 parent b88f193 commit 6eefdee

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

packages/gatsby/src/schema/__tests__/node-model.js

+53
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,10 @@ describe(`NodeModel`, () => {
558558
{
559559
id: `id1`,
560560
title: `Foo`,
561+
nested: {
562+
foo: `foo1`,
563+
bar: `bar1`,
564+
},
561565
internal: {
562566
type: `Test`,
563567
contentDigest: `0`,
@@ -567,6 +571,10 @@ describe(`NodeModel`, () => {
567571
id: `id2`,
568572
title: `Bar`,
569573
hidden: false,
574+
nested: {
575+
foo: `foo2`,
576+
bar: `bar2`,
577+
},
570578
internal: {
571579
type: `Test`,
572580
contentDigest: `1`,
@@ -582,6 +590,14 @@ describe(`NodeModel`, () => {
582590
store.dispatch({
583591
type: `CREATE_TYPES`,
584592
payload: [
593+
typeBuilders.buildObjectType({
594+
name: `TestNested`,
595+
fields: {
596+
foo: { type: `String` },
597+
bar: { type: `String` },
598+
},
599+
}),
600+
585601
typeBuilders.buildObjectType({
586602
name: `Test`,
587603
interfaces: [`Node`],
@@ -604,6 +620,10 @@ describe(`NodeModel`, () => {
604620
type: `Boolean!`,
605621
resolve: parent => Boolean(parent.hidden),
606622
},
623+
nested: {
624+
type: `TestNested`,
625+
resolve: source => source.nested,
626+
},
607627
},
608628
}),
609629
],
@@ -702,6 +722,39 @@ describe(`NodeModel`, () => {
702722
expect(result[0].id).toBe(`id1`)
703723
expect(result[1].id).toBe(`id2`)
704724
})
725+
726+
it(`merges query caches when filtering by nested field`, async () => {
727+
// See https://github.com/gatsbyjs/gatsby/issues/26056
728+
nodeModel.replaceFiltersCache()
729+
const result1 = await nodeModel.runQuery(
730+
{
731+
query: {
732+
filter: { nested: { foo: { eq: `foo1` } } },
733+
},
734+
firstOnly: false,
735+
type: `Test`,
736+
},
737+
{ path: `/` }
738+
)
739+
const result2 = await nodeModel.runQuery(
740+
{
741+
query: {
742+
filter: { nested: { bar: { eq: `bar2` } } },
743+
},
744+
firstOnly: false,
745+
type: `Test`,
746+
},
747+
{ path: `/` }
748+
)
749+
750+
expect(result1).toBeTruthy()
751+
expect(result1.length).toBe(1)
752+
expect(result1[0].id).toBe(`id1`)
753+
754+
expect(result2).toBeTruthy()
755+
expect(result2.length).toBe(1)
756+
expect(result2[0].id).toBe(`id2`)
757+
})
705758
})
706759

707760
describe(`node tracking`, () => {

packages/gatsby/src/schema/node-model.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,8 @@ const determineResolvableFields = (
748748
schema,
749749
type,
750750
fields,
751-
nodeTypeNames
751+
nodeTypeNames,
752+
isNestedType = false
752753
) => {
753754
const fieldsToResolve = {}
754755
const gqlFields = type.getFields()
@@ -775,7 +776,8 @@ const determineResolvableFields = (
775776
schema,
776777
gqlFieldType,
777778
field,
778-
toNodeTypeNames(schema, gqlFieldType)
779+
toNodeTypeNames(schema, gqlFieldType),
780+
true
779781
)
780782
if (!_.isEmpty(innerResolved)) {
781783
fieldsToResolve[fieldName] = innerResolved
@@ -785,6 +787,11 @@ const determineResolvableFields = (
785787
if (!fieldsToResolve[fieldName] && needsResolve) {
786788
fieldsToResolve[fieldName] = true
787789
}
790+
if (!fieldsToResolve[fieldName] && isNestedType) {
791+
// If parent field needs to be resolved - all nested fields should be added as well
792+
// See https://github.com/gatsbyjs/gatsby/issues/26056
793+
fieldsToResolve[fieldName] = true
794+
}
788795
})
789796
return fieldsToResolve
790797
}

0 commit comments

Comments
 (0)