英文:
Unable to insert data into db where a collection has a relationship
问题
这是在一个Node
项目中,我正在尝试为一个简单的博客编写一个graphql
API。
我正在使用apollo
,并在后台尝试通过mongoose将数据保存到mongo
数据库。
当我尝试保存帖子数据时,我目前正在收到以下错误。
"GraphQLError: ID cannot represent value: { type: \"Buffer\", data: [Array] }",
"at GraphQLScalarType.serialize (/Users/name/projects/graphtime/node_modules/graphql/type/scalars.js:301:11)",
我认为它在Post的作者插入部分出现问题。
我的模式或我在Apollo GraphiEditor上执行查询的方式是否有问题?
这是数据库模式
import mongoose from "mongoose";
const authorSchema = new mongoose.Schema({
name: { type: String, required: true },
avatar: { type: String },
});
export const Author = mongoose.model('Author', authorSchema);
const postSchema = new mongoose.Schema({
title: { type: String, required: true },
authors: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Author'
}
]
});
export const Post = mongoose.model('Post', postSchema);
这是GraphQL模式
type Post {
id: ID!
title: String
authors: [Author]
}
type Author {
id: ID!
name: String
avatar: String
}
type Mutation {
addPost(
title: String!,
authors: [ID!],
): Post
}
这是GraphQL解析器
const resolvers = {
Mutation: {
addPost: (_, args, context) => {
const newPost = new Post({
title: args.title,
authors: args.authors,
});
return newPost.save();
},
},
};
这是我在编辑器中查询时引发错误的方式。
mutation {
addPost(
title: "New Post",
authors: ["63babc44e18d174016b03433"],
) {
title
authors {
id
name
}
}
}
希望这些信息有助于解决您遇到的问题。
英文:
This is in a Node
project where I am trying to write a graphql
api for a simple blog.
<br/>I am using apollo
and behind the scenes attempting to save data to a mongo
db via mongoose.
I am currently getting the following error when I try to save a Post data.
> "GraphQLError: ID cannot represent value: { type: "Buffer", data:
> [Array] }", "at GraphQLScalarType.serialize
> (/Users/name/projects/graphtime/node_modules/graphql/type/scalars.js:301:11)",
It is complaining about the Author insertion as part of the Post I believe.
Is there something wrong with my schema or how I am performing the query on the Apollo GraphiEditor?
The is the DB Schema
import mongoose from "mongoose";
const authorSchema = new mongoose.Schema({
name: { type: String, required: true },
avatar: { type: String },
});
export const Author = mongoose.model('Author', authorSchema);
const postSchema = new mongoose.Schema({
title: { type: String, required: true },
authors: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Author'
}
]
});
export const Post = mongoose.model('Post', postSchema);
This is the graphql Schema
type Post {
id: ID!
title: String
authors: [Author]
}
type Author {
id: ID!
name: String
avatar: String
}
type Mutation {
addPost(
title: String!,
authors: [ID!],
): Post
}
This is the grapql resolver.
const resolvers = {
Mutation: {
addPost: (_, args, context) => {
const newPost = new Post({
title: args.title,
authors: args.authors,
});
return newPost.save();
},
},
};
This is how I am querying in the editor which throws the error.
<br/>P.S: The DB already has an existing author in there and lets say the id for that row is: 63babc44e18d174016b03433
mutation {
addPost(
title: "New Post",
authors: ["63babc44e18d174016b03433"],
) {
title
authors {
id
name
}
}
}
答案1
得分: 1
你已经以这种方式定义了mutation:
type Mutation {
addPost(
title: String!,
authors: [ID!],
): Post
}
这意味着addPost
会返回一个Post
对象,它的结构如下:
type Post {
id: ID!
title: String
authors: [Author]
}
因此,authors
是一个包含Author
对象的数组,而不是_id
的数组。
虽然你将ID保存到了数据库中,但返回的对象与[Author]
不匹配。还要注意,postSchema
是mongoose.Schema.Types.ObjectId
。虽然它是对作者的引用,但它是一个包含ID的数组。
因此,你可以返回一个ID数组而不是Author对象,或者在mongoose中填充查询,因为你有引用关系。
英文:
You have defined the mutation in this way:
type Mutation {
addPost(
title: String!,
authors: [ID!],
): Post
}
That's mean addPost
return a Post
object, which is:
type Post {
id: ID!
title: String
authors: [Author]
}
So authors
is an array of Author
s objects, not the _id
s.
You are getting the ID and saving into DB but the returned object does not match [Author]
. Also note how the postSchema
is mongoose.Schema.Types.ObjectId
. Referenced to Authors, yes, but is an array of IDs.
So you can return an array of IDs instead the Author object or populate the query in mongoose because you have the reference.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论