Skip to content

feat(execution): add max coercion errors option to execution context #4366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Kind } from '../../language/kinds';
import { parse } from '../../language/parser';

import {
GraphQLInputObjectType,
GraphQLInterfaceType,
GraphQLList,
GraphQLNonNull,
Expand Down Expand Up @@ -1320,4 +1321,74 @@ describe('Execute: Handles basic execution tasks', () => {
expect(result).to.deep.equal({ data: { foo: { bar: 'bar' } } });
expect(possibleTypes).to.deep.equal([fooObject]);
});

it('uses a different number of max coercion errors', () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
dummy: { type: GraphQLString },
},
}),
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: {
updateUser: {
type: GraphQLString,
args: {
data: {
type: new GraphQLInputObjectType({
name: 'User',
fields: {
email: { type: new GraphQLNonNull(GraphQLString) },
},
}),
},
},
},
},
}),
});

const document = parse(`
mutation ($data: User) {
updateUser(data: $data)
}
`);

const options = {
maxCoercionErrors: 1,
};

const result = executeSync({
schema,
document,
variableValues: {
data: {
email: '',
wrongArg: 'wrong',
wrongArg2: 'wrong',
wrongArg3: 'wrong',
},
},
options,
});

// Returns at least 2 errors, one for the first 'wrongArg', and one for coercion limit
expect(result.errors).to.have.lengthOf(options.maxCoercionErrors + 1);

expectJSON(result).toDeepEqual({
errors: [
{
message:
'Variable "$data" got invalid value { email: "", wrongArg: "wrong", wrongArg2: "wrong", wrongArg3: "wrong" }; Field "wrongArg" is not defined by type "User".',
locations: [{ line: 2, column: 17 }],
},
{
message:
'Too many errors processing variables, error limit reached. Execution aborted.',
},
],
});
});
});
8 changes: 7 additions & 1 deletion src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ export interface ExecutionArgs {
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
/** Additional execution options. */
options?: {
/** Set the maximum number of errors allowed for coercing (defaults to 50). */
maxCoercionErrors?: number;
};
}

/**
Expand Down Expand Up @@ -286,6 +291,7 @@ export function buildExecutionContext(
fieldResolver,
typeResolver,
subscribeFieldResolver,
options,
} = args;

let operation: OperationDefinitionNode | undefined;
Expand Down Expand Up @@ -329,7 +335,7 @@ export function buildExecutionContext(
schema,
variableDefinitions,
rawVariableValues ?? {},
{ maxErrors: 50 },
{ maxErrors: options?.maxCoercionErrors ?? 50 },
);

if (coercedVariableValues.errors) {
Expand Down