Skip to content

Commit afeea40

Browse files
fix: handle mutation failure (#230)
--------- Co-authored-by: Paul Le Bras <[email protected]>
1 parent 5038019 commit afeea40

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

lib/query-validation-visitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = class QueryValidationVisitor {
9898
this.currentField = node
9999

100100
// this if handles union type correctly
101-
if (this.currentTypeInfo.typeDef.getFields) { this.currentrFieldDef = this.currentTypeInfo.typeDef.getFields()[node.name.value] }
101+
if (this.currentTypeInfo?.typeDef?.getFields) { this.currentrFieldDef = this.currentTypeInfo.typeDef.getFields()[node.name.value] }
102102

103103
if (this.currentrFieldDef) {
104104
const newTypeDef = getNamedType(this.currentrFieldDef.type)

test/mutation-failure.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { strictEqual } = require('assert')
2+
3+
module.exports.test = function (setup, implType) {
4+
describe('GraphQL schema setup', function () {
5+
before(async function () {
6+
this.typeDefs = /* GraphQL */`
7+
type Query {
8+
book: Book
9+
}
10+
type Book {
11+
title: String
12+
}
13+
`
14+
15+
this.request = await setup({ typeDefs: this.typeDefs })
16+
})
17+
18+
describe('Mutation operation failure', function () {
19+
it('should fail but not throw "Cannot read properties of undefined (reading \'getFields\')"', async function () {
20+
const query = /* GraphQL */`
21+
mutation GetBook {
22+
book {
23+
title
24+
}
25+
}
26+
`
27+
28+
const { body, statusCode } = await this.request
29+
.post('/graphql')
30+
.set('Accept', 'application/json')
31+
.send({ query })
32+
33+
strictEqual(
34+
statusCode,
35+
200,
36+
'Expected HTTP status code 200 for successful GraphQL query execution.'
37+
)
38+
strictEqual(
39+
body.errors[0].message,
40+
'Schema is not configured to execute mutation operation.',
41+
'Expected error message indicating failure in mutation operation.'
42+
)
43+
})
44+
})
45+
})
46+
}

test/testsuite-apollo-plugin.js

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

0 commit comments

Comments
 (0)