Skip to content

Commit 29df455

Browse files
committed
Add CompatibleValidationContext for use in tests to continue to support the older getErrors API.
1 parent 90d39f4 commit 29df455

File tree

5 files changed

+92
-76
lines changed

5 files changed

+92
-76
lines changed

src/__tests__/QueryComplexity-test.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
*/
44

55
import {
6-
GraphQLError,
76
parse,
87
TypeInfo,
9-
ValidationContext,
108
visit,
119
visitWithTypeInfo,
1210
} from 'graphql';
@@ -21,6 +19,7 @@ import {
2119
directiveEstimator,
2220
fieldExtensionsEstimator,
2321
} from '../index';
22+
import { CompatibleValidationContext } from './fixtures/CompatibleValidationContext';
2423

2524
describe('QueryComplexity analysis', () => {
2625
const typeInfo = new TypeInfo(schema);
@@ -155,7 +154,7 @@ describe('QueryComplexity analysis', () => {
155154
}
156155
`);
157156

158-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
157+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
159158
const visitor = new ComplexityVisitor(context, {
160159
maximumComplexity: 100,
161160
estimators: [
@@ -174,8 +173,7 @@ describe('QueryComplexity analysis', () => {
174173
}
175174
`);
176175

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

189187
visit(ast, visitWithTypeInfo(typeInfo, visitor));
190188
expect(visitor.complexity).to.equal(1000);
191-
expect(validationErrors.length).to.equal(1);
192-
expect(validationErrors[0].message).to.equal(
189+
expect(context.getErrors().length).to.equal(1);
190+
expect(context.getErrors()[0].message).to.equal(
193191
'The query exceeds the maximum complexity of 100. Actual complexity is 1000'
194192
);
195193
});
@@ -205,7 +203,7 @@ describe('QueryComplexity analysis', () => {
205203
}
206204
`);
207205

208-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
206+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
209207
const visitor = new ComplexityVisitor(context, {
210208
maximumComplexity: 100,
211209
estimators: [
@@ -232,7 +230,7 @@ describe('QueryComplexity analysis', () => {
232230
}
233231
`);
234232

235-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
233+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
236234
const visitor = new ComplexityVisitor(context, {
237235
maximumComplexity: 100,
238236
estimators: [
@@ -259,7 +257,7 @@ describe('QueryComplexity analysis', () => {
259257
}
260258
`);
261259

262-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
260+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
263261
const visitor = new ComplexityVisitor(context, {
264262
maximumComplexity: 100,
265263
estimators: [
@@ -286,7 +284,7 @@ describe('QueryComplexity analysis', () => {
286284
}
287285
`);
288286

289-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
287+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
290288
const visitor = new ComplexityVisitor(context, {
291289
maximumComplexity: 100,
292290
estimators: [
@@ -312,7 +310,7 @@ describe('QueryComplexity analysis', () => {
312310
}
313311
`);
314312

315-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
313+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
316314
const visitor = new ComplexityVisitor(context, {
317315
maximumComplexity: 100,
318316
estimators: [
@@ -334,7 +332,7 @@ describe('QueryComplexity analysis', () => {
334332
}
335333
`);
336334

337-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
335+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
338336
const visitor = new ComplexityVisitor(context, {
339337
maximumComplexity: 100,
340338
estimators: [
@@ -355,8 +353,7 @@ describe('QueryComplexity analysis', () => {
355353
requiredArgs
356354
}
357355
`);
358-
const validationErrors: GraphQLError[] = [];
359-
const context = new ValidationContext(schema, ast, typeInfo, err => validationErrors.push(err));
356+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
360357
const visitor = new ComplexityVisitor(context, {
361358
maximumComplexity: 100,
362359
estimators: [
@@ -367,8 +364,8 @@ describe('QueryComplexity analysis', () => {
367364
]
368365
});
369366
visit(ast, visitWithTypeInfo(typeInfo, visitor));
370-
expect(validationErrors.length).to.equal(1);
371-
expect(validationErrors[0].message).to.equal('Argument "count" of required type "Int!" was not provided.');
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.');
372369
});
373370

374371
it('should report error when no estimator is configured', () => {
@@ -377,15 +374,14 @@ describe('QueryComplexity analysis', () => {
377374
scalar
378375
}
379376
`);
380-
const validationErrors: GraphQLError[] = [];
381-
const context = new ValidationContext(schema, ast, typeInfo, err => validationErrors.push(err));
377+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
382378
const visitor = new ComplexityVisitor(context, {
383379
maximumComplexity: 100,
384380
estimators: []
385381
});
386382
visit(ast, visitWithTypeInfo(typeInfo, visitor));
387-
expect(validationErrors.length).to.equal(1);
388-
expect(validationErrors[0].message).to.equal(
383+
expect(context.getErrors().length).to.equal(1);
384+
expect(context.getErrors()[0].message).to.equal(
389385
'No complexity could be calculated for field Query.scalar. ' +
390386
'At least one complexity estimator has to return a complexity score.'
391387
);
@@ -397,17 +393,16 @@ describe('QueryComplexity analysis', () => {
397393
scalar
398394
}
399395
`);
400-
const validationErrors: GraphQLError[] = [];
401-
const context = new ValidationContext(schema, ast, typeInfo, err => validationErrors.push(err));
396+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
402397
const visitor = new ComplexityVisitor(context, {
403398
maximumComplexity: 100,
404399
estimators: [
405400
fieldExtensionsEstimator()
406401
]
407402
});
408403
visit(ast, visitWithTypeInfo(typeInfo, visitor));
409-
expect(validationErrors.length).to.equal(1);
410-
expect(validationErrors[0].message).to.equal(
404+
expect(context.getErrors().length).to.equal(1);
405+
expect(context.getErrors()[0].message).to.equal(
411406
'No complexity could be calculated for field Query.scalar. ' +
412407
'At least one complexity estimator has to return a complexity score.'
413408
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { GraphQLError, TypeInfo, ValidationContext } from "graphql";
2+
import { GraphQLSchema } from "graphql/type/schema";
3+
import { DocumentNode } from "graphql/language/ast";
4+
5+
/**
6+
* This class is used to test that validation errors are raised correctly
7+
*
8+
* A compatibility layer is necessary to support graphql versions since 15.0.0
9+
* *as well as* versions prior to 14.5.0 with the same test, because older
10+
* versions of `ValidationContext` only expose a `getErrors` API and newer
11+
* versions only expose the `onError` API via a fourth constructor argument.
12+
*
13+
* Once we drop support for versions older than 14.5.0 this layer will no
14+
* longer be necessary and tests may use `ValidationContext` directly using the
15+
* `onError` API.
16+
*/
17+
export class CompatibleValidationContext extends ValidationContext {
18+
private errors: GraphQLError[] = []
19+
20+
constructor(schema: GraphQLSchema, ast: DocumentNode, typeInfo: TypeInfo) {
21+
super(schema, ast, typeInfo, err => this.errors.push(err));
22+
}
23+
24+
getErrors(): ReadonlyArray<GraphQLError> {
25+
// @ts-ignore
26+
return super.getErrors ? super.getErrors() : this.errors
27+
}
28+
}

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
*/
44

55
import {
6-
GraphQLError,
76
parse,
87
TypeInfo,
9-
ValidationContext,
108
visit,
119
visitWithTypeInfo,
1210
} from 'graphql';
@@ -17,6 +15,7 @@ import schema from './fixtures/schema';
1715

1816
import ComplexityVisitor from '../../../QueryComplexity';
1917
import directiveEstimator from '../index';
18+
import { CompatibleValidationContext } from '../../../__tests__/fixtures/CompatibleValidationContext';
2019

2120
describe('directiveEstimator analysis', () => {
2221
const typeInfo = new TypeInfo(schema);
@@ -28,7 +27,7 @@ describe('directiveEstimator analysis', () => {
2827
}
2928
`);
3029

31-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
30+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
3231
const visitor = new ComplexityVisitor(context, {
3332
maximumComplexity: 100,
3433
estimators: [
@@ -47,7 +46,7 @@ describe('directiveEstimator analysis', () => {
4746
}
4847
`);
4948

50-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
49+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
5150
const visitor = new ComplexityVisitor(context, {
5251
maximumComplexity: 100,
5352
estimators: [
@@ -66,7 +65,7 @@ describe('directiveEstimator analysis', () => {
6665
}
6766
`);
6867

69-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
68+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
7069
const visitor = new ComplexityVisitor(context, {
7170
maximumComplexity: 100,
7271
estimators: [
@@ -85,7 +84,7 @@ describe('directiveEstimator analysis', () => {
8584
}
8685
`);
8786

88-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
87+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
8988
const visitor = new ComplexityVisitor(context, {
9089
maximumComplexity: 100,
9190
estimators: [
@@ -108,7 +107,7 @@ describe('directiveEstimator analysis', () => {
108107
}
109108
`);
110109

111-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
110+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
112111
const visitor = new ComplexityVisitor(context, {
113112
maximumComplexity: 100,
114113
estimators: [
@@ -129,7 +128,7 @@ describe('directiveEstimator analysis', () => {
129128
}
130129
`);
131130

132-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
131+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
133132
const visitor = new ComplexityVisitor(context, {
134133
maximumComplexity: 100,
135134
estimators: [
@@ -150,7 +149,7 @@ describe('directiveEstimator analysis', () => {
150149
}
151150
`);
152151

153-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
152+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
154153
const visitor = new ComplexityVisitor(context, {
155154
maximumComplexity: 100,
156155
estimators: [
@@ -171,7 +170,7 @@ describe('directiveEstimator analysis', () => {
171170
}
172171
`);
173172

174-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
173+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
175174
const visitor = new ComplexityVisitor(context, {
176175
maximumComplexity: 100,
177176
estimators: [
@@ -192,7 +191,7 @@ describe('directiveEstimator analysis', () => {
192191
}
193192
`);
194193

195-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
194+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
196195
const visitor = new ComplexityVisitor(context, {
197196
maximumComplexity: 100,
198197
estimators: [
@@ -213,7 +212,7 @@ describe('directiveEstimator analysis', () => {
213212
}
214213
`);
215214

216-
const context = new ValidationContext(schema, ast, typeInfo, () => null);
215+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
217216
const visitor = new ComplexityVisitor(context, {
218217
maximumComplexity: 100,
219218
estimators: [
@@ -232,8 +231,7 @@ describe('directiveEstimator analysis', () => {
232231
}
233232
`);
234233

235-
const validationErrors: GraphQLError[] = [];
236-
const context = new ValidationContext(schema, ast, typeInfo, err => validationErrors.push(err));
234+
const context = new CompatibleValidationContext(schema, ast, typeInfo);
237235
const visitor = new ComplexityVisitor(context, {
238236
maximumComplexity: 100,
239237
estimators: [
@@ -242,8 +240,8 @@ describe('directiveEstimator analysis', () => {
242240
});
243241

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

0 commit comments

Comments
 (0)