【发布时间】:2019-12-23 22:58:18
【问题描述】:
架构:
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id : { type: GraphQLID },
name : { type: GraphQLString },
email : { type: GraphQLString },
})
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: {
id: { type: GraphQLID }
},
resolve(parent, args){
return User.findById(args.id);
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery
});
这里我从 mongodb 中按 id 获取数据。
我正在为 mongodb 客户端使用 mongoose。 我已经用 express 和 graphql 设置了服务器。
我正在进行以下查询以通过特定 id 获取结果
{
user(id: 2){
name
}
}
但是,我在查询时遇到了错误。
{
"errors": [
{
"message": "Cast to ObjectId failed for value \"2\" at path \"_id\" for model \"User\"",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"user"
]
}
],
"data": {
"user": null
}
}
我在数据库中的 id 只是一个整数值,如下所示
{
"_id" : ObjectId("5d49c6dc54f1cec013ee6b49"),
"id" : 10,
"name" : "Clementina DuBuque",
"email" : "Rey.Padberg@karina.biz",
}
请看一下。
【问题讨论】: