Skip to content

Commit 558c087

Browse files
mrcarolinmr-c
andauthored
feat: added support id scalar constraints (#205)
Co-authored-by: mr-c <[email protected]>
1 parent 971f4a9 commit 558c087

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

lib/type-utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ const {
44
GraphQLString,
55
isNonNullType,
66
isScalarType,
7-
isListType
7+
isListType,
8+
GraphQLID
89
} = require('graphql')
910
const { ConstraintStringType, validate: validateStringFn } = require('../scalars/string')
1011
const { ConstraintNumberType, validate: validateNumberFn } = require('../scalars/number')
1112

1213
function getConstraintTypeObject (fieldName, type, uniqueTypeName, directiveArgumentMap) {
13-
if (type === GraphQLString) {
14+
if (type === GraphQLString || type === GraphQLID) {
1415
return new ConstraintStringType(
1516
fieldName,
1617
uniqueTypeName,
@@ -30,7 +31,7 @@ function getConstraintTypeObject (fieldName, type, uniqueTypeName, directiveArgu
3031
}
3132

3233
function getConstraintValidateFn (type) {
33-
if (type === GraphQLString) {
34+
if (type === GraphQLString || type === GraphQLID) {
3435
return validateStringFn
3536
} else if (type === GraphQLFloat || type === GraphQLInt) {
3637
return validateNumberFn

test/id.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const { deepStrictEqual, strictEqual } = require('assert')
2+
const { valueByImplType, isStatusCodeError } = require('./testutils')
3+
4+
module.exports.test = function (setup, implType) {
5+
describe('@constraint ID in INPUT_FIELD_DEFINITION treated like String', function () {
6+
const query = `mutation createBook($input: BookInput) {
7+
createBook(input: $input) {
8+
title
9+
}
10+
}`
11+
12+
describe('#minLength', function () {
13+
before(async function () {
14+
this.typeDefs = `
15+
type Query {
16+
books: [Book]
17+
}
18+
type Book {
19+
title: ID
20+
}
21+
type Mutation {
22+
createBook(input: BookInput): Book
23+
}
24+
input BookInput {
25+
title: ID! @constraint(minLength: 3)
26+
}`
27+
28+
this.request = await setup({ typeDefs: 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: { input: { title: 'he💩' } } })
36+
37+
strictEqual(statusCode, 200)
38+
deepStrictEqual(body, { data: { createBook: 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: { input: { title: 'a💩' } } })
46+
47+
isStatusCodeError(statusCode, implType)
48+
strictEqual(body.errors[0].message,
49+
'Variable "$input" got invalid value "a💩" at "input.title"' + valueByImplType(implType, '; Expected type "title_String_NotNull_minLength_3"') + '. Must be at least 3 characters in length')
50+
})
51+
52+
it('should fail with empty id', async function () {
53+
const { body, statusCode } = await this.request
54+
.post('/graphql')
55+
.set('Accept', 'application/json')
56+
.send({ query, variables: { input: { title: '' } } })
57+
58+
isStatusCodeError(statusCode, implType)
59+
strictEqual(body.errors[0].message,
60+
'Variable "$input" got invalid value "" at "input.title"' + valueByImplType(implType, '; Expected type "title_String_NotNull_minLength_3"') + '. Must be at least 3 characters in length')
61+
})
62+
})
63+
})
64+
}

0 commit comments

Comments
 (0)