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

Strict type checking + custom cacheKeyFn gives a compile error #62

Open
vincentB-lemon opened this issue Oct 27, 2023 · 0 comments
Open

Comments

@vincentB-lemon
Copy link

vincentB-lemon commented Oct 27, 2023

Hi,

For one of our dataloaders in our NestJS GraphQL API, we have a composite key. Initially we had configured the dataloader to use this composite key as a tuple, but I noticed that this resulted in an issue with caching. The cache map has duplicated keys. When it eventually performs the SQL query, there are a lot of AND statements tied together with OR statements (one AND statement per composite key).

While this, performance wise does not cause issues, I still wanted to look into it. I tried using the cacheKeyFn function to transform the tuple into a string, so that the composite key would be saved in the cache as a string, and it would recognize the duplication itself.

This works as expected, but NOT with strict parameter enabled in tsconfig. I have to ignore the file from compilation errors to allow it to run. Below is the code of the creation of our DataLoader. The DataLoader still expects the composite key, as this is also what our repository layer expects. However, as you can see we use the cacheKeyFn to store the key as a string in the cache.

private createCodeLoader() {
      return new DataLoader<[string, string], GeneralCodeDb | null>(
          async (keys: readonly [string, string][]) => {
              return this.codeService.findByCategoriesAndKeys([...keys]);
          },
          {
              cacheKeyFn: (key) => {
                  return key.toString();
              },
          }
      );
  }

When I log the codeDataLoader object, you can see the cache is correctly set.

DataLoader {
  _batchLoadFn: [AsyncFunction (anonymous)],
  _maxBatchSize: Infinity,
  _batchScheduleFn: [Function (anonymous)],
  _cacheKeyFn: [Function: cacheKeyFn],
  _cacheMap: Map(1) { 'CLS_LG,1' => Promise { [GeneralCodeDb] } },
  _batch: {
    hasDispatched: false,
    keys: [],
    callbacks: [],
    cacheHits: [
      [Function (anonymous)],
      [Function (anonymous)],
      [Function (anonymous)],
      [Function (anonymous)],
      [Function (anonymous)],
      [Function (anonymous)],
      [Function (anonymous)],
      [Function (anonymous)]
    ]
  },
  name: null
}

However, enabling the strict parameter, this error is logged:

src/modules/dataloader/dataloader.service.ts:91:17 - error TS2322: Type '(key: [string, string]) => string' is not assignable to type '(key: [string, string]) => [string, string]'.
  Type 'string' is not assignable to type '[string, string]'.

91                 cacheKeyFn: (key) => {

Am I implementing it incorrectly, or is something off in the typechecking?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant