【发布时间】:2018-09-09 02:56:24
【问题描述】:
我正在用 graphql 和 mongodb (mongoose) 构建一个 express JS 应用程序。我正在使用 facebooks Dataloader 来批处理和缓存请求。
除了这个用例之外,它工作得很好。
我有一个充满用户帖子的数据库。每个帖子都包含用户 ID 以供参考。当我打电话返回数据库中的所有帖子时。帖子返回正常,但如果我尝试在每个帖子中获取用户。拥有多个帖子的用户将只返回一个用户,因为第二个用户的密钥已被缓存。所以来自用户“x”的 2 个帖子(键)只会返回 1 个用户对象“x”。
但是,Dataloader 必须返回与其收到的密钥相同数量的承诺。
它有一个选项可以将缓存指定为 false,这样每个键都会发出请求。但这似乎不适用于我的用例。
对不起,如果我没有很好地解释这一点。
这是我的 graphql 请求
query {
getAllPosts {
_id // This is returned fine
user {
_id
}
}
}
返回错误:DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise of an Array of the same length as the Array of keys.
【问题讨论】:
-
你的 DataLoader 是什么样的?
标签: mongodb express mongoose graphql