如何解析graphQl nodejs中查询的mongoDB嵌入式文档数组
我是GraphQL的新手,并且在学习时正在尝试构建应用程序。实际上,我正在尝试编写一个解析器来解决以下模式的查询。 请告诉我如何解决参考物体的源泉:[fefitemschema] 如何获得Farmintake的相应记录进行进食。 我不知道我向您解释了我的问题,但是我敢肯定,当您看到模式的结构时,您会理解我的问题。请帮助我找不到。 当我在下面查询时,它可以正常工作。
query{
feeds{
_id
feedname
feedtime
feedformula{
feeding{
_id
}
qty
mesunit
description
remarks
}creator{
email
}
}
}
但是,当我使用以下查询时会引起错误:
query{
feeds{
_id
feedname
feedtime
feedformula{
feeding{
_id
name
}
qty
mesunit
description
remarks
}creator{
email
}
}
}
{
"errors": [
{
"message": "Cannot return null for non-nullable field
Farmintake.name.",
"locations": [
{
"line": 81,
"column": 8
}
],
"path": [
"feeds",
0,
"feedformula",
0,
"feeding",
"name"
]
},
{
"message": "Cannot return null for non-nullable field
Farmintake.name.",
"locations": [
{
"line": 81,
"column": 8
}
],
"path": [
"feeds",
0,
"feedformula",
1,
"feeding",
"name"
]
},
{
"message": "Cannot return null for non-nullable field
Farmintake.name.",
"locations": [
{
"line": 81,
"column": 8
}
],
"path": [
"feeds",
0,
"feedformula",
2,
"feeding",
"name"
]
}
],
"data": {
"feeds": [
{
"_id": "62b417a91797e5053c2d58a3",
"feedname": "Dana",
"feedtime": "Evening",
"feedformula": [
null,
null,
null
],
"creator": {
"email": "[email protected]"
}
}
]
}
}
遵循模式:
const feeditemSchema = new Schema({
feeding: {type: Schema.Types.ObjectId,ref: 'Farmintake'},
mesunit: {type: String},
qty: { type: Number },
description:{ type: String },
remarks:{ type: String }
});
const feedSchema = new Schema({
feedname:{ type: String },
feedtime:{ type: String },
feedformula:[ feeditemSchema ],
description:{ type: String },
creator: {type: Schema.Types.ObjectId,ref: 'User'}
},{ timestamps: true });
const farmintakeSchema = new Schema({
name:{type: String,required: true},
itmtype:{type: String,required: true},
madein:{type: String,required: true},
mesunit:{type: String},
usage:{type: String},
remarks:{type: String},
creator: {type: Schema.Types.ObjectId,ref: 'User'}
},{ timestamps: true}
);
module.exports = mongoose.model('Feed', feedSchema );
module.exports = mongoose.model('Farmintake',farmintakeSchema);
我尝试关注解析器:
const transformFeed = feed =>{
return {
...feed._doc,
_id: feed.id,
feeding: farmintake.bind(this, feed.feedformula.feeding),
creator: user.bind(this,feed.creator)
};
};
const farmintake = async farmintakeId => {
try{
const farmintake = await Farmintake.findById(farmintakeId);
return {
...farmintake._doc,
_id:farmintake.id
};
}catch (err) {
throw err;
}
};
I am new to Graphql, and I am trying to build an application while I am learning it. Actually I am trying to write a resolver to resolve the query on following schema.
Please tell me how could I resolve the referenced object resides in the array of feedformula: [feeditemSchema]
How to get the corresponding record from farmintake for feeding.
I don't know how clearly I explained my problem to you but i am sure that when you see the structure of schema you will understand my problem. Please help a I am unable to find.
When I query as below it workes.
query{
feeds{
_id
feedname
feedtime
feedformula{
feeding{
_id
}
qty
mesunit
description
remarks
}creator{
email
}
}
}
But when I use below query it raise an error:
query{
feeds{
_id
feedname
feedtime
feedformula{
feeding{
_id
name
}
qty
mesunit
description
remarks
}creator{
email
}
}
}
{
"errors": [
{
"message": "Cannot return null for non-nullable field
Farmintake.name.",
"locations": [
{
"line": 81,
"column": 8
}
],
"path": [
"feeds",
0,
"feedformula",
0,
"feeding",
"name"
]
},
{
"message": "Cannot return null for non-nullable field
Farmintake.name.",
"locations": [
{
"line": 81,
"column": 8
}
],
"path": [
"feeds",
0,
"feedformula",
1,
"feeding",
"name"
]
},
{
"message": "Cannot return null for non-nullable field
Farmintake.name.",
"locations": [
{
"line": 81,
"column": 8
}
],
"path": [
"feeds",
0,
"feedformula",
2,
"feeding",
"name"
]
}
],
"data": {
"feeds": [
{
"_id": "62b417a91797e5053c2d58a3",
"feedname": "Dana",
"feedtime": "Evening",
"feedformula": [
null,
null,
null
],
"creator": {
"email": "[email protected]"
}
}
]
}
}
Following in Schema:
const feeditemSchema = new Schema({
feeding: {type: Schema.Types.ObjectId,ref: 'Farmintake'},
mesunit: {type: String},
qty: { type: Number },
description:{ type: String },
remarks:{ type: String }
});
const feedSchema = new Schema({
feedname:{ type: String },
feedtime:{ type: String },
feedformula:[ feeditemSchema ],
description:{ type: String },
creator: {type: Schema.Types.ObjectId,ref: 'User'}
},{ timestamps: true });
const farmintakeSchema = new Schema({
name:{type: String,required: true},
itmtype:{type: String,required: true},
madein:{type: String,required: true},
mesunit:{type: String},
usage:{type: String},
remarks:{type: String},
creator: {type: Schema.Types.ObjectId,ref: 'User'}
},{ timestamps: true}
);
module.exports = mongoose.model('Feed', feedSchema );
module.exports = mongoose.model('Farmintake',farmintakeSchema);
I tried following Resolver:
const transformFeed = feed =>{
return {
...feed._doc,
_id: feed.id,
feeding: farmintake.bind(this, feed.feedformula.feeding),
creator: user.bind(this,feed.creator)
};
};
const farmintake = async farmintakeId => {
try{
const farmintake = await Farmintake.findById(farmintakeId);
return {
...farmintake._doc,
_id:farmintake.id
};
}catch (err) {
throw err;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有等待任何答复,而是我自己挣扎并找到了解决方案,在下面是我的解决方案,它对我有用,专家反馈要求提出任何改进。我的GraphQL查询现在如下所示。
我想要的查询结果:
在更改模式之后,看起来像:
Typedefs:
最后的解析器:
Instead of waiting for any reply, I struggled and found the solution by myself, below is my solution and it worked for me, experts feedbacks are requested to suggest for any improvement. My Graphql query is working now as below.
Query Result as I wanted:
After changes the Schema looking like as:
TypeDefs:
Finally the Resolver: