Skip to content

Commit 9aaf846

Browse files
authored
fix: support dynamic arguments (#146)
Co-authored-by: James Mortemore <[email protected]> fixes #145
1 parent 3e67653 commit 9aaf846

22 files changed

+283
-205
lines changed

lib/query-validation-visitor.js

+6
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ module.exports = class QueryValidationVisitor {
148148
}
149149

150150
function validateScalarTypeValue (context, currentQueryField, typeDefWithDirective, valueTypeDef, value, variableName, argName, fieldNameForError, errMessageAt) {
151+
if (!typeDefWithDirective.astNode) { return }
152+
151153
const directiveArgumentMap = getDirectiveValues(constraintDirectiveTypeDefsObj, typeDefWithDirective.astNode)
152154

153155
if (directiveArgumentMap) {
@@ -172,13 +174,17 @@ function validateScalarTypeValue (context, currentQueryField, typeDefWithDirecti
172174
}
173175

174176
function validateInputTypeValue (context, inputObjectTypeDef, argName, variableName, value, currentField, parentNames) {
177+
if (!inputObjectTypeDef.astNode) { return }
178+
175179
// use new visitor to traverse input object structure
176180
const visitor = new InputObjectValidationVisitor(context, inputObjectTypeDef, argName, variableName, value, currentField, parentNames)
177181

178182
visit(inputObjectTypeDef.astNode, visitor)
179183
}
180184

181185
function validateArrayTypeValue (context, valueTypeDef, typeDefWithDirective, value, currentField, argName, variableName, iFieldNameFull) {
186+
if (!typeDefWithDirective.astNode) { return }
187+
182188
let valueTypeDefArray = valueTypeDef.ofType
183189

184190
if (isNonNullType(valueTypeDefArray)) valueTypeDefArray = valueTypeDefArray.ofType

test/argument-dynamic.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const { strictEqual } = require('assert')
2+
const { GraphQLString } = require('graphql')
3+
const { mapSchema, getDirective, MapperKind } = require('@graphql-tools/utils')
4+
5+
const dateDirectiveTypeDefs = 'directive @formatDate on FIELD_DEFINITION'
6+
7+
const dateDirectiveTransformer = (schema) =>
8+
mapSchema(schema, {
9+
[MapperKind.OBJECT_FIELD] (fieldConfig) {
10+
const dateDirective = getDirective(schema, fieldConfig, 'formatDate')?.[0]
11+
if (dateDirective) {
12+
fieldConfig.args.format = {
13+
type: GraphQLString
14+
}
15+
16+
fieldConfig.type = GraphQLString
17+
return fieldConfig
18+
}
19+
}
20+
})
21+
22+
module.exports.test = function (setup, implType) {
23+
describe('Dynamic argument', function () {
24+
before(async function () {
25+
this.typeDefs = /* GraphQL */`
26+
type Query {
27+
getBook: String @formatDate
28+
getBooks: [String] @formatDate
29+
}
30+
` + '\n' + dateDirectiveTypeDefs
31+
32+
this.request = await setup({ typeDefs: this.typeDefs, schemaCreatedCallback: (schema) => { return dateDirectiveTransformer(schema) } })
33+
})
34+
35+
it('should pass', async function () {
36+
const query = /* GraphQL */`
37+
query Go {
38+
getBook(format: "aa")
39+
getBooks(format: "aa")
40+
}
41+
42+
`
43+
const { body, statusCode } = await this.request
44+
.post('/graphql')
45+
.set('Accept', 'application/json')
46+
.send({ query })
47+
48+
if (statusCode !== 200) { console.log(body) }
49+
strictEqual(statusCode, 200)
50+
})
51+
})
52+
}

test/argument.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports.test = function (setup, implType) {
4444
authors (size: Int @constraint(max: 4)): [String]
4545
}
4646
`
47-
this.request = await setup(this.typeDefs)
47+
this.request = await setup({ typeDefs: this.typeDefs })
4848
})
4949

5050
it('should pass', async function () {
@@ -105,7 +105,7 @@ module.exports.test = function (setup, implType) {
105105

106106
if (isSchemaWrapperImplType(implType)) {
107107
it('should throw custom error', async function () {
108-
const request = await setup(this.typeDefs, formatError)
108+
const request = await setup({ typeDefs: this.typeDefs, formatError })
109109
const { body, statusCode } = await request
110110
.post('/graphql')
111111
.set('Accept', 'application/json')
@@ -183,7 +183,7 @@ module.exports.test = function (setup, implType) {
183183
}
184184
`
185185

186-
this.request = await setup(this.typeDefs)
186+
this.request = await setup({ typeDefs: this.typeDefs })
187187
})
188188

189189
it('should pass', async function () {
@@ -257,7 +257,7 @@ module.exports.test = function (setup, implType) {
257257

258258
if (isSchemaWrapperImplType(implType)) {
259259
it('should throw custom error', async function () {
260-
const request = await setup(this.typeDefs, formatError)
260+
const request = await setup({ typeDefs: this.typeDefs, formatError })
261261
const { body, statusCode } = await request
262262
.post('/graphql')
263263
.set('Accept', 'application/json')

test/array-size.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports.test = function (setup, implType) {
2626
title: [Int!] @constraint(minItems: 3)
2727
}`
2828

29-
this.request = await setup(this.typeDefs)
29+
this.request = await setup({ typeDefs: this.typeDefs })
3030
})
3131

3232
it('should pass', async function () {
@@ -86,7 +86,7 @@ module.exports.test = function (setup, implType) {
8686
title: [Int!] @constraint(maxItems: 2)
8787
}`
8888

89-
this.request = await setup(this.typeDefs)
89+
this.request = await setup({ typeDefs: this.typeDefs })
9090
})
9191

9292
it('should pass', async function () {
@@ -140,7 +140,7 @@ module.exports.test = function (setup, implType) {
140140
createBook(input: [Int] @constraint(minItems: 3)): Book
141141
}`
142142

143-
this.request = await setup(this.typeDefs)
143+
this.request = await setup({ typeDefs: this.typeDefs })
144144
})
145145

146146
it('should pass', async function () {
@@ -212,7 +212,7 @@ module.exports.test = function (setup, implType) {
212212
createBook(input: [Int] @constraint(maxItems: 2, max: 100)): Book
213213
}`
214214

215-
this.request = await setup(this.typeDefs)
215+
this.request = await setup({ typeDefs: this.typeDefs })
216216
})
217217

218218
it('should pass', async function () {
@@ -307,7 +307,7 @@ module.exports.test = function (setup, implType) {
307307
createBook(input: [ID]! @constraint(minItems: 3)): Book
308308
}`
309309

310-
this.request = await setup(this.typeDefs)
310+
this.request = await setup({ typeDefs: this.typeDefs })
311311
})
312312

313313
it('should pass', async function () {
@@ -365,7 +365,7 @@ module.exports.test = function (setup, implType) {
365365
title: String @constraint(maxLength: 2)
366366
}
367367
`
368-
this.request = await setup(this.typeDefs)
368+
this.request = await setup({ typeDefs: this.typeDefs })
369369
})
370370

371371
it('should pass', async function () {

test/array-structures.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports.test = function (setup, implType) {
2929
}
3030
`
3131

32-
this.request = await setup(this.typeDefs)
32+
this.request = await setup({ typeDefs: this.typeDefs })
3333
})
3434

3535
it('should pass', async function () {
@@ -79,7 +79,7 @@ module.exports.test = function (setup, implType) {
7979
}
8080
`
8181

82-
this.request = await setup(this.typeDefs)
82+
this.request = await setup({ typeDefs: this.typeDefs })
8383
})
8484

8585
it('should pass', async function () {
@@ -142,7 +142,7 @@ module.exports.test = function (setup, implType) {
142142
}
143143
`
144144

145-
this.request = await setup(this.typeDefs)
145+
this.request = await setup({ typeDefs: this.typeDefs })
146146
})
147147

148148
it('should pass', async function () {
@@ -221,7 +221,7 @@ module.exports.test = function (setup, implType) {
221221
}
222222
`
223223

224-
this.request = await setup(this.typeDefs)
224+
this.request = await setup({ typeDefs: this.typeDefs })
225225
})
226226

227227
it('should pass', async function () {
@@ -264,7 +264,7 @@ module.exports.test = function (setup, implType) {
264264
}
265265
`
266266

267-
this.request = await setup(this.typeDefs)
267+
this.request = await setup({ typeDefs: this.typeDefs })
268268
})
269269

270270
it('should pass', async function () {
@@ -330,7 +330,7 @@ module.exports.test = function (setup, implType) {
330330
}
331331
`
332332

333-
this.request = await setup(this.typeDefs)
333+
this.request = await setup({ typeDefs: this.typeDefs })
334334
})
335335

336336
it('should pass', async function () {
@@ -381,7 +381,7 @@ module.exports.test = function (setup, implType) {
381381
}
382382
`
383383

384-
this.request = await setup(this.typeDefs)
384+
this.request = await setup({ typeDefs: this.typeDefs })
385385
})
386386

387387
it('should pass', async function () {

test/array.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports.test = function (setup, implType) {
2626
title: [Int!]! @constraint(min: 3)
2727
}`
2828

29-
this.request = await setup(this.typeDefs)
29+
this.request = await setup({ typeDefs: this.typeDefs })
3030
})
3131

3232
it('should pass', async function () {
@@ -70,7 +70,7 @@ module.exports.test = function (setup, implType) {
7070
title: [Int!] @constraint(max: 3)
7171
}`
7272

73-
this.request = await setup(this.typeDefs)
73+
this.request = await setup({ typeDefs: this.typeDefs })
7474
})
7575

7676
it('should pass', async function () {
@@ -115,7 +115,7 @@ module.exports.test = function (setup, implType) {
115115
title: [Int!] @constraint(multipleOf: 2)
116116
}`
117117

118-
this.request = await setup(this.typeDefs)
118+
this.request = await setup({ typeDefs: this.typeDefs })
119119
})
120120

121121
if (!isServerValidatorEnvelop(implType)) {
@@ -174,7 +174,7 @@ module.exports.test = function (setup, implType) {
174174
title: [String!] @constraint(minLength: 3)
175175
}`
176176

177-
this.request = await setup(this.typeDefs)
177+
this.request = await setup({ typeDefs: this.typeDefs })
178178
})
179179

180180
it('should pass', async function () {
@@ -219,7 +219,7 @@ module.exports.test = function (setup, implType) {
219219
title: [String] @constraint(maxLength: 3)
220220
}`
221221

222-
this.request = await setup(this.typeDefs)
222+
this.request = await setup({ typeDefs: this.typeDefs })
223223
})
224224

225225
it('should pass', async function () {
@@ -264,7 +264,7 @@ module.exports.test = function (setup, implType) {
264264
title: [String!]! @constraint(format: "uri")
265265
}`
266266

267-
this.request = await setup(this.typeDefs)
267+
this.request = await setup({ typeDefs: this.typeDefs })
268268
})
269269

270270
it('should pass', async function () {

0 commit comments

Comments
 (0)