Skip to content

Commit dff4047

Browse files
committed
Switch to new ValidationContext API, use onError, remove getErrors
1 parent 76dfdba commit dff4047

File tree

7 files changed

+112
-93
lines changed

7 files changed

+112
-93
lines changed

src/QueryComplexity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function getComplexity(options: {
9191
}): number {
9292
const typeInfo = new TypeInfo(options.schema);
9393

94-
const context = new ValidationContext(options.schema, options.query, typeInfo);
94+
const context = new ValidationContext(options.schema, options.query, typeInfo, () => null);
9595
const visitor = new QueryComplexity(context, {
9696
// Maximum complexity does not matter since we're only interested in the calculated complexity.
9797
maximumComplexity: Infinity,

src/__tests__/QueryComplexity-test.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import {
6+
GraphQLError,
67
parse,
78
TypeInfo,
89
ValidationContext,
@@ -154,7 +155,7 @@ describe('QueryComplexity analysis', () => {
154155
}
155156
`);
156157

157-
const context = new ValidationContext(schema, ast, typeInfo);
158+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
158159
const visitor = new ComplexityVisitor(context, {
159160
maximumComplexity: 100,
160161
estimators: [
@@ -173,7 +174,8 @@ describe('QueryComplexity analysis', () => {
173174
}
174175
`);
175176

176-
const context = new ValidationContext(schema, ast, typeInfo);
177+
const validationErrors: GraphQLError[] = [];
178+
const context = new ValidationContext(schema, ast, typeInfo, err => validationErrors.push(err));
177179
const visitor = new ComplexityVisitor(context, {
178180
maximumComplexity: 100,
179181
estimators: [
@@ -186,8 +188,8 @@ describe('QueryComplexity analysis', () => {
186188

187189
visit(ast, visitWithTypeInfo(typeInfo, visitor));
188190
expect(visitor.complexity).to.equal(1000);
189-
expect(context.getErrors().length).to.equal(1);
190-
expect(context.getErrors()[0].message).to.equal(
191+
expect(validationErrors.length).to.equal(1);
192+
expect(validationErrors[0].message).to.equal(
191193
'The query exceeds the maximum complexity of 100. Actual complexity is 1000'
192194
);
193195
});
@@ -203,7 +205,7 @@ describe('QueryComplexity analysis', () => {
203205
}
204206
`);
205207

206-
const context = new ValidationContext(schema, ast, typeInfo);
208+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
207209
const visitor = new ComplexityVisitor(context, {
208210
maximumComplexity: 100,
209211
estimators: [
@@ -230,7 +232,7 @@ describe('QueryComplexity analysis', () => {
230232
}
231233
`);
232234

233-
const context = new ValidationContext(schema, ast, typeInfo);
235+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
234236
const visitor = new ComplexityVisitor(context, {
235237
maximumComplexity: 100,
236238
estimators: [
@@ -257,7 +259,7 @@ describe('QueryComplexity analysis', () => {
257259
}
258260
`);
259261

260-
const context = new ValidationContext(schema, ast, typeInfo);
262+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
261263
const visitor = new ComplexityVisitor(context, {
262264
maximumComplexity: 100,
263265
estimators: [
@@ -284,7 +286,7 @@ describe('QueryComplexity analysis', () => {
284286
}
285287
`);
286288

287-
const context = new ValidationContext(schema, ast, typeInfo);
289+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
288290
const visitor = new ComplexityVisitor(context, {
289291
maximumComplexity: 100,
290292
estimators: [
@@ -310,7 +312,7 @@ describe('QueryComplexity analysis', () => {
310312
}
311313
`);
312314

313-
const context = new ValidationContext(schema, ast, typeInfo);
315+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
314316
const visitor = new ComplexityVisitor(context, {
315317
maximumComplexity: 100,
316318
estimators: [
@@ -332,7 +334,7 @@ describe('QueryComplexity analysis', () => {
332334
}
333335
`);
334336

335-
const context = new ValidationContext(schema, ast, typeInfo);
337+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
336338
const visitor = new ComplexityVisitor(context, {
337339
maximumComplexity: 100,
338340
estimators: [
@@ -353,7 +355,8 @@ describe('QueryComplexity analysis', () => {
353355
requiredArgs
354356
}
355357
`);
356-
const context = new ValidationContext(schema, ast, typeInfo);
358+
const validationErrors: GraphQLError[] = [];
359+
const context = new ValidationContext(schema, ast, typeInfo, err => validationErrors.push(err));
357360
const visitor = new ComplexityVisitor(context, {
358361
maximumComplexity: 100,
359362
estimators: [
@@ -364,8 +367,8 @@ describe('QueryComplexity analysis', () => {
364367
]
365368
});
366369
visit(ast, visitWithTypeInfo(typeInfo, visitor));
367-
expect(context.getErrors().length).to.equal(1);
368-
expect(context.getErrors()[0].message).to.equal('Argument "count" of required type "Int!" was not provided.');
370+
expect(validationErrors.length).to.equal(1);
371+
expect(validationErrors[0].message).to.equal('Argument "count" of required type "Int!" was not provided.');
369372
});
370373

371374
it('should report error when no estimator is configured', () => {
@@ -374,14 +377,15 @@ describe('QueryComplexity analysis', () => {
374377
scalar
375378
}
376379
`);
377-
const context = new ValidationContext(schema, ast, typeInfo);
380+
const validationErrors: GraphQLError[] = [];
381+
const context = new ValidationContext(schema, ast, typeInfo, err => validationErrors.push(err));
378382
const visitor = new ComplexityVisitor(context, {
379383
maximumComplexity: 100,
380384
estimators: []
381385
});
382386
visit(ast, visitWithTypeInfo(typeInfo, visitor));
383-
expect(context.getErrors().length).to.equal(1);
384-
expect(context.getErrors()[0].message).to.equal(
387+
expect(validationErrors.length).to.equal(1);
388+
expect(validationErrors[0].message).to.equal(
385389
'No complexity could be calculated for field Query.scalar. ' +
386390
'At least one complexity estimator has to return a complexity score.'
387391
);
@@ -393,16 +397,17 @@ describe('QueryComplexity analysis', () => {
393397
scalar
394398
}
395399
`);
396-
const context = new ValidationContext(schema, ast, typeInfo);
400+
const validationErrors: GraphQLError[] = [];
401+
const context = new ValidationContext(schema, ast, typeInfo, err => validationErrors.push(err));
397402
const visitor = new ComplexityVisitor(context, {
398403
maximumComplexity: 100,
399404
estimators: [
400405
fieldConfigEstimator()
401406
]
402407
});
403408
visit(ast, visitWithTypeInfo(typeInfo, visitor));
404-
expect(context.getErrors().length).to.equal(1);
405-
expect(context.getErrors()[0].message).to.equal(
409+
expect(validationErrors.length).to.equal(1);
410+
expect(validationErrors[0].message).to.equal(
406411
'No complexity could be calculated for field Query.scalar. ' +
407412
'At least one complexity estimator has to return a complexity score.'
408413
);

src/estimators/directive/__tests__/directiveEstimator-test.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import {
6+
GraphQLError,
67
parse,
78
TypeInfo,
89
ValidationContext,
@@ -27,7 +28,7 @@ describe('directiveEstimator analysis', () => {
2728
}
2829
`);
2930

30-
const context = new ValidationContext(schema, ast, typeInfo);
31+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
3132
const visitor = new ComplexityVisitor(context, {
3233
maximumComplexity: 100,
3334
estimators: [
@@ -46,7 +47,7 @@ describe('directiveEstimator analysis', () => {
4647
}
4748
`);
4849

49-
const context = new ValidationContext(schema, ast, typeInfo);
50+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
5051
const visitor = new ComplexityVisitor(context, {
5152
maximumComplexity: 100,
5253
estimators: [
@@ -65,7 +66,7 @@ describe('directiveEstimator analysis', () => {
6566
}
6667
`);
6768

68-
const context = new ValidationContext(schema, ast, typeInfo);
69+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
6970
const visitor = new ComplexityVisitor(context, {
7071
maximumComplexity: 100,
7172
estimators: [
@@ -84,7 +85,7 @@ describe('directiveEstimator analysis', () => {
8485
}
8586
`);
8687

87-
const context = new ValidationContext(schema, ast, typeInfo);
88+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
8889
const visitor = new ComplexityVisitor(context, {
8990
maximumComplexity: 100,
9091
estimators: [
@@ -107,7 +108,7 @@ describe('directiveEstimator analysis', () => {
107108
}
108109
`);
109110

110-
const context = new ValidationContext(schema, ast, typeInfo);
111+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
111112
const visitor = new ComplexityVisitor(context, {
112113
maximumComplexity: 100,
113114
estimators: [
@@ -128,7 +129,7 @@ describe('directiveEstimator analysis', () => {
128129
}
129130
`);
130131

131-
const context = new ValidationContext(schema, ast, typeInfo);
132+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
132133
const visitor = new ComplexityVisitor(context, {
133134
maximumComplexity: 100,
134135
estimators: [
@@ -149,7 +150,7 @@ describe('directiveEstimator analysis', () => {
149150
}
150151
`);
151152

152-
const context = new ValidationContext(schema, ast, typeInfo);
153+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
153154
const visitor = new ComplexityVisitor(context, {
154155
maximumComplexity: 100,
155156
estimators: [
@@ -170,7 +171,7 @@ describe('directiveEstimator analysis', () => {
170171
}
171172
`);
172173

173-
const context = new ValidationContext(schema, ast, typeInfo);
174+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
174175
const visitor = new ComplexityVisitor(context, {
175176
maximumComplexity: 100,
176177
estimators: [
@@ -191,7 +192,7 @@ describe('directiveEstimator analysis', () => {
191192
}
192193
`);
193194

194-
const context = new ValidationContext(schema, ast, typeInfo);
195+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
195196
const visitor = new ComplexityVisitor(context, {
196197
maximumComplexity: 100,
197198
estimators: [
@@ -212,7 +213,7 @@ describe('directiveEstimator analysis', () => {
212213
}
213214
`);
214215

215-
const context = new ValidationContext(schema, ast, typeInfo);
216+
const context = new ValidationContext(schema, ast, typeInfo, () => null);
216217
const visitor = new ComplexityVisitor(context, {
217218
maximumComplexity: 100,
218219
estimators: [
@@ -231,7 +232,8 @@ describe('directiveEstimator analysis', () => {
231232
}
232233
`);
233234

234-
const context = new ValidationContext(schema, ast, typeInfo);
235+
const validationErrors: GraphQLError[] = [];
236+
const context = new ValidationContext(schema, ast, typeInfo, err => validationErrors.push(err));
235237
const visitor = new ComplexityVisitor(context, {
236238
maximumComplexity: 100,
237239
estimators: [
@@ -240,8 +242,8 @@ describe('directiveEstimator analysis', () => {
240242
});
241243

242244
visit(ast, visitWithTypeInfo(typeInfo, visitor));
243-
expect(visitor.context.getErrors().length).to.equal(1);
244-
expect(visitor.context.getErrors()[0].message).to.include(
245+
expect(validationErrors.length).to.equal(1);
246+
expect(validationErrors[0].message).to.include(
245247
'No complexity could be calculated for field Query.noDirective',
246248
);
247249
});

0 commit comments

Comments
 (0)