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

Support the in operator for an array of objects #6315

Merged
merged 6 commits into from
Jul 11, 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 @@ -130,6 +130,22 @@ describe(`GraphQL Input args`, () => {
circle: `happy`,
},
boolean: false,
data: {
tags: [
{
tag: {
document: [
{
data: {
tag: `Design System`,
},
number: 3,
},
],
},
},
],
},
},
{
index: 2,
Expand All @@ -149,6 +165,33 @@ describe(`GraphQL Input args`, () => {
blue: 10010,
circle: `happy`,
},
data: {
tags: [
{
tag: {
document: [
{
data: {
tag: `Gatsby`,
},
},
],
},
},
{
tag: {
document: [
{
data: {
tag: `Design System`,
},
number: 5,
},
],
},
},
],
},
},
]

Expand Down Expand Up @@ -505,18 +548,45 @@ describe(`GraphQL Input args`, () => {
expect(result.data.allNode.edges[0].node.name).toEqual(`The Mad Wax`)
})

it(`handles the glob operator`, async () => {
it(`handles the in operator for array of objects`, async () => {
let result = await queryResult(
nodes,
`
{
allNode(limit: 10, filter: {name: { glob: "*Wax" }}) {
edges { node { name }}
test1:allNode(filter: {data: {tags: {elemMatch: {tag: {document: {elemMatch: {data: {tag: {eq: "Gatsby"}}}}}}}}}) {
edges { node { index }}
}
test2:allNode(filter: {data: {tags: {elemMatch: {tag: {document: {elemMatch: {data: {tag: {eq: "Design System"}}}}}}}}}) {
edges { node { index }}
}
test3:allNode(filter: {data: {tags: {elemMatch: {tag: {document: {elemMatch: {number: {lt: 4}}}}}}}}) {
edges { node { index }}
}
}
`
)
expect(result.errors).not.toBeDefined()
expect(result.data.test1.edges.length).toEqual(1)
expect(result.data.test1.edges[0].node.index).toEqual(2)
expect(result.data.test2.edges.length).toEqual(2)
expect(result.data.test2.edges[0].node.index).toEqual(1)
expect(result.data.test2.edges[1].node.index).toEqual(2)
expect(result.data.test3.edges.length).toEqual(1)
expect(result.data.test3.edges[0].node.index).toEqual(1)
})

it(`handles the glob operator`, async () => {
let result = await queryResult(
nodes,
`
{
allNode(limit: 10, filter: {name: { glob: "*Wax" }}) {
edges { node { name }}
}
}
`
)
expect(result.errors).not.toBeDefined()
expect(result.data.allNode.edges.length).toEqual(2)
expect(result.data.allNode.edges[0].node.name).toEqual(`The Mad Wax`)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,21 @@ function convertToInputFilter(
const innerFilter = convertToInputFilter(`${prefix}ListElem`, innerType)
const innerFields = innerFilter ? innerFilter.getFields() : {}

return new GraphQLInputObjectType({
name: createTypeName(`${prefix}QueryList`),
fields: {
let fields
if (innerType instanceof GraphQLInputObjectType) {
fields = {
elemMatch: { type: innerFilter },
}
} else {
fields = {
...innerFields,
in: { type: new GraphQLList(innerType) },
},
}
}

return new GraphQLInputObjectType({
name: createTypeName(`${prefix}QueryList`),
fields,
})
} else if (type instanceof GraphQLNonNull) {
return convertToInputFilter(prefix, type.ofType)
Expand Down
19 changes: 15 additions & 4 deletions packages/gatsby/src/schema/infer-graphql-input-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,24 @@ function inferGraphQLInputFields({
)
}

let fields
if (headType === `object`) {
fields = {
elemMatch: {
type: inType,
},
}
} else {
fields = {
...typeFields(headType),
in: { type: new GraphQLList(inType) },
}
}

return {
type: new GraphQLInputObjectType({
name: createTypeName(`${prefix}QueryList`),
fields: {
...typeFields(headType),
in: { type: new GraphQLList(inType) },
},
fields,
}),
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/gatsby/src/schema/run-sift.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ module.exports = ({
const siftifyArgs = object => {
const newObject = {}
_.each(object, (v, k) => {
if (_.isObject(v) && !_.isArray(v)) {
if (_.isPlainObject(v)) {
if (k === `elemMatch`) {
k = `$elemMatch`
}
newObject[k] = siftifyArgs(v)
} else {
// Compile regex first.
Expand All @@ -64,7 +67,7 @@ module.exports = ({
// Build an object that excludes the innermost leafs,
// this avoids including { eq: x } when resolving fields.
function extractFieldsToSift(prekey, key, preobj, obj, val) {
if (_.isObject(val) && !_.isArray(val)) {
if (_.isPlainObject(val)) {
_.forEach((val: any), (v, k) => {
preobj[prekey] = obj
extractFieldsToSift(key, k, obj, {}, v)
Expand Down