Skip to content

Commit 915eb8d

Browse files
authored
fix: falsy string and int query values (#189)
1 parent 39efc7e commit 915eb8d

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

lib/query-validation-visitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ module.exports = class QueryValidationVisitor {
140140
validateArrayTypeValue(this.context, valueTypeDef, argTypeDef, value, this.currentField, argName, variableName, variableName)
141141
} else {
142142
// nothing to validate
143-
if (!value) return
143+
if (!value && value !== '' && value !== 0) return
144144

145145
const fieldNameForError = variableName || this.currentField.name.value + '.' + argName
146146

test/variable.test.js

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const { deepStrictEqual, strictEqual } = require('assert')
22
const { valueByImplType, isStatusCodeError } = require('./testutils')
33

44
module.exports.test = function (setup, implType) {
5-
const queryIntType = valueByImplType(implType, 'size_Int_max_3', 'Int')
5+
const queryIntMaxType = valueByImplType(implType, 'size_Int_max_3', 'Int')
6+
const queryIntMinType = valueByImplType(implType, 'size_Int_min_3', 'Int')
7+
const queryStringMinLengthType = valueByImplType(implType, 'search_String_minLength_3', 'String')
68

79
describe('Variables', function () {
810
describe('variable default value', function () {
@@ -20,7 +22,7 @@ module.exports.test = function (setup, implType) {
2022

2123
it('should pass', async function () {
2224
const query = /* GraphQL */`
23-
query ($size: ${queryIntType} = 3) {
25+
query ($size: ${queryIntMaxType} = 3) {
2426
books(size: $size) {
2527
title
2628
}
@@ -37,7 +39,7 @@ module.exports.test = function (setup, implType) {
3739

3840
it('should fail', async function () {
3941
const query = /* GraphQL */`
40-
query ($size: ${queryIntType} = 4) {
42+
query ($size: ${queryIntMaxType} = 4) {
4143
books(size: $size) {
4244
title
4345
}
@@ -53,5 +55,103 @@ module.exports.test = function (setup, implType) {
5355
valueByImplType(implType, 'Expected value of type "size_Int_max_3", found 4;', 'Variable "$size" got invalid value 4.') + ' Must be no greater than 3')
5456
})
5557
})
58+
59+
describe('variable int falsy', function () {
60+
before(async function () {
61+
this.typeDefs = /* GraphQL */`
62+
type Query {
63+
books (size: Int @constraint(min: 3)): [Book]
64+
}
65+
type Book {
66+
title: String
67+
}
68+
`
69+
this.request = await setup({ typeDefs: this.typeDefs })
70+
})
71+
72+
it('should pass', async function () {
73+
const query = /* GraphQL */`
74+
query ($size: ${queryIntMinType} = 3) {
75+
books(size: $size) {
76+
title
77+
}
78+
}
79+
`
80+
const { body, statusCode } = await this.request
81+
.post('/graphql')
82+
.set('Accept', 'application/json')
83+
.send({ query })
84+
85+
strictEqual(statusCode, 200)
86+
deepStrictEqual(body, { data: { books: null } })
87+
})
88+
89+
it('should fail', async function () {
90+
const query = /* GraphQL */`
91+
query ($size: ${queryIntMinType} = 0) {
92+
books(size: $size) {
93+
title
94+
}
95+
}
96+
`
97+
const { body, statusCode } = await this.request
98+
.post('/graphql')
99+
.set('Accept', 'application/json')
100+
.send({ query })
101+
102+
isStatusCodeError(statusCode, implType)
103+
strictEqual(body.errors[0].message,
104+
valueByImplType(implType, 'Expected value of type "size_Int_min_3", found 0;', 'Variable "$size" got invalid value 0.') + ' Must be at least 3')
105+
})
106+
})
107+
108+
describe('variable string falsy', function () {
109+
before(async function () {
110+
this.typeDefs = /* GraphQL */`
111+
type Query {
112+
books (search: String @constraint(minLength: 3)): [Book]
113+
}
114+
type Book {
115+
title: String
116+
}
117+
`
118+
this.request = await setup({ typeDefs: this.typeDefs })
119+
})
120+
121+
it('should pass', async function () {
122+
const query = /* GraphQL */`
123+
query ($search: ${queryStringMinLengthType} = "test") {
124+
books(search: $search) {
125+
title
126+
}
127+
}
128+
`
129+
const { body, statusCode } = await this.request
130+
.post('/graphql')
131+
.set('Accept', 'application/json')
132+
.send({ query })
133+
134+
strictEqual(statusCode, 200)
135+
deepStrictEqual(body, { data: { books: null } })
136+
})
137+
138+
it('should fail - despite being falsy', async function () {
139+
const query = /* GraphQL */`
140+
query ($search: ${queryStringMinLengthType} = "") {
141+
books(search: $search) {
142+
title
143+
}
144+
}
145+
`
146+
const { body, statusCode } = await this.request
147+
.post('/graphql')
148+
.set('Accept', 'application/json')
149+
.send({ query })
150+
151+
isStatusCodeError(statusCode, implType)
152+
strictEqual(body.errors[0].message,
153+
valueByImplType(implType, 'Expected value of type "search_String_minLength_3", found "";', 'Variable "$search" got invalid value "".') + ' Must be at least 3 characters in length')
154+
})
155+
})
56156
})
57157
}

0 commit comments

Comments
 (0)