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

Merged
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)) {
martijnvdbrug marked this conversation as resolved.
Show resolved Hide resolved
// 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.
Copy link
Member

Choose a reason for hiding this comment

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

I think we should note that the ctx is not the RequestContext object here, since I think a lot of people will make that assumption based on typical Vendure patterns.

Maybe even rename it to context to make it even more explicit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@michaelbromley Valid concern. Fixed :-)

*
* @example
* ```ts
* HardenPlugin.init({
* skip: (ctx) => ctx.request.http.headers['x-storefront-ssr-auth'] === 'some-secret-token'
* }),
* ```
*/
skip?: (ctx: GraphQLRequestContext<any>) => boolean;
}
Loading