Skip to content

Commit 4a94642

Browse files
authored
add "in" operator for single value string, int, float and boolean fields (#6331)
1 parent 95ae60e commit 4a94642

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

packages/gatsby/src/schema/__tests__/infer-graphql-input-type-from-fields-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function isIntInput(type) {
2525
lte: { name: `lte`, type: GraphQLInt },
2626
gt: { name: `gt`, type: GraphQLInt },
2727
gte: { name: `gte`, type: GraphQLInt },
28+
in: { name: `in`, type: new GraphQLList(GraphQLInt) },
2829
})
2930
}
3031

@@ -33,6 +34,7 @@ function isIdInput(type) {
3334
expect(type.getFields()).toEqual({
3435
eq: { name: `eq`, type: GraphQLID },
3536
ne: { name: `ne`, type: GraphQLID },
37+
in: { name: `in`, type: new GraphQLList(GraphQLID) },
3638
})
3739
}
3840

@@ -43,6 +45,7 @@ function isStringInput(type) {
4345
ne: { name: `ne`, type: GraphQLString },
4446
regex: { name: `regex`, type: GraphQLString },
4547
glob: { name: `glob`, type: GraphQLString },
48+
in: { name: `in`, type: new GraphQLList(GraphQLString) },
4649
})
4750
}
4851

@@ -97,6 +100,7 @@ describe(`GraphQL Input args from fields, test-only`, () => {
97100
lte: { name: `lte`, type: GraphQLFloat },
98101
gt: { name: `gt`, type: GraphQLFloat },
99102
gte: { name: `gte`, type: GraphQLFloat },
103+
in: { name: `in`, type: new GraphQLList(GraphQLFloat) },
100104
})
101105

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

114119
expect(inferredFields).not.toHaveProperty(`scal_odd_unknown`)

packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ describe(`GraphQL Input args`, () => {
8585
{
8686
index: 0,
8787
name: `The Mad Max`,
88+
string: `a`,
89+
float: 1.5,
8890
hair: 1,
8991
date: `2006-07-22T22:39:53.000Z`,
9092
anArray: [1, 2, 3, 4],
@@ -112,6 +114,8 @@ describe(`GraphQL Input args`, () => {
112114
{
113115
index: 1,
114116
name: `The Mad Wax`,
117+
string: `b`,
118+
float: 2.5,
115119
hair: 2,
116120
anArray: [1, 2, 5, 4],
117121
anotherKey: {
@@ -130,6 +134,8 @@ describe(`GraphQL Input args`, () => {
130134
{
131135
index: 2,
132136
name: `The Mad Wax`,
137+
string: `c`,
138+
float: 3.5,
133139
hair: 0,
134140
date: `2006-07-29T22:39:53.000Z`,
135141
anotherKey: {
@@ -448,7 +454,42 @@ describe(`GraphQL Input args`, () => {
448454
expect(result.data.allNode.edges[0].node.name).toEqual(`The Mad Wax`)
449455
})
450456

451-
it(`handles the in operator`, async () => {
457+
it(`handles the in operator for scalars`, async () => {
458+
let result = await queryResult(
459+
nodes,
460+
`
461+
{
462+
string:allNode(filter: { string: { in: ["b", "c"] }}) {
463+
edges { node { index }}
464+
}
465+
int:allNode(filter: { index: { in: [0, 2] }}) {
466+
edges { node { index }}
467+
}
468+
float:allNode(filter: { float: { in: [1.5, 2.5] }}) {
469+
edges { node { index }}
470+
}
471+
boolean:allNode(filter: { boolean: { in: [true, null] }}) {
472+
edges { node { index }}
473+
}
474+
}
475+
`
476+
)
477+
expect(result.errors).not.toBeDefined()
478+
expect(result.data.string.edges.length).toEqual(2)
479+
expect(result.data.string.edges[0].node.index).toEqual(1)
480+
expect(result.data.string.edges[1].node.index).toEqual(2)
481+
expect(result.data.int.edges.length).toEqual(2)
482+
expect(result.data.int.edges[0].node.index).toEqual(0)
483+
expect(result.data.int.edges[1].node.index).toEqual(2)
484+
expect(result.data.float.edges.length).toEqual(2)
485+
expect(result.data.float.edges[0].node.index).toEqual(0)
486+
expect(result.data.float.edges[1].node.index).toEqual(1)
487+
expect(result.data.boolean.edges.length).toEqual(2)
488+
expect(result.data.boolean.edges[0].node.index).toEqual(0)
489+
expect(result.data.boolean.edges[1].node.index).toEqual(2)
490+
})
491+
492+
it(`handles the in operator for array`, async () => {
452493
let result = await queryResult(
453494
nodes,
454495
`

packages/gatsby/src/schema/infer-graphql-input-fields-from-fields.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const scalarFilterMap = {
8989
gte: { type: GraphQLInt },
9090
lt: { type: GraphQLInt },
9191
lte: { type: GraphQLInt },
92+
in: { type: new GraphQLList(GraphQLInt) },
9293
},
9394
Float: {
9495
eq: { type: GraphQLFloat },
@@ -97,20 +98,24 @@ const scalarFilterMap = {
9798
gte: { type: GraphQLFloat },
9899
lt: { type: GraphQLFloat },
99100
lte: { type: GraphQLFloat },
101+
in: { type: new GraphQLList(GraphQLFloat) },
100102
},
101103
ID: {
102104
eq: { type: GraphQLID },
103105
ne: { type: GraphQLID },
106+
in: { type: new GraphQLList(GraphQLID) },
104107
},
105108
String: {
106109
eq: { type: GraphQLString },
107110
ne: { type: GraphQLString },
108111
regex: { type: GraphQLString },
109112
glob: { type: GraphQLString },
113+
in: { type: new GraphQLList(GraphQLString) },
110114
},
111115
Boolean: {
112116
eq: { type: GraphQLBoolean },
113117
ne: { type: GraphQLBoolean },
118+
in: { type: new GraphQLList(GraphQLBoolean) },
114119
},
115120
}
116121

packages/gatsby/src/schema/infer-graphql-input-fields.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ function typeFields(type): GraphQLInputFieldConfigMap {
3434
return {
3535
eq: { type: GraphQLBoolean },
3636
ne: { type: GraphQLBoolean },
37+
in: { type: new GraphQLList(GraphQLBoolean) },
3738
}
3839
case `string`:
3940
return {
4041
eq: { type: GraphQLString },
4142
ne: { type: GraphQLString },
4243
regex: { type: GraphQLString },
4344
glob: { type: GraphQLString },
45+
in: { type: new GraphQLList(GraphQLString) },
4446
}
4547
case `int`:
4648
return {
@@ -50,6 +52,7 @@ function typeFields(type): GraphQLInputFieldConfigMap {
5052
gte: { type: GraphQLInt },
5153
lt: { type: GraphQLInt },
5254
lte: { type: GraphQLInt },
55+
in: { type: new GraphQLList(GraphQLInt) },
5356
}
5457
case `float`:
5558
return {
@@ -59,6 +62,7 @@ function typeFields(type): GraphQLInputFieldConfigMap {
5962
gte: { type: GraphQLFloat },
6063
lt: { type: GraphQLFloat },
6164
lte: { type: GraphQLFloat },
65+
in: { type: new GraphQLList(GraphQLFloat) },
6266
}
6367
}
6468
return {}

0 commit comments

Comments
 (0)