【问题标题】:I am getting error in graphql while making query for specific id在查询特定 ID 时,我在 graphql 中遇到错误
【发布时间】: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",                
}

请看一下。

【问题讨论】:

    标签: mongodb graphql


    【解决方案1】:

    type: UserType 这应该是type: new GraphQLList(UserType),

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2022-01-24
      • 2019-10-18
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      相关资源
      最近更新 更多