Skip to content

Commit 1adc4d6

Browse files
authored
feat: support argument definitions (#91)
fixes #11
1 parent 44340b8 commit 1adc4d6

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ function constraintDirective () {
115115
[MapperKind.FIELD]: (fieldConfig) => {
116116
const directiveArgumentMap = getDirective(schema, fieldConfig, 'constraint')?.[0]
117117

118+
if (directiveArgumentMap) {
119+
wrapType(fieldConfig, directiveArgumentMap)
120+
121+
return fieldConfig
122+
}
123+
},
124+
[MapperKind.ARGUMENT]: (fieldConfig) => {
125+
const directiveArgumentMap = getDirective(schema, fieldConfig, 'constraint')?.[0]
126+
118127
if (directiveArgumentMap) {
119128
wrapType(fieldConfig, directiveArgumentMap)
120129

@@ -124,7 +133,7 @@ function constraintDirective () {
124133
})
125134
}
126135

127-
const constraintDirectiveTypeDefs = `
136+
const constraintDirectiveTypeDefs = /* GraphQL */`
128137
directive @constraint(
129138
# String constraints
130139
minLength: Int
@@ -143,6 +152,6 @@ const constraintDirectiveTypeDefs = `
143152
exclusiveMax: Float
144153
multipleOf: Float
145154
uniqueTypeName: String
146-
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION`
155+
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION | ARGUMENT_DEFINITION`
147156

148157
module.exports = { constraintDirective, constraintDirectiveTypeDefs }

test/argument.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const { deepStrictEqual, strictEqual } = require('assert')
2+
const setup = require('./setup')
3+
const formatError = (error) => {
4+
const { message, code, fieldName, context } = error?.originalError?.originalError || error?.originalError || error
5+
return { message, code, fieldName, context }
6+
}
7+
8+
describe('@constraint Int in ARGUMENT_DEFINITION', function () {
9+
describe('#max', function () {
10+
const query = /* GraphQL */`
11+
query ($size: size_Int_max_3) {
12+
books(size: $size) {
13+
title
14+
}
15+
}
16+
`
17+
18+
before(async function () {
19+
this.typeDefs = /* GraphQL */`
20+
type Query {
21+
books (size: Int @constraint(max: 3)): [Book]
22+
}
23+
type Book {
24+
title: String
25+
}
26+
`
27+
28+
this.request = await setup(this.typeDefs)
29+
})
30+
31+
it('should pass', async function () {
32+
const { body, statusCode } = await this.request
33+
.post('/graphql')
34+
.set('Accept', 'application/json')
35+
.send({ query, variables: { size: 2 } })
36+
37+
strictEqual(statusCode, 200)
38+
deepStrictEqual(body, { data: { books: null } })
39+
})
40+
41+
it('should fail', async function () {
42+
const { body, statusCode } = await this.request
43+
.post('/graphql')
44+
.set('Accept', 'application/json')
45+
.send({ query, variables: { size: 100 } })
46+
47+
strictEqual(statusCode, 400)
48+
strictEqual(body.errors[0].message,
49+
'Variable "$size" got invalid value 100; Expected type "size_Int_max_3". Must be no greater than 3')
50+
})
51+
52+
it('should throw custom error', async function () {
53+
const request = await setup(this.typeDefs, formatError)
54+
const { body, statusCode } = await request
55+
.post('/graphql')
56+
.set('Accept', 'application/json')
57+
.send({ query, variables: { size: 100 } })
58+
59+
strictEqual(statusCode, 400)
60+
deepStrictEqual(body.errors[0], {
61+
message: 'Must be no greater than 3',
62+
code: 'ERR_GRAPHQL_CONSTRAINT_VALIDATION',
63+
fieldName: 'size',
64+
context: [{ arg: 'max', value: 3 }]
65+
})
66+
})
67+
})
68+
})

0 commit comments

Comments
 (0)