Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nestjs displays error during production that: "Persisted queries are enabled and are using an unbounded cache."

Nestjs application does not display the error message during development only during production. I found no module where the apollo -server can be configured to cache: "bounded". The Nestjs documentation itself makes no mention of it anywhere. The complete error message says:

Persisted queries are enabled and are using an unbounded cache. Your server is vulnerable to denial of service attacks via memory exhaustion. Set cache: "bounded" or persistedQueries: false in your ApolloServer constructor, or see https://go.apollo.dev/s/cache-backends for other alternatives. Here are some dependencies I suspect could be related to it.

"@nestjs/apollo": "10.0.19",
"@nestjs/common": "9.0.5",
"@nestjs/core": "9.0.5",
"@nestjs/graphql": "10.0.20",

A similar issue was opened at github and sadly it was closed without any solution.

like image 277
Nditah Avatar asked Oct 21 '25 05:10

Nditah


2 Answers

This is related to the Apollo module's configuration, by default the cache is "unbounded" and not safe for production (see : https://www.apollographql.com/docs/apollo-server/performance/cache-backends/#ensuring-a-bounded-cache)

You can easily follow their recommendation by adding the optional "cache" configuration inside your GraphQL module.

GraphQLModule.forRoot<ApolloDriverConfig>({
  driver: ApolloDriver,
  cache: 'bounded', // <--- This option
  ...
}

Or provide an external caching with KeyvAdapter see: https://github.com/apollographql/apollo-utils/tree/main/packages/keyvAdapter#keyvadapter-class

like image 144
M4n0x Avatar answered Oct 24 '25 09:10

M4n0x


In the app.module.ts inside the import array,

    GraphQLModule.forRootAsync<ApolloDriverConfig>({
      driver: ApolloDriver,
      useClass: GqlConfigService,
    }),

Then in the GqlConfigService I add cache: 'bounded' in the ApolloDriverConfig options.

import { ConfigService } from '@nestjs/config';
import { ApolloDriverConfig } from '@nestjs/apollo';
import { Injectable } from '@nestjs/common';
import { GqlOptionsFactory } from '@nestjs/graphql';
import { GraphqlConfig } from './config.interface';

@Injectable()
export class GqlConfigService implements GqlOptionsFactory {
  constructor(private configService: ConfigService) {}
  createGqlOptions(): ApolloDriverConfig {
    const graphqlConfig = this.configService.get<GraphqlConfig>('graphql');
    return {
      // schema options
      cache: 'bounded', // ! <== Added here
      autoSchemaFile: graphqlConfig.schemaDestination || './src/schema.graphql',
      sortSchema: graphqlConfig.sortSchema,
      buildSchemaOptions: {
        numberScalarMode: 'integer',
      },
      // subscription
      installSubscriptionHandlers: true,
      debug: graphqlConfig.debug,
      playground: graphqlConfig.playgroundEnabled,
      context: ({ req }) => ({ req }),
    };
  }
}
like image 22
Nditah Avatar answered Oct 24 '25 10:10

Nditah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!