用于跨猫鼬和 GraphQL 的 ObjectId 字段的正确类型是什么? [英] What is the correct type to use for an ObjectId field across mongoose and GraphQL?
问题描述
按照
但读取值似乎有问题.
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({name: '突变',字段:{新增帐户: {类型:帐户类型,描述: '创建新帐户',参数:{名称: {name: '账户名',类型:新 GraphQLNonNull(GraphQLString)}},解析:(root, args) =>{const newAccount = 新账户({名称:args.name});newAccount.id = newAccount._id;返回新的承诺((解决,拒绝)=> {newAccount.save(err => {如果(错误)拒绝(错误);否则解决(新帐户);});});}}});
要获取工作示例:克隆 存储库.在此存储库中,应用程序使用 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屋!