我最近从Graphql开始工作,当从数据库中找到多个带有条件的记录(例如find({'author.id':id}))时,我得到响应NULL。在控制台中,所有记录都被打印出来,但是当解决响应显示为空。
我的代码像:
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
}
}
}
这是我的EventType:
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。我也在编辑我的帖子。感谢您的支持..
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句