如何实现自定义Apollo服务器KeyValueCache?

问题描述 投票:1回答:1

我正在寻找使用自定义KeyValueCache实现的Apollo服务器缓存的工作示例-https://www.npmjs.com/package/apollo-server-caching

这是我目前正在使用的代码段。

export default class MyCache implements KeyValueCache {
  cachedInfo: [];

  async set(key: string, value: string, options?: KeyValueCacheSetOptions): Promise<void> {
    this.cachedInfo[key] = value;
  }

  async get(key: string): Promise<string | undefined> {
    if (this.cachedInfo[key]) return this.cachedInfo[key];
  }

  async delete(key: string): Promise<boolean> {
    this.cachedInfo[key] = null;
    return this.cachedInfo[key] === null;
  }
}

到目前为止,每个查询都引发错误...

 "message": "Cannot read property 'fqc:1e5108601dcce15b1e80befed8f799d75322c573a873d274ec466adc4bd52f59' of undefined"

cachedInfo为空,因此不会返回任何内容。我不知道我在想什么。

感谢您的任何帮助。

typescript graphql apollo apollo-server
1个回答
0
投票

属性cachedInfo没有初始化程序,并且在构造函数中未明确分配。

cachedInfo: []更改为cachedInfo = {};将解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.