Skip to content

Commit 908ad42

Browse files
committed
Merge branch 'Piefayth-required-args'
2 parents d28ac79 + 770d801 commit 908ad42

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
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: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import ComplexityVisitor from '../QueryComplexity';
2020

2121
describe('QueryComplexity analysis', () => {
2222
const typeInfo = new TypeInfo(schema);
23-
23+
2424
it('should consider default scalar cost', () => {
2525
const ast = parse(`
2626
query {
@@ -36,7 +36,7 @@ describe('QueryComplexity analysis', () => {
3636
visit(ast, visitWithTypeInfo(typeInfo, visitor));
3737
expect(visitor.complexity).to.equal(1);
3838
});
39-
39+
4040
it('should consider custom scalar cost', () => {
4141
const ast = parse(`
4242
query {
@@ -52,7 +52,7 @@ describe('QueryComplexity analysis', () => {
5252
visit(ast, visitWithTypeInfo(typeInfo, visitor));
5353
expect(visitor.complexity).to.equal(20);
5454
});
55-
55+
5656
it('should consider variable scalar cost', () => {
5757
const ast = parse(`
5858
query {
@@ -68,7 +68,7 @@ describe('QueryComplexity analysis', () => {
6868
visit(ast, visitWithTypeInfo(typeInfo, visitor));
6969
expect(visitor.complexity).to.equal(1000);
7070
});
71-
71+
7272
it('should not allow negative cost', () => {
7373
const ast = parse(`
7474
query {
@@ -84,7 +84,7 @@ describe('QueryComplexity analysis', () => {
8484
visit(ast, visitWithTypeInfo(typeInfo, visitor));
8585
expect(visitor.complexity).to.equal(0);
8686
});
87-
87+
8888
it('should report error above threshold', () => {
8989
const ast = parse(`
9090
query {
@@ -104,7 +104,7 @@ describe('QueryComplexity analysis', () => {
104104
'The query exceeds the maximum complexity of 100. Actual complexity is 1000'
105105
);
106106
});
107-
107+
108108
it('should add inline fragments', () => {
109109
const ast = parse(`
110110
query {
@@ -124,14 +124,14 @@ describe('QueryComplexity analysis', () => {
124124
visit(ast, visitWithTypeInfo(typeInfo, visitor));
125125
expect(visitor.complexity).to.equal(52);
126126
});
127-
127+
128128
it('should add fragments', () => {
129129
const ast = parse(`
130130
query {
131131
scalar
132132
...QueryFragment
133133
}
134-
134+
135135
fragment QueryFragment on Query {
136136
variableScalar(count: 2)
137137
}
@@ -145,7 +145,7 @@ describe('QueryComplexity analysis', () => {
145145
visit(ast, visitWithTypeInfo(typeInfo, visitor));
146146
expect(visitor.complexity).to.equal(21);
147147
});
148-
148+
149149
it('should add complexity for union types', () => {
150150
const ast = parse(`
151151
query {
@@ -166,7 +166,7 @@ describe('QueryComplexity analysis', () => {
166166
visit(ast, visitWithTypeInfo(typeInfo, visitor));
167167
expect(visitor.complexity).to.equal(22);
168168
});
169-
169+
170170
it('should add complexity for interface types', () => {
171171
const ast = parse(`
172172
query {
@@ -187,7 +187,7 @@ describe('QueryComplexity analysis', () => {
187187
visit(ast, visitWithTypeInfo(typeInfo, visitor));
188188
expect(visitor.complexity).to.equal(3);
189189
});
190-
190+
191191
it('should add complexity for inline fragments without type condition', () => {
192192
const ast = parse(`
193193
query {
@@ -207,7 +207,7 @@ describe('QueryComplexity analysis', () => {
207207
visit(ast, visitWithTypeInfo(typeInfo, visitor));
208208
expect(visitor.complexity).to.equal(2);
209209
});
210-
210+
211211
it('should add complexity for enum types', () => {
212212
const ast = parse(`
213213
query {
@@ -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
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)