@@ -25,6 +25,7 @@ import {
2525} from '..' ;
2626import { mockLogger } from './mockLogger' ;
2727import { jest , describe , it , expect } from '@jest/globals' ;
28+ import { singleResult } from './ApolloServer.test' ;
2829
2930async function runQuery (
3031 config : ApolloServerOptions < BaseContext > ,
@@ -1192,4 +1193,51 @@ describe('parsing and validation cache', () => {
11921193 expect ( parsingDidStart . mock . calls . length ) . toBe ( 6 ) ;
11931194 expect ( validationDidStart . mock . calls . length ) . toBe ( 6 ) ;
11941195 } ) ;
1196+
1197+ describe ( 'validationMaxErrors option' , ( ) => {
1198+ it ( 'should be 100 by default' , async ( ) => {
1199+ const server = new ApolloServer ( {
1200+ schema,
1201+ } ) ;
1202+ await server . start ( ) ;
1203+
1204+ const vars = new Array ( 1000 ) . fill ( `$a:a` ) . join ( ',' ) ;
1205+ const query = `query aaa (${ vars } ) { a }` ;
1206+
1207+ const res = await server . executeOperation ( { query } ) ;
1208+ expect ( res . http . status ) . toBe ( 400 ) ;
1209+
1210+ const body = singleResult ( res . body ) ;
1211+
1212+ // 100 by default plus one "Too many validation errors" error
1213+ // https://github.com/graphql/graphql-js/blob/main/src/validation/validate.ts#L46
1214+ expect ( body . errors ) . toHaveLength ( 101 ) ;
1215+ await server . stop ( ) ;
1216+ } ) ;
1217+ it ( 'aborts the validation if max errors more than expected' , async ( ) => {
1218+ const server = new ApolloServer ( {
1219+ schema,
1220+ validationMaxErrors : 1 ,
1221+ } ) ;
1222+ await server . start ( ) ;
1223+
1224+ const vars = new Array ( 1000 ) . fill ( `$a:a` ) . join ( ',' ) ;
1225+ const query = `query aaa (${ vars } ) { a }` ;
1226+
1227+ const res = await server . executeOperation ( { query } ) ;
1228+ expect ( res . http . status ) . toBe ( 400 ) ;
1229+
1230+ const body = singleResult ( res . body ) ;
1231+
1232+ expect ( body . errors ) . toHaveLength ( 2 ) ;
1233+ expect ( body . errors ?. [ 0 ] ) . toMatchObject ( {
1234+ message : `There can be only one variable named "$a".` ,
1235+ } ) ;
1236+ expect ( body . errors ?. [ 1 ] ) . toMatchObject ( {
1237+ message : `Too many validation errors, error limit reached. Validation aborted.` ,
1238+ } ) ;
1239+
1240+ await server . stop ( ) ;
1241+ } ) ;
1242+ } ) ;
11951243} ) ;
0 commit comments