Skip to content

feat: Allow configuration of graphql execution options(maxCoercionErrors) #8062

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions .changeset/curly-friends-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@apollo/server': minor
---

Allow configuration of graphql execution options (maxCoercionErrors)

```js
const server = new ApolloServer({
typeDefs,
resolvers,
executionOptions: {
maxCoercionErrors: 50,
},
});
```
3 changes: 3 additions & 0 deletions packages/server/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
print,
printSchema,
type DocumentNode,
type ExecutionArgs,
type FormattedExecutionResult,
type GraphQLFieldResolver,
type GraphQLFormattedError,
Expand Down Expand Up @@ -149,6 +150,7 @@ export interface ApolloServerInternals<TContext extends BaseContext> {
apolloConfig: ApolloConfig;
plugins: ApolloServerPlugin<TContext>[];
parseOptions: ParseOptions;
executionOptions?: ExecutionArgs['options'];
// `undefined` means we figure out what to do during _start (because
// the default depends on whether or not we used the background version
// of start).
Expand Down Expand Up @@ -329,6 +331,7 @@ export class ApolloServer<in out TContext extends BaseContext = BaseContext> {
// `start()` will call `addDefaultPlugins` to add default plugins.
plugins: config.plugins ?? [],
parseOptions: config.parseOptions ?? {},
executionOptions: config.executionOptions ?? {},
state,
stopOnTerminationSignals: config.stopOnTerminationSignals,

Expand Down
28 changes: 28 additions & 0 deletions packages/server/src/__tests__/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,34 @@ describe('ApolloServer executeOperation', () => {
await server.stop();
});

it('variable coercion errors, configure max number of errors', async () => {
const server = new ApolloServer({
typeDefs,
resolvers,
status400ForVariableCoercionErrors: true,
executionOptions: {
maxCoercionErrors: 1,
},
});
await server.start();
const { body, http } = await server.executeOperation({
query: `#graphql
query NeedsArg($arg: CompoundInput!) { needsCompoundArg(aCompound: $arg) }
`,
variables: {
arg: { compound: { error1: '1', error2: '2', error3: '3' } },
},
});
const result = singleResult(body);
expect(result.errors).toHaveLength(2);
expect(result.errors?.[0].extensions?.code).toBe('BAD_USER_INPUT');
expect(result.errors?.[1].extensions?.code).toBe('INTERNAL_SERVER_ERROR');
expect(result.errors?.[1].message).toMatch(
'Too many errors processing variables, error limit reached. Execution aborted.',
);
expect(http.status).toBe(400);
});

it('passes its second argument as context object', async () => {
const server = new ApolloServer({
typeDefs,
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/externalTypes/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Logger } from '@apollo/utils.logger';
import type { IExecutableSchemaDefinition } from '@graphql-tools/schema';
import type {
DocumentNode,
ExecutionArgs,
FormattedExecutionResult,
GraphQLFieldResolver,
GraphQLFormattedError,
Expand Down Expand Up @@ -108,6 +109,8 @@ interface ApolloServerOptionsBase<TContext extends BaseContext> {
// parsing the schema.
parseOptions?: ParseOptions;

executionOptions?: ExecutionArgs['options'];

// TODO(AS5): remove OR warn + ignore with this option set, ignore option and
// flip default behavior. Default false. This opt-in configuration fixes a
// regression introduced in v4. In v3, Apollo Server would correctly respond
Expand Down
49 changes: 25 additions & 24 deletions packages/server/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,35 +541,36 @@ export async function processGraphQLRequest<TContext extends BaseContext>(

if (internals.__testing_incrementalExecutionResults) {
return internals.__testing_incrementalExecutionResults;
} else if (internals.gatewayExecutor) {
}
if (internals.gatewayExecutor) {
const result = await internals.gatewayExecutor(
makeGatewayGraphQLRequestContext(requestContext, server, internals),
);
return { singleResult: result };
} else {
const resultOrResults = await executeIncrementally({
schema: schemaDerivedData.schema,
document,
rootValue:
typeof internals.rootValue === 'function'
? internals.rootValue(document)
: internals.rootValue,
contextValue: requestContext.contextValue,
variableValues: request.variables,
operationName: request.operationName,
fieldResolver: internals.fieldResolver,
});
if ('initialResult' in resultOrResults) {
return {
initialResult: resultOrResults.initialResult,
subsequentResults: formatErrorsInSubsequentResults(
resultOrResults.subsequentResults,
),
};
} else {
return { singleResult: resultOrResults };
}
}

const resultOrResults = await executeIncrementally({
schema: schemaDerivedData.schema,
document,
rootValue:
typeof internals.rootValue === 'function'
? internals.rootValue(document)
: internals.rootValue,
contextValue: requestContext.contextValue,
variableValues: request.variables,
operationName: request.operationName,
fieldResolver: internals.fieldResolver,
options: internals.executionOptions,
});
if ('initialResult' in resultOrResults) {
return {
initialResult: resultOrResults.initialResult,
subsequentResults: formatErrorsInSubsequentResults(
resultOrResults.subsequentResults,
),
};
}
return { singleResult: resultOrResults };
}

async function* formatErrorsInSubsequentResults(
Expand Down