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

add "in" operator for single value string, int, float and boolean fields #6331

Merged
merged 1 commit into from
Jul 7, 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 @@ -25,6 +25,7 @@ function isIntInput(type) {
lte: { name: `lte`, type: GraphQLInt },
gt: { name: `gt`, type: GraphQLInt },
gte: { name: `gte`, type: GraphQLInt },
in: { name: `in`, type: new GraphQLList(GraphQLInt) },
})
}

Expand All @@ -33,6 +34,7 @@ function isIdInput(type) {
expect(type.getFields()).toEqual({
eq: { name: `eq`, type: GraphQLID },
ne: { name: `ne`, type: GraphQLID },
in: { name: `in`, type: new GraphQLList(GraphQLID) },
})
}

Expand All @@ -43,6 +45,7 @@ function isStringInput(type) {
ne: { name: `ne`, type: GraphQLString },
regex: { name: `regex`, type: GraphQLString },
glob: { name: `glob`, type: GraphQLString },
in: { name: `in`, type: new GraphQLList(GraphQLString) },
})
}

Expand Down Expand Up @@ -97,6 +100,7 @@ describe(`GraphQL Input args from fields, test-only`, () => {
lte: { name: `lte`, type: GraphQLFloat },
gt: { name: `gt`, type: GraphQLFloat },
gte: { name: `gte`, type: GraphQLFloat },
in: { name: `in`, type: new GraphQLList(GraphQLFloat) },
})

const string = inferredFields.scal_string.type
Expand All @@ -109,6 +113,7 @@ describe(`GraphQL Input args from fields, test-only`, () => {
expect(bool.getFields()).toEqual({
eq: { name: `eq`, type: GraphQLBoolean },
ne: { name: `ne`, type: GraphQLBoolean },
in: { name: `in`, type: new GraphQLList(GraphQLBoolean) },
})

expect(inferredFields).not.toHaveProperty(`scal_odd_unknown`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ describe(`GraphQL Input args`, () => {
{
index: 0,
name: `The Mad Max`,
string: `a`,
float: 1.5,
hair: 1,
date: `2006-07-22T22:39:53.000Z`,
anArray: [1, 2, 3, 4],
Expand Down Expand Up @@ -112,6 +114,8 @@ describe(`GraphQL Input args`, () => {
{
index: 1,
name: `The Mad Wax`,
string: `b`,
float: 2.5,
hair: 2,
anArray: [1, 2, 5, 4],
anotherKey: {
Expand All @@ -130,6 +134,8 @@ describe(`GraphQL Input args`, () => {
{
index: 2,
name: `The Mad Wax`,
string: `c`,
float: 3.5,
hair: 0,
date: `2006-07-29T22:39:53.000Z`,
anotherKey: {
Expand Down Expand Up @@ -448,7 +454,42 @@ describe(`GraphQL Input args`, () => {
expect(result.data.allNode.edges[0].node.name).toEqual(`The Mad Wax`)
})

it(`handles the in operator`, async () => {
it(`handles the in operator for scalars`, async () => {
let result = await queryResult(
nodes,
`
{
string:allNode(filter: { string: { in: ["b", "c"] }}) {
edges { node { index }}
}
int:allNode(filter: { index: { in: [0, 2] }}) {
edges { node { index }}
}
float:allNode(filter: { float: { in: [1.5, 2.5] }}) {
edges { node { index }}
}
boolean:allNode(filter: { boolean: { in: [true, null] }}) {
edges { node { index }}
}
}
`
)
expect(result.errors).not.toBeDefined()
expect(result.data.string.edges.length).toEqual(2)
expect(result.data.string.edges[0].node.index).toEqual(1)
expect(result.data.string.edges[1].node.index).toEqual(2)
expect(result.data.int.edges.length).toEqual(2)
expect(result.data.int.edges[0].node.index).toEqual(0)
expect(result.data.int.edges[1].node.index).toEqual(2)
expect(result.data.float.edges.length).toEqual(2)
expect(result.data.float.edges[0].node.index).toEqual(0)
expect(result.data.float.edges[1].node.index).toEqual(1)
expect(result.data.boolean.edges.length).toEqual(2)
expect(result.data.boolean.edges[0].node.index).toEqual(0)
expect(result.data.boolean.edges[1].node.index).toEqual(2)
})

it(`handles the in operator for array`, async () => {
let result = await queryResult(
nodes,
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const scalarFilterMap = {
gte: { type: GraphQLInt },
lt: { type: GraphQLInt },
lte: { type: GraphQLInt },
in: { type: new GraphQLList(GraphQLInt) },
},
Float: {
eq: { type: GraphQLFloat },
Expand All @@ -97,20 +98,24 @@ const scalarFilterMap = {
gte: { type: GraphQLFloat },
lt: { type: GraphQLFloat },
lte: { type: GraphQLFloat },
in: { type: new GraphQLList(GraphQLFloat) },
},
ID: {
eq: { type: GraphQLID },
ne: { type: GraphQLID },
in: { type: new GraphQLList(GraphQLID) },
},
String: {
eq: { type: GraphQLString },
ne: { type: GraphQLString },
regex: { type: GraphQLString },
glob: { type: GraphQLString },
in: { type: new GraphQLList(GraphQLString) },
},
Boolean: {
eq: { type: GraphQLBoolean },
ne: { type: GraphQLBoolean },
in: { type: new GraphQLList(GraphQLBoolean) },
},
}

Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/src/schema/infer-graphql-input-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ function typeFields(type): GraphQLInputFieldConfigMap {
return {
eq: { type: GraphQLBoolean },
ne: { type: GraphQLBoolean },
in: { type: new GraphQLList(GraphQLBoolean) },
}
case `string`:
return {
eq: { type: GraphQLString },
ne: { type: GraphQLString },
regex: { type: GraphQLString },
glob: { type: GraphQLString },
in: { type: new GraphQLList(GraphQLString) },
}
case `int`:
return {
Expand All @@ -50,6 +52,7 @@ function typeFields(type): GraphQLInputFieldConfigMap {
gte: { type: GraphQLInt },
lt: { type: GraphQLInt },
lte: { type: GraphQLInt },
in: { type: new GraphQLList(GraphQLInt) },
}
case `float`:
return {
Expand All @@ -59,6 +62,7 @@ function typeFields(type): GraphQLInputFieldConfigMap {
gte: { type: GraphQLFloat },
lt: { type: GraphQLFloat },
lte: { type: GraphQLFloat },
in: { type: new GraphQLList(GraphQLFloat) },
}
}
return {}
Expand Down