Skip to content

Commit 23775d6

Browse files
author
Cris Naranjo
committed
feat(execution): add max coercion errors option to execution context
1 parent 0a848cc commit 23775d6

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/execution/__tests__/executor-test.ts

+66
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Kind } from '../../language/kinds';
1111
import { parse } from '../../language/parser';
1212

1313
import {
14+
GraphQLInputObjectType,
1415
GraphQLInterfaceType,
1516
GraphQLList,
1617
GraphQLNonNull,
@@ -1320,4 +1321,69 @@ describe('Execute: Handles basic execution tasks', () => {
13201321
expect(result).to.deep.equal({ data: { foo: { bar: 'bar' } } });
13211322
expect(possibleTypes).to.deep.equal([fooObject]);
13221323
});
1324+
1325+
it('uses a different number of max coercion errors', () => {
1326+
const schema = new GraphQLSchema({
1327+
query: new GraphQLObjectType({
1328+
name: 'Query',
1329+
fields: {
1330+
dummy: { type: GraphQLString },
1331+
},
1332+
}),
1333+
mutation: new GraphQLObjectType({
1334+
name: 'Mutation',
1335+
fields: {
1336+
updateUser: {
1337+
type: GraphQLString,
1338+
args: {
1339+
data: {
1340+
type: new GraphQLInputObjectType({
1341+
name: 'User',
1342+
fields: {
1343+
email: { type: new GraphQLNonNull(GraphQLString) },
1344+
},
1345+
}),
1346+
},
1347+
},
1348+
},
1349+
},
1350+
}),
1351+
});
1352+
1353+
const document = parse(`
1354+
mutation ($data: User) {
1355+
updateUser(data: $data)
1356+
}
1357+
`);
1358+
1359+
const executionOptions = {
1360+
maxCoercionErrors: 1,
1361+
};
1362+
1363+
const result = executeSync({
1364+
schema,
1365+
document,
1366+
variableValues: {
1367+
data: {
1368+
email: '',
1369+
wrongArg: 'wrong',
1370+
wrongArg2: 'wrong',
1371+
wrongArg3: 'wrong',
1372+
},
1373+
},
1374+
executionOptions,
1375+
});
1376+
1377+
// Returns at least 2 errors, one for the first 'wrongArg', and one for coercion limit
1378+
expect(result.errors).to.have.lengthOf(
1379+
executionOptions.maxCoercionErrors + 1,
1380+
);
1381+
expect(
1382+
result.errors && result.errors.length > 0
1383+
? result.errors[result.errors.length - 1].message
1384+
: undefined,
1385+
).to.equal(
1386+
'Too many errors processing variables, error limit reached. Execution aborted.',
1387+
);
1388+
});
13231389
});

src/execution/execute.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ export interface ExecutionArgs {
152152
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
153153
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
154154
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
155+
executionOptions?: {
156+
maxCoercionErrors?: number;
157+
};
155158
}
156159

157160
/**
@@ -286,6 +289,7 @@ export function buildExecutionContext(
286289
fieldResolver,
287290
typeResolver,
288291
subscribeFieldResolver,
292+
executionOptions,
289293
} = args;
290294

291295
let operation: OperationDefinitionNode | undefined;
@@ -329,7 +333,7 @@ export function buildExecutionContext(
329333
schema,
330334
variableDefinitions,
331335
rawVariableValues ?? {},
332-
{ maxErrors: 50 },
336+
{ maxErrors: executionOptions?.maxCoercionErrors ?? 50 },
333337
);
334338

335339
if (coercedVariableValues.errors) {

0 commit comments

Comments
 (0)