如何实现自定义 Apollo Server KeyValueCache?

How to implement custom Apollo Server KeyValueCache?

我正在寻找使用自定义 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 为空,因此不会返回任何内容。我不知道我错过了什么。

感谢任何帮助。

属性 cachedInfo 没有初始化器,在构造函数中没有明确赋值。

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

一个工作示例:https://github.com/mrdulin/apollo-graphql-tutorial/tree/master/src/Whosebug/60977550