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.
