Cześć ;)
Jestem w trakcie tworzenia apki w stylu social media. Mam w swojej BD (MongoDB) tabelę z komentarzami i jednym polem jest "replies" gdzie daję odpowiedzi na ten konkretny komentarz. W BD wszystko jest spoko, jak widać się tworzy nowe id i wgl. Niestety GraphQL zwraca null dla id:
Kod modelu Comment:
const { model, Schema } = require('mongoose');
const commentSchema = new Schema({
postId: {
type: Schema.Types.ObjectId,
ref: 'comments'
},
body: String,
username: String,
publishingTime: String,
replies: [ this ],
plusses: [
{
username: String,
plussedAt: String
}
],
minusses: [
{
username: String,
minussedAt: String
}
],
user: {
type: Schema.Types.ObjectId,
ref: 'users'
}
});
module.exports = model('Comment', commentSchema);
Kod dodanie odpowiedzi do komentarza:
async postReplyToComment(parent, { commentId, body }, context) {
const user = auth(context);
if (body.trim() === '') {
throw new UserInputError('Pusty komentarz', {
errors: {
body: "Komentarz nie może być pusty"
}
})
}
const comment = await Comment.findById(commentId);
if (comment) {
const newReply = new Comment({
body,
user: user.id,
username: user.username,
publishingTime: new Date().toISOString()
});
comment.replies.push(newReply);
await comment.save();
return comment;
} else {
throw new UserInputError('Nie znaleziono takiego komentarza');
}
}