Skip to content

Commit 6b26de8

Browse files
authored
fix: handle undefined args (#223)
fixes #224
1 parent c204fbd commit 6b26de8

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

lib/query-validation-visitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ module.exports = class QueryValidationVisitor {
115115
onArgumentEnter (arg) {
116116
const argName = arg.name.value
117117
// look for directive and validate argument if directive found
118-
const argTypeDef = this.currentrFieldDef.args.find(d => d.name === argName)
118+
const argTypeDef = this.currentrFieldDef?.args.find(d => d.name === argName)
119119

120120
if (!argTypeDef) return
121121

test/foreign-query-directives.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const { deepStrictEqual, strictEqual } = require('assert')
2+
3+
module.exports.test = function (setup, implType) {
4+
describe('Directive Argument Handling', function () {
5+
before(async function () {
6+
this.typeDefs = `
7+
directive @component(name: String!) on QUERY | MUTATION | SUBSCRIPTION
8+
9+
type Query {
10+
getUsers: [User]
11+
}
12+
13+
type User {
14+
id: ID
15+
name: String
16+
email: String
17+
}
18+
`
19+
20+
this.request = await setup({ typeDefs: this.typeDefs })
21+
})
22+
23+
it("should not throw \"Cannot read properties of undefined (reading 'args')\" error when querying", async function () {
24+
const query = `
25+
query GetCurrentUser @component(name: "exampleComponent") {
26+
getUsers {
27+
id
28+
name
29+
email
30+
}
31+
}
32+
`
33+
const { body, statusCode } = await this.request
34+
.post('/graphql')
35+
.set('Accept', 'application/json')
36+
.send({ query })
37+
38+
// Check for successful execution and no errors in the response
39+
strictEqual(
40+
statusCode,
41+
200,
42+
'Expected HTTP status code 200 for successful GraphQL query execution.'
43+
)
44+
deepStrictEqual(
45+
body.errors,
46+
undefined,
47+
'Expected no errors in the GraphQL response.'
48+
)
49+
})
50+
})
51+
}

test/testsuite-apollo-plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ describe('Server validator based implementation - Apollo plugin', function () {
1818
require('./string.test').test(setup, IMPL_TYPE)
1919
require('./argument-dynamic.test').test(setup, IMPL_TYPE)
2020
require('./union.test').test(setup, IMPL_TYPE)
21+
require('./foreign-query-directives.test').test(setup, IMPL_TYPE)
2122
})

0 commit comments

Comments
 (0)