在猫鼬和GraphQL中用于ObjectId字段的正确类型是什么? [英] What is the correct type to use for an ObjectId field across mongoose and GraphQL?

查看:42
本文介绍了在猫鼬和GraphQL中用于ObjectId字段的正确类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但是读取值似乎有问题.

GraphQLID mongoose.Schema.Types.ObjectId 的兼容性如何?如果它们不兼容,我会误解该教程,尤其是它的使用:

  newAccount.id = newAccount._id; 

?我无法确定是GraphQL,MongoDB,Mongoose还是其他东西抛出了错误.

编辑

有关该错误的任何信息

ID不能代表值:{_bsontype:\"ObjectID \",id:}

将非常有用.我感觉这是在告诉我它无法序列化BSON对象..但随后它显示了它已序列化.即使知道是什么技术(mongo?mongoose?graphql?)产生了错误也会有所帮助.我对Google没有任何运气.

编辑2

这是由对最近引入的graphql软件包进行的更改引起的,并且有一个PR 等待合并并解决.

解决方案

我没有发现问题,而是使用我现有的代码库之一运行了此代码.除了我将突变包装在 GraphQLObjectType 中.

  const Mutation = new GraphQLObjectType({名称:静音",栏位:{新增帐户: {类型:AccountType,说明:创建新帐户",args:{名称: {名称:帐户名称",类型:new GraphQLNonNull(GraphQLString)}},解析:(root,args)=>{const newAccount =新帐户({名称:args.name});newAccount.id = newAccount._id;返回新的Promise((resolve,reject)=> {newAccount.save(err => {如果(err)拒绝(err);否则resolve(newAccount);});});}}}); 

要获取工作示例,请执行以下操作:克隆回购.在此存储库中,该应用程序使用 v0.13.2 ,而您正在使用通过 npm i graphql 安装的 v14.0.2 .将 graphql 降级为 v0.13.2 .

Following this tutorial, I have a mongoose model: (I'm using the term "Account" instead of "Todo", but it's the same thing)

const Account = mongoose.model('Account', new mongoose.Schema({
  id: mongoose.Schema.Types.ObjectId,
  name: String
}));

and a GraphQLObjectType:

const AccountType = new GraphQLObjectType({
  name: 'account',
  fields: function () {
    return {
      id: {
        type: GraphQLID
      },
      name: {
        type: GraphQLString
      }
    }
  }
});

and a GraphQL mutation to create one of these:

const mutationCreateType = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
    add: {
      type: AccountType,
      description: 'Create new account',
      args: {
        name: {
          name: 'Account Name',
          type: new GraphQLNonNull(GraphQLString)
        }
      },
      resolve: (root, args) => {
        const newAccount = new Account({
          name: args.name
        });

        newAccount.id = newAccount._id;

        return new Promise((resolve, reject) => {
          newAccount.save(err => {
            if (err) reject(err);
            else resolve(newAccount);
          });
        });
      }
    }
  }
})

After running the query:

mutation {
  add(name: "Potato")
  {
    id,
    name
  }
}

in GraphiQL, I get the response:

{
  "errors": [
    {
      "message": "ID cannot represent value: { _bsontype: \"ObjectID\", id: <Buffer 5b 94 eb ca e7 4f 2d 06 43 a6 92 20> }",
      "locations": [
        {
          "line": 33,
          "column": 5
        }
      ],
      "path": [
        "add",
        "id"
      ]
    }
  ],
  "data": {
    "add": {
      "id": null,
      "name": "Potato"
    }
  }
}

The creation of the object was successful, and I can see it in MongoDB Compass:

but there seems to be a problem reading the value.

How compatible are GraphQLID and mongoose.Schema.Types.ObjectId ? If they are not compatible, am I misunderstanding the tutorial, particularly it's use of:

newAccount.id = newAccount._id;

? I can't tell if the error is being thrown by GraphQL, or MongoDB, or Mongoose, or something else entirely.

EDIT

Any information on the error

ID cannot represent value: { _bsontype: \"ObjectID\", id: }

would be very helpful. I feel it's telling me it couldn't serialize a BSON object .. but then it displays it serialized. Even knowing what tech (mongo? mongoose? graphql?) was generating the error would help. I'm not having any luck with Google.

EDIT 2

This was a caused by a change to the graphql package introduced recently, and there is a PR awaiting merge which resolves it.

解决方案

I didn't find an issue and ran this code with one of my existing code bases. Except I wrapped the mutation in the GraphQLObjectType.

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        addAccount: {
            type: AccountType,
            description: 'Create new account',
            args: {
                name: {
                    name: 'Account Name',
                    type: new GraphQLNonNull(GraphQLString)
                }
            },
            resolve: (root, args) => {
                const newAccount = new Account({
                    name: args.name
                });

                newAccount.id = newAccount._id;

                return new Promise((resolve, reject) => {
                    newAccount.save(err => {
                        if (err) reject(err);
                        else resolve(newAccount);
                    });
                });
            }
        }
    });

To get the working example: Clone the repo. In this repo, the app uses v0.13.2 and you are using v14.0.2 installed via npm i graphql. Downgrade graphql to v0.13.2.

这篇关于在猫鼬和GraphQL中用于ObjectId字段的正确类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆