Skip to content

Commit

Permalink
fix(gatsby): Resolve all fields before querying with Sift (#11149)
Browse files Browse the repository at this point in the history
Fix extractFieldsToSift silently omitting fields that need to be resolved before querying.
  • Loading branch information
stefanprobst authored and freiksenet committed Jan 24, 2019
1 parent 0a8b189 commit d432e2a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 24 deletions.
71 changes: 68 additions & 3 deletions packages/gatsby/src/redux/__tests__/run-sift.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ const mockNodes = [
id: `id_3`,
string: `baz`,
},
{
id: `id_4`,
string: `qux`,
first: {
willBeResolved: `willBeResolved`,
second: {
willBeResolved: `willBeResolved`,
third: {
foo: `foo`,
},
},
},
},
]

jest.mock(`../../db/nodes`, () => {
Expand All @@ -40,8 +53,36 @@ describe(`run-sift`, () => {
name: typeName,
fields: () => {
return {
id: new GraphQLNonNull(GraphQLID),
string: GraphQLString,
id: { type: new GraphQLNonNull(GraphQLID) },
string: { type: GraphQLString },
first: {
type: new GraphQLObjectType({
name: `First`,
fields: {
willBeResolved: {
type: GraphQLString,
resolve: () => `resolvedValue`,
},
second: {
type: new GraphQLObjectType({
name: `Second`,
fields: {
willBeResolved: {
type: GraphQLString,
resolve: () => `resolvedValue`,
},
third: new GraphQLObjectType({
name: `Third`,
fields: {
foo: GraphQLString,
},
}),
},
}),
},
},
}),
},
}
},
})
Expand Down Expand Up @@ -91,7 +132,31 @@ describe(`run-sift`, () => {
})

expect(resultSingular).toEqual([nodes[0]])
expect(resultMany).toEqual([nodes[0], nodes[2]])
expect(resultMany).toEqual([nodes[0], nodes[2], nodes[3]])
})
})

it(`resolves fields before querying`, async () => {
const queryArgs = {
filter: {
first: {
willBeResolved: { eq: `resolvedValue` },
second: {
willBeResolved: { eq: `resolvedValue` },
third: {
foo: { eq: `foo` },
},
},
},
},
}

const results = await runSift({
gqlType,
queryArgs,
firstOnly: true,
})

expect(results[0].id).toBe(`id_4`)
})
})
37 changes: 16 additions & 21 deletions packages/gatsby/src/redux/run-sift.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,20 @@ function siftifyArgs(object) {

// Build an object that excludes the innermost leafs,
// this avoids including { eq: x } when resolving fields.
function extractFieldsToSift(prekey, key, preobj, obj, val) {
if (_.isPlainObject(val)) {
_.forEach((val: any), (v, k) => {
if (k === `elemMatch`) {
// elemMatch is operator for arrays and not field we want to prepare
// so we need to skip it
extractFieldsToSift(prekey, key, preobj, obj, v)
return
}
preobj[prekey] = obj
extractFieldsToSift(key, k, obj, {}, v)
})
} else {
preobj[prekey] = true
}
}
const extractFieldsToSift = filter =>
Object.keys(filter).reduce((acc, key) => {
const value = filter[key]
const k = Object.keys(value)[0]
const v = value[k]
if (key === `elemMatch`) {
acc[k] = extractFieldsToSift(v)
} else if (_.isPlainObject(value) && _.isPlainObject(v)) {
acc[key] = extractFieldsToSift(value)
} else {
acc[key] = true
}
return acc
}, {})

/**
* Parse filter and returns an object with two fields:
Expand All @@ -87,19 +85,16 @@ function extractFieldsToSift(prekey, key, preobj, obj, val) {
*/
function parseFilter(filter) {
const siftArgs = []
const fieldsToSift = {}
let fieldsToSift = {}
if (filter) {
_.each(filter, (v, k) => {
// Ignore connection and sorting args.
if (_.includes([`skip`, `limit`, `sort`], k)) return

siftArgs.push(
siftifyArgs({
[k]: v,
})
)
extractFieldsToSift(``, k, {}, fieldsToSift, v)
})
fieldsToSift = extractFieldsToSift(filter)
}
return { siftArgs, fieldsToSift }
}
Expand Down

0 comments on commit d432e2a

Please sign in to comment.