Skip to content

Commit 78a5076

Browse files
committed
fix: check for valid type
fixes #104
1 parent db8fd47 commit 78a5076

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

lib/query-validation-visitor.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ module.exports = class QueryValidationVisitor {
114114
const argName = arg.name.value
115115
// look for directive and validate argument if directive found
116116
const argTypeDef = this.currentrFieldDef.args.find(d => d.name === argName)
117+
118+
if (!argTypeDef) return
119+
117120
const value = valueFromAST(arg.value, argTypeDef.type, this.variableValues)
118121

119122
let variableName

test/string.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,71 @@ module.exports.test = function (setup, implType) {
10641064
})
10651065
})
10661066

1067+
describe('other directives', function () {
1068+
const query = `mutation createBook($input: BookInput, $skipTest: Boolean!) {
1069+
createBook(input: $input) {
1070+
title @skip(if: $skipTest)
1071+
}
1072+
}`
1073+
1074+
before(async function () {
1075+
this.typeDefs = `
1076+
type Query {
1077+
books: [Book]
1078+
}
1079+
type Book {
1080+
title: String
1081+
}
1082+
type Mutation {
1083+
createBook(input: BookInput): Book
1084+
}
1085+
input BookInput {
1086+
title: String! @constraint(minLength: 3)
1087+
}`
1088+
1089+
this.request = await setup(this.typeDefs)
1090+
})
1091+
1092+
it('should pass', async function () {
1093+
const { body, statusCode } = await this.request
1094+
.post('/graphql')
1095+
.set('Accept', 'application/json')
1096+
.send({ query, variables: { input: { title: 'he💩' }, skipTest: false } })
1097+
1098+
strictEqual(statusCode, 200)
1099+
deepStrictEqual(body, { data: { createBook: null } })
1100+
})
1101+
1102+
it('should fail', async function () {
1103+
const { body, statusCode } = await this.request
1104+
.post('/graphql')
1105+
.set('Accept', 'application/json')
1106+
.send({ query, variables: { input: { title: 'a💩' }, skipTest: false } })
1107+
1108+
isStatusCodeError(statusCode, implType)
1109+
strictEqual(body.errors[0].message,
1110+
'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')
1111+
})
1112+
1113+
if (isSchemaWrapperImplType(implType)) {
1114+
it('should throw custom error', async function () {
1115+
const request = await setup(this.typeDefs, formatError)
1116+
const { body, statusCode } = await request
1117+
.post('/graphql')
1118+
.set('Accept', 'application/json')
1119+
.send({ query, variables: { input: { title: 'a💩' }, skipTest: false } })
1120+
1121+
strictEqual(statusCode, 400)
1122+
deepStrictEqual(body.errors[0], {
1123+
message: 'Must be at least 3 characters in length',
1124+
code: 'ERR_GRAPHQL_CONSTRAINT_VALIDATION',
1125+
fieldName: 'title',
1126+
context: [{ arg: 'minLength', value: 3 }]
1127+
})
1128+
})
1129+
}
1130+
})
1131+
10671132
if (isSchemaWrapperImplType(implType)) {
10681133
describe('#uniqueTypeName', function () {
10691134
before(async function () {

0 commit comments

Comments
 (0)