33 */
44
55import {
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
2524describe ( '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 ) ;
0 commit comments