Skip to content

Commit

Permalink
Add disableValidation option to apollo-server-core
Browse files Browse the repository at this point in the history
  • Loading branch information
ganemone committed Nov 13, 2023
1 parent ea2e2c3 commit 11f5981
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
14 changes: 14 additions & 0 deletions docs/source/api/apollo-server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ An array containing custom functions to use as additional [validation rules](htt
</tr>


<tr>
<td>

##### `disableValidation`

`Boolean`
</td>
<td>

Option to disable validation of graphql operations entirely.
</td>
</tr>


<tr>
<td>

Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ export class ApolloServerBase<
logger: this.logger,
plugins: this.plugins,
documentStore,
disableValidation: this.config.disableValidation,
context,
parseOptions: this.parseOptions,
...this.requestOptions,
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-core/src/graphqlOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface GraphQLServerOptions<
persistedQueries?: PersistedQueryOptions;
plugins?: ApolloServerPlugin[];
documentStore?: DocumentStore | null;
disableValidation?: boolean;
parseOptions?: ParseOptions;
nodeEnv?: string;
allowBatchedHttpRequests?: boolean;
Expand Down
23 changes: 13 additions & 10 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface GraphQLRequestPipelineConfig<TContext> {
) => GraphQLResponse | null;

plugins?: ApolloServerPlugin[];
disableValidation?: boolean;
documentStore?: DocumentStore | null;

parseOptions?: ParseOptions;
Expand Down Expand Up @@ -257,18 +258,20 @@ export async function processGraphQLRequest<TContext extends BaseContext>(
return await sendErrorResponse(syntaxError as GraphQLError, SyntaxError);
}

const validationDidEnd = await dispatcher.invokeDidStartHook(
'validationDidStart',
requestContext as GraphQLRequestContextValidationDidStart<TContext>,
);
if (config.disableValidation !== true) {
const validationDidEnd = await dispatcher.invokeDidStartHook(
'validationDidStart',
requestContext as GraphQLRequestContextValidationDidStart<TContext>,
);

const validationErrors = validate(requestContext.document);
const validationErrors = validate(requestContext.document);

if (validationErrors.length === 0) {
await validationDidEnd();
} else {
await validationDidEnd(validationErrors);
return await sendErrorResponse(validationErrors, ValidationError);
if (validationErrors.length === 0) {
await validationDidEnd();
} else {
await validationDidEnd(validationErrors);
return await sendErrorResponse(validationErrors, ValidationError);
}
}

if (config.documentStore) {
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export async function runHttpQuery(
// passed in.
cache: options.cache!,
dataSources: options.dataSources,
disableValidation: options.disableValidation,
documentStore: options.documentStore,

persistedQueries: options.persistedQueries,
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface Config<ContextFunctionParams = any> extends BaseConfig {
stopOnTerminationSignals?: boolean;
apollo?: ApolloConfigInput;
nodeEnv?: string;
disableValidation?: boolean;
documentStore?: DocumentStore | null;
csrfPrevention?: CSRFPreventionOptions | boolean;
cache?: KeyValueCache | 'bounded';
Expand Down
16 changes: 16 additions & 0 deletions packages/apollo-server-integration-testsuite/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ export function testApolloServer<AS extends ApolloServerBase>(
);
});

it('Allows disabling query validation', async () => {
const { url: uri } = await createApolloServer({
schema,
stopOnTerminationSignals: false,
nodeEnv: 'production',
cache: 'bounded',
disableValidation: true,
});

const apolloFetch = createApolloFetch({ uri });

const result = await apolloFetch({ query: INTROSPECTION_QUERY });
expect(result.errors).toBeUndefined();
expect(result.data).toBeDefined();
});

it('allows introspection to be enabled explicitly', async () => {
const { url: uri } = await createApolloServer({
schema,
Expand Down

0 comments on commit 11f5981

Please sign in to comment.