如何使用带有 Mongoose 和 ES6 承诺的 Graphql 中的查找解决多条记录
How to resolve the multiple records using find in Graphql with Mongoose and ES6 promises
我最近开始使用 Graphql,当我从数据库中找到具有条件(如 find({'author.id':id}))的多条记录时,我得到了响应 NULL.In 控制台所有记录都是正在打印,但解析时响应显示为空。
我的代码如下:
export default {
eventByUserId: {
type: new GraphQLList(EventType),
args: {
id: {
type: GraphQLID
}
},
resolve: (root, {id}) => {
return new Promise((resolve, reject) => {
Event.find({'author.id': id}).exec((err, res) => {
console.log(res);
err ? reject(err) : resolve(res);
});
});
};
}
};
传递如下查询:
{
eventByUserId
(
id:"55dd69e702a488b81c4dd8ed"
)
{
title
description
start
media{url}
location{state,city}
author{id, name}
comments{text,created,author{id,name}}
posts{url,mediaType,imageUrl,note,author{id,name}}
_id
}
}
回复如下:
{
"data": {
"eventByUserId": {
"title": null,
"description": null,
"start": null,
"media": null,
"location": null,
"author": null,
"comments": null,
"posts": null,
"_id": null
}
}
}
这是我的事件类型:
export default new GraphQLObjectType({
name: 'Event',
description: 'A event',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
title: {
type: GraphQLString,
description: 'The title of the event.',
},
description: {
type: GraphQLString,
description: 'The description of the event.',
},
start: {
type: GraphQLString,
description: 'The start date of the event.',
},
media:{
type:new GraphQLList(MediaType),
description:'List of media.',
},
location:{
type:new GraphQLList(LocationType),
description:' The list of location. ',
},
comments:{
type:new GraphQLList(CommentType),
description:' The list of Comments. ',
},
posts:{
type:new GraphQLList(PostType),
description:' The list of Posts. ',
},
created: {
type: GraphQLString,
description: 'The created at.',
},
author:{
type: AuthorType,
description: 'The author for the event.',
}
})
});
在这种情况下,我们正在尝试解析数据列表,因此在 eventByUserId 中,我们必须将 GraphQLList 作为 type:new GraphQLList(EventType) 类型来代替 type:EventType。我也在编辑我的 post。感谢您的支持..
我最近开始使用 Graphql,当我从数据库中找到具有条件(如 find({'author.id':id}))的多条记录时,我得到了响应 NULL.In 控制台所有记录都是正在打印,但解析时响应显示为空。
我的代码如下:
export default {
eventByUserId: {
type: new GraphQLList(EventType),
args: {
id: {
type: GraphQLID
}
},
resolve: (root, {id}) => {
return new Promise((resolve, reject) => {
Event.find({'author.id': id}).exec((err, res) => {
console.log(res);
err ? reject(err) : resolve(res);
});
});
};
}
};
传递如下查询:
{
eventByUserId
(
id:"55dd69e702a488b81c4dd8ed"
)
{
title
description
start
media{url}
location{state,city}
author{id, name}
comments{text,created,author{id,name}}
posts{url,mediaType,imageUrl,note,author{id,name}}
_id
}
}
回复如下:
{
"data": {
"eventByUserId": {
"title": null,
"description": null,
"start": null,
"media": null,
"location": null,
"author": null,
"comments": null,
"posts": null,
"_id": null
}
}
}
这是我的事件类型:
export default new GraphQLObjectType({
name: 'Event',
description: 'A event',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
title: {
type: GraphQLString,
description: 'The title of the event.',
},
description: {
type: GraphQLString,
description: 'The description of the event.',
},
start: {
type: GraphQLString,
description: 'The start date of the event.',
},
media:{
type:new GraphQLList(MediaType),
description:'List of media.',
},
location:{
type:new GraphQLList(LocationType),
description:' The list of location. ',
},
comments:{
type:new GraphQLList(CommentType),
description:' The list of Comments. ',
},
posts:{
type:new GraphQLList(PostType),
description:' The list of Posts. ',
},
created: {
type: GraphQLString,
description: 'The created at.',
},
author:{
type: AuthorType,
description: 'The author for the event.',
}
})
});
在这种情况下,我们正在尝试解析数据列表,因此在 eventByUserId 中,我们必须将 GraphQLList 作为 type:new GraphQLList(EventType) 类型来代替 type:EventType。我也在编辑我的 post。感谢您的支持..