Skip to content

Commit f5928cd

Browse files
committed
correctly handle missing required arguments
1 parent d28ac79 commit f5928cd

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/QueryComplexity.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ export default class QueryComplexity {
138138
const fieldType = getNamedType(field.type);
139139

140140
// Get arguments
141-
const args = getArgumentValues(field, childNode, this.options.variables || {});
141+
let args;
142+
try {
143+
args = getArgumentValues(field, childNode, this.options.variables || {});
144+
} catch (e) {
145+
return this.context.reportError(e);
146+
}
142147

143148
// Check if we have child complexity
144149
let childComplexity = 0;

src/__tests__/QueryComplexity-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,19 @@ describe('QueryComplexity analysis', () => {
223223
visit(ast, visitWithTypeInfo(typeInfo, visitor));
224224
expect(visitor.complexity).to.equal(1);
225225
});
226+
227+
it('should error on a missing non-null argument', () => {
228+
const ast = parse(`
229+
query {
230+
requiredArgs(notcount: 0)
231+
}
232+
`);
233+
const context = new ValidationContext(schema, ast, typeInfo);
234+
const visitor = new ComplexityVisitor(context, {
235+
maximumComplexity: 100
236+
});
237+
visit(ast, visitWithTypeInfo(typeInfo, visitor));
238+
expect(context.getErrors().length).to.equal(1);
239+
expect(context.getErrors()[0].message).to.equal('Argument "count" of required type "Int!" was not provided.');
240+
});
226241
});

src/__tests__/fixtures/schema.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ const Query = new GraphQLObjectType({
119119
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(Item))),
120120
resolve: () => [],
121121
},
122+
requiredArgs: {
123+
type: Item,
124+
args: {
125+
count: {
126+
type: new GraphQLNonNull(GraphQLInt)
127+
}
128+
}
129+
}
122130
}),
123131
});
124132

0 commit comments

Comments
 (0)