我正在寻找一个使用自定义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
为空,因此不会返回任何内容。我不知道我在想什么。
感谢您的帮助。
答案 0 :(得分:1)
属性cachedInfo
没有初始化程序,并且在构造函数中未明确分配。
将cachedInfo: []
更改为cachedInfo = {};
将解决此问题。
一个有效的示例:https://github.com/mrdulin/apollo-graphql-tutorial/tree/master/src/stackoverflow/60977550