Skip to content

Commit e170f73

Browse files
committed
chore: expose all validation options
1 parent a0ada52 commit e170f73

File tree

6 files changed

+24
-20
lines changed

6 files changed

+24
-20
lines changed

.changeset/validate-options.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@apollo/server': minor
3+
---
4+
5+
Expose `graphql` validation options.
6+
7+
```
8+
const server = new ApolloServer({
9+
typeDefs,
10+
resolvers,
11+
validateOptions: { maxErrors: 10 },
12+
});

.changeset/validation-max-errors.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/server/src/ApolloServer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
assertValidSchema,
1515
print,
1616
printSchema,
17+
type validate,
1718
type DocumentNode,
1819
type FormattedExecutionResult,
1920
type GraphQLFieldResolver,
@@ -152,11 +153,12 @@ type ServerState =
152153
stopError: Error | null;
153154
};
154155

156+
export type ValidateOptions = NonNullable<Parameters<typeof validate>[3]>;
157+
155158
export interface ApolloServerInternals<TContext extends BaseContext> {
156159
state: ServerState;
157160
gatewayExecutor: GatewayExecutor | null;
158161
dangerouslyDisableValidation?: boolean;
159-
validationMaxErrors?: number;
160162
formatError?: (
161163
formattedError: GraphQLFormattedError,
162164
error: unknown,
@@ -168,6 +170,7 @@ export interface ApolloServerInternals<TContext extends BaseContext> {
168170
apolloConfig: ApolloConfig;
169171
plugins: ApolloServerPlugin<TContext>[];
170172
parseOptions: ParseOptions;
173+
validationOptions: ValidateOptions;
171174
// `undefined` means we figure out what to do during _start (because
172175
// the default depends on whether or not we used the background version
173176
// of start).
@@ -305,7 +308,7 @@ export class ApolloServer<in out TContext extends BaseContext = BaseContext> {
305308
hideSchemaDetailsFromClientErrors,
306309
dangerouslyDisableValidation:
307310
config.dangerouslyDisableValidation ?? false,
308-
validationMaxErrors: config.validationMaxErrors,
311+
validationOptions: config.validationOptions ?? {},
309312
fieldResolver: config.fieldResolver,
310313
includeStacktraceInErrorResponses:
311314
config.includeStacktraceInErrorResponses ??

packages/server/src/__tests__/runQuery.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ describe('parsing and validation cache', () => {
12011201
});
12021202
await server.start();
12031203

1204-
const vars = new Array(1000).fill(`$a:a`).join(',');
1204+
const vars = new Array(1000).fill('$a:a').join(',');
12051205
const query = `query aaa (${vars}) { a }`;
12061206

12071207
const res = await server.executeOperation({ query });
@@ -1214,14 +1214,15 @@ describe('parsing and validation cache', () => {
12141214
expect(body.errors).toHaveLength(101);
12151215
await server.stop();
12161216
});
1217+
12171218
it('aborts the validation if max errors more than expected', async () => {
12181219
const server = new ApolloServer({
12191220
schema,
1220-
validationMaxErrors: 1,
1221+
validationOptions: { maxErrors: 1 },
12211222
});
12221223
await server.start();
12231224

1224-
const vars = new Array(1000).fill(`$a:a`).join(',');
1225+
const vars = new Array(1000).fill('$a:a').join(',');
12251226
const query = `query aaa (${vars}) { a }`;
12261227

12271228
const res = await server.executeOperation({ query });

packages/server/src/externalTypes/constructor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { GatewayInterface } from '@apollo/server-gateway-interface';
2020
import type { ApolloServerPlugin } from './plugins.js';
2121
import type { BaseContext } from './index.js';
2222
import type { GraphQLExperimentalIncrementalExecutionResults } from '../incrementalDeliveryPolyfill.js';
23+
import type { ValidateOptions } from '../ApolloServer.js';
2324

2425
export type DocumentStore = KeyValueCache<DocumentNode>;
2526

@@ -101,7 +102,7 @@ interface ApolloServerOptionsBase<TContext extends BaseContext> {
101102
nodeEnv?: string;
102103
documentStore?: DocumentStore | null;
103104
dangerouslyDisableValidation?: boolean;
104-
validationMaxErrors?: number;
105+
validationOptions?: ValidateOptions;
105106
csrfPrevention?: CSRFPreventionOptions | boolean;
106107

107108
// Used for parsing operations; unlike in AS3, this is not also used for

packages/server/src/requestPipeline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export async function processGraphQLRequest<TContext extends BaseContext>(
247247
schemaDerivedData.schema,
248248
requestContext.document,
249249
[...specifiedRules, ...internals.validationRules],
250-
{ maxErrors: internals.validationMaxErrors },
250+
internals.validationOptions,
251251
);
252252

253253
if (validationErrors.length === 0) {

0 commit comments

Comments
 (0)