所以我对graphql还是很陌生,但遇到了一个似乎无法解决的问题。因此,在我的模式中,我将其设置为每个用户都可以拥有与之关联的一系列书籍。我写了一个变异来添加一本新书。这是我的代码:
addBook: {
type: BookType,
args: {
genre: { type: GraphQLString },
title: { type: new GraphQLNonNull(GraphQLString) },
authorId: { type: new GraphQLNonNull(GraphQLID) },
},
resolve(parentValue, { genre, title, authorId }) {
return User.findById(authorId).then((user) => {
const book = new Book({ genre, title, author: user })
.save()
.then((book) =>
User.findOneAndUpdate(
{ _id: authorId },
{
$push: { books: book },
}
)
);
});
},
},
当我console.log记录正在传输的数据时,我会为关联的用户获取
{
loggedIn: false,
books: [
{
paragraphs: [],
likes: 0,
comments: [],
_id: 5f74bef27fd69a55045441aa,
title: 'Hey?',
author: 5f74bed67fd69a55045441a9
},
{
paragraphs: [],
likes: 0,
comments: [],
_id: 5f74bf3a88a5cc1218db1cf4,
title: 'Bye?',
author: 5f74bed67fd69a55045441a9
},
{
paragraphs: [],
likes: 0,
comments: [],
_id: 5f74c0453810e848f874a7d5,
title: 'MAMA?',
author: 5f74bed67fd69a55045441a9
},
{
paragraphs: [],
likes: 0,
comments: [],
_id: 5f74c33973f7172d9c6a92be,
title: 'HO?',
author: 5f74bed67fd69a55045441a9,
__v: 0
}
],
_id: 5f74bed67fd69a55045441a9,
name: 'jose',
__v: 0
}
这与所创建的书有关:
{
paragraphs: [],
likes: 0,
comments: [],
_id: 5f753912d90b433808648ba2,
title: 'babab',
author: {
loggedIn: false,
books: [ [Object], [Object], [Object], [Object] ],
_id: 5f74bed67fd69a55045441a9,
name: 'jose',
__v: 0
},
__v: 0
}
因此,它似乎是正确创建的。但是,当我查询与该用户相关的书籍时,我只能找回书名,其余的都归零。当我查询与一本书相关的作者时,所有结果都为空。我不确定问题是否出在我的查询或架构上。
这是我的用户架构:
const mongoose = require("mongoose");
const { Schema } = mongoose;
const userSchema = new Schema({
loggedIn: { type: Boolean, default: false },
name: { type: String },
bio: { type: Schema.Types.ObjectId, ref: "bio" },
books: {
type: Schema.Types.Array,
ref: "book",
},
age: Number,
livesIn: String,
});
const User = mongoose.model("user", userSchema);
module.exports = User;
还有我的Book模式:
const mongoose = require("mongoose");
const { Schema } = mongoose;
const bookSchema = new Schema({
title: String,
author: { type: Schema.Types.ObjectId, ref: "user" },
paragraphs: [
{
type: Schema.Types.ObjectId,
ref: "paragraph",
},
],
likes: { type: Number, default: 0 },
comments: [
{
type: Schema.Types.ObjectId,
ref: "comments",
},
],
genre: String,
about: { type: Schema.Types.ObjectId, ref: "bio" },
});
const Book = mongoose.model("book", bookSchema);
module.exports = Book;
这是我的用户类型:
const UserType = new GraphQLObjectType({
name: "User",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
loggedIn: { type: GraphQLBoolean },
age: { type: GraphQLInt },
livesIn: { type: GraphQLString },
bio: {
type: BioType,
resolve(parentValue, args) {
return User.findById(parentValue.id)
.populate("bio")
.then((user) => user.bio);
},
},
books: {
type: new GraphQLList(BookType),
resolve(parentValue, args) {
return User.findById(parentValue.id).then((user) =>
user.books.map((item) => {
return item;
})
);
},
},
}),
});
我的用户查询:
users: {
type: new GraphQLList(UserType),
async resolve(parentValue, args) {
const user = await User.find({});
return user;
},
},
user: {
type: UserType,
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve(parentValue, { id }) {
return User.findById(id);
},
},
很抱歉,如果代码太多。这是我第一次在这里发帖。希望有人可以帮助我解决这个问题。在过去的两天里,我一直坚持下去!