[英]Graphql query is only resolving _id field, the other fields are null
我的 GraphQL 查询正在将 docID 和 docTitle 解析为 null。 尽管正在从数据库中检索数据。 仅返回 _id 字段。 当我控制台日志时,我可以看到数据。
GraphQL 查询
query{
docs{
docId
docTitle
_id
}
}
我的解析器
const getDocs = async (_parent, _args, context, _info) => {
const docs = await context.app.models.docs.find()
return docs
}
文档架构
type Docs {
_id: ID!
docId: String
docTitle: String
}
在 GraphQL 操场上,这就是我得到的
{
"data": {
"getDocs": [
{
"docId": null,
"docTitle": null,
"_id": "5e7c9007f4c3"
}
]
}
}
但是当我控制台日志时,我能够看到所有字段的数据
[
{
_id: 5e7c9007f4c3,
docId: 'c18c-57e4b5134cbe',
docTitle: 'cloud',
__v: 0
}
]
请问我错过了什么?
我猜从数据库返回的docId
和docTitle
的类型不是明确的字符串,因此,某些类型检查正在某处发生,导致它们的值从输出docs
对象中剥离出来。 尝试向查询添加精益选项,以便将查询结果中的每个文档转换为一个 javascript 对象,这应该有助于确保将docId
和docTitle
值docTitle
为字符串。
将您的数据库查询更新为如下所示:
await context.app.models.docs.find({}, {}, { lean: true })
使用聚合参数也正确解析了查询
const getDocs = async (_parent, _args, context, _info) => {
return await context.app.models.docs.aggregate([
{
$project: { docId:1, docTitle:1 },
},
])}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.