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

fix(gatsby): Resolve all fields before querying with Sift #11149

Merged
merged 2 commits into from
Jan 24, 2019
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
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