Skip to content
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

Feat: Allow skipping complexity check in Harden Plugin #3340

Open
wants to merge 2 commits into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions packages/harden-plugin/src/middleware/query-complexity-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import { HardenPluginOptions } from '../types';
export class QueryComplexityPlugin implements ApolloServerPlugin {
constructor(private options: HardenPluginOptions) {}

async requestDidStart({ schema }: GraphQLRequestContext<any>): Promise<GraphQLRequestListener<any>> {
async requestDidStart(ctx: GraphQLRequestContext<any>): Promise<GraphQLRequestListener<any>> {
const maxQueryComplexity = this.options.maxQueryComplexity ?? 1000;
return {
didResolveOperation: async ({ request, document }) => {
if (isAdminApi(schema)) {
if (this.options.skip?.(ctx)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if this supported Awaitable<boolean> in addition to boolean. Supporting promises would allow developers to include external resources, such as making a third-party validation request.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I purposely did not do that, because it can have severe performance impact if you do anything async in every request.

But I guess we can move that responsibility to the consumer.... Let me add it.

// Given skip function tells use we should not check this request for complexity
return;
}
if (isAdminApi(ctx.schema)) {
// We don't want to apply the cost analysis on the
// Admin API, since any expensive operations would require
// an authenticated session.
Expand All @@ -41,7 +45,7 @@ export class QueryComplexityPlugin implements ApolloServerPlugin {
);
}
const complexity = getComplexity({
schema,
schema: ctx.schema,
query,
variables: request.variables,
estimators: this.options.queryComplexityEstimators ?? [
Expand Down
13 changes: 13 additions & 0 deletions packages/harden-plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GraphQLRequestContext } from '@apollo/server';
import { ComplexityEstimator } from 'graphql-query-complexity';

/**
Expand Down Expand Up @@ -79,4 +80,16 @@ export interface HardenPluginOptions {
* @default 'prod'
*/
apiMode?: 'dev' | 'prod';
/**
* @description
* Allows you to skip the complexity check for certain requests.
*
* @example
* ```ts
* HardenPlugin.init({
* skip: (ctx) => ctx.request.http.headers['x-storefront-ssr-auth'] === 'some-secret-token'
* }),
* ```
*/
skip?: (ctx: GraphQLRequestContext<any>) => boolean;
}
Loading