Skip to content

Commit 29ce50e

Browse files
dannywilspieh
authored andcommitted
Add "nin" operator for single value string, int, float and boolean fields (#7579)
1 parent 5759015 commit 29ce50e

4 files changed

+81
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function isIntInput(type) {
2626
gt: { name: `gt`, type: GraphQLInt },
2727
gte: { name: `gte`, type: GraphQLInt },
2828
in: { name: `in`, type: new GraphQLList(GraphQLInt) },
29+
nin: { name: `nin`, type: new GraphQLList(GraphQLInt) },
2930
})
3031
}
3132

@@ -35,6 +36,7 @@ function isIdInput(type) {
3536
eq: { name: `eq`, type: GraphQLID },
3637
ne: { name: `ne`, type: GraphQLID },
3738
in: { name: `in`, type: new GraphQLList(GraphQLID) },
39+
nin: { name: `nin`, type: new GraphQLList(GraphQLID) },
3840
})
3941
}
4042

@@ -46,6 +48,7 @@ function isStringInput(type) {
4648
regex: { name: `regex`, type: GraphQLString },
4749
glob: { name: `glob`, type: GraphQLString },
4850
in: { name: `in`, type: new GraphQLList(GraphQLString) },
51+
nin: { name: `nin`, type: new GraphQLList(GraphQLString) },
4952
})
5053
}
5154

@@ -101,6 +104,7 @@ describe(`GraphQL Input args from fields, test-only`, () => {
101104
gt: { name: `gt`, type: GraphQLFloat },
102105
gte: { name: `gte`, type: GraphQLFloat },
103106
in: { name: `in`, type: new GraphQLList(GraphQLFloat) },
107+
nin: { name: `nin`, type: new GraphQLList(GraphQLFloat) },
104108
})
105109

106110
const string = inferredFields.scal_string.type
@@ -114,6 +118,7 @@ describe(`GraphQL Input args from fields, test-only`, () => {
114118
eq: { name: `eq`, type: GraphQLBoolean },
115119
ne: { name: `ne`, type: GraphQLBoolean },
116120
in: { name: `in`, type: new GraphQLList(GraphQLBoolean) },
121+
nin: { name: `nin`, type: new GraphQLList(GraphQLBoolean) },
117122
})
118123

119124
expect(inferredFields).not.toHaveProperty(`scal_odd_unknown`)
@@ -311,6 +316,7 @@ describe(`GraphQL Input args from fields, test-only`, () => {
311316
lt: { name: `lt`, type: GraphQLFloat },
312317
lte: { name: `lte`, type: GraphQLFloat },
313318
in: { name: `in`, type: new GraphQLList(GraphQLFloat) },
319+
nin: { name: `nin`, type: new GraphQLList(GraphQLFloat) },
314320
})
315321
})
316322

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

+66
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,72 @@ describe(`GraphQL Input args`, () => {
578578
expect(result.data.test3.edges[0].node.index).toEqual(1)
579579
})
580580

581+
it(`handles the nin operator for array`, async () => {
582+
let result = await queryResult(
583+
nodes,
584+
`
585+
{
586+
allNode(filter: {anArray: { nin: [5] }}) {
587+
edges { node { anArray }}
588+
}
589+
}
590+
`
591+
)
592+
expect(result.errors).not.toBeDefined()
593+
expect(result.data.allNode.edges.length).toEqual(2)
594+
595+
result.data.allNode.edges.forEach(edge => {
596+
expect(edge.node.anArray).not.toEqual(expect.arrayContaining([5]))
597+
})
598+
})
599+
600+
it(`handles the nin operator for scalars`, async () => {
601+
let result = await queryResult(
602+
nodes,
603+
`
604+
{
605+
string:allNode(filter: { string: { nin: ["b", "c"] }}) {
606+
edges { node { string }}
607+
}
608+
int:allNode(filter: { index: { nin: [0, 2] }}) {
609+
edges { node { index }}
610+
}
611+
float:allNode(filter: { float: { nin: [1.5] }}) {
612+
edges { node { float }}
613+
}
614+
boolean:allNode(filter: { boolean: { nin: [true, null] }}) {
615+
edges { node { boolean }}
616+
}
617+
}
618+
`
619+
)
620+
621+
expect(result.errors).not.toBeDefined()
622+
623+
expect(result.data.string.edges.length).toEqual(1)
624+
result.data.string.edges.forEach(edge => {
625+
expect(edge.node.string).not.toEqual(`b`)
626+
expect(edge.node.string).not.toEqual(`c`)
627+
})
628+
629+
expect(result.data.int.edges.length).toEqual(1)
630+
result.data.int.edges.forEach(edge => {
631+
expect(edge.node.index).not.toEqual(0)
632+
expect(edge.node.index).not.toEqual(2)
633+
})
634+
635+
expect(result.data.float.edges.length).toEqual(2)
636+
result.data.float.edges.forEach(edge => {
637+
expect(edge.node.float).not.toEqual(1.5)
638+
})
639+
640+
expect(result.data.boolean.edges.length).toEqual(1)
641+
result.data.boolean.edges.forEach(edge => {
642+
expect(edge.node.boolean).not.toEqual(null)
643+
expect(edge.node.boolean).not.toEqual(true)
644+
})
645+
})
646+
581647
it(`handles the glob operator`, async () => {
582648
let result = await queryResult(
583649
nodes,

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

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const scalarFilterMap = {
9090
lt: { type: GraphQLInt },
9191
lte: { type: GraphQLInt },
9292
in: { type: new GraphQLList(GraphQLInt) },
93+
nin: { type: new GraphQLList(GraphQLInt) },
9394
},
9495
Float: {
9596
eq: { type: GraphQLFloat },
@@ -99,23 +100,27 @@ const scalarFilterMap = {
99100
lt: { type: GraphQLFloat },
100101
lte: { type: GraphQLFloat },
101102
in: { type: new GraphQLList(GraphQLFloat) },
103+
nin: { type: new GraphQLList(GraphQLFloat) },
102104
},
103105
ID: {
104106
eq: { type: GraphQLID },
105107
ne: { type: GraphQLID },
106108
in: { type: new GraphQLList(GraphQLID) },
109+
nin: { type: new GraphQLList(GraphQLID) },
107110
},
108111
String: {
109112
eq: { type: GraphQLString },
110113
ne: { type: GraphQLString },
111114
regex: { type: GraphQLString },
112115
glob: { type: GraphQLString },
113116
in: { type: new GraphQLList(GraphQLString) },
117+
nin: { type: new GraphQLList(GraphQLString) },
114118
},
115119
Boolean: {
116120
eq: { type: GraphQLBoolean },
117121
ne: { type: GraphQLBoolean },
118122
in: { type: new GraphQLList(GraphQLBoolean) },
123+
nin: { type: new GraphQLList(GraphQLBoolean) },
119124
},
120125
}
121126

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

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function typeFields(type): GraphQLInputFieldConfigMap {
3535
eq: { type: GraphQLBoolean },
3636
ne: { type: GraphQLBoolean },
3737
in: { type: new GraphQLList(GraphQLBoolean) },
38+
nin: { type: new GraphQLList(GraphQLBoolean) },
3839
}
3940
case `string`:
4041
return {
@@ -43,6 +44,7 @@ function typeFields(type): GraphQLInputFieldConfigMap {
4344
regex: { type: GraphQLString },
4445
glob: { type: GraphQLString },
4546
in: { type: new GraphQLList(GraphQLString) },
47+
nin: { type: new GraphQLList(GraphQLString) },
4648
}
4749
case `int`:
4850
return {
@@ -53,6 +55,7 @@ function typeFields(type): GraphQLInputFieldConfigMap {
5355
lt: { type: GraphQLInt },
5456
lte: { type: GraphQLInt },
5557
in: { type: new GraphQLList(GraphQLInt) },
58+
nin: { type: new GraphQLList(GraphQLInt) },
5659
}
5760
case `float`:
5861
return {
@@ -63,6 +66,7 @@ function typeFields(type): GraphQLInputFieldConfigMap {
6366
lt: { type: GraphQLFloat },
6467
lte: { type: GraphQLFloat },
6568
in: { type: new GraphQLList(GraphQLFloat) },
69+
nin: { type: new GraphQLList(GraphQLFloat) },
6670
}
6771
}
6872
return {}

0 commit comments

Comments
 (0)