Mark-Lasfar commited on
Commit ·
d27b740
1
Parent(s): 8b33722
Add GraphQL
Browse files- graphql/resolvers.js +48 -78
graphql/resolvers.js
CHANGED
|
@@ -447,86 +447,56 @@ const resolvers = {
|
|
| 447 |
// 🔹 POST RESOLVERS - ✅ الحل النهائي
|
| 448 |
// ============================================
|
| 449 |
Post: {
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
return parent.shares.length;
|
| 470 |
-
},
|
| 471 |
-
savesCount: (parent) => {
|
| 472 |
-
if (!parent || !parent.saves || !Array.isArray(parent.saves)) {
|
| 473 |
-
return 0;
|
| 474 |
-
}
|
| 475 |
-
return parent.saves.length;
|
| 476 |
-
},
|
| 477 |
-
isLiked: (parent, args, context) => {
|
| 478 |
-
if (!context?.user) return false;
|
| 479 |
-
if (!parent || !parent.likes || !Array.isArray(parent.likes)) return false;
|
| 480 |
-
return parent.likes.some(l => l.userId?.toString() === context.user.userId);
|
| 481 |
-
},
|
| 482 |
-
isSaved: (parent, args, context) => {
|
| 483 |
-
if (!context?.user) return false;
|
| 484 |
-
if (!parent || !parent.saves || !Array.isArray(parent.saves)) return false;
|
| 485 |
-
return parent.saves.some(s => s.userId?.toString() === context.user.userId);
|
| 486 |
-
},
|
| 487 |
-
comments: async (parent, args, context) => {
|
| 488 |
-
const comments = parent.comments || [];
|
| 489 |
-
const userId = context?.user?.userId;
|
| 490 |
-
|
| 491 |
-
const commentData = await Promise.all(comments.map(async (c) => {
|
| 492 |
-
let user = null;
|
| 493 |
-
if (c.userId) {
|
| 494 |
-
user = await User.findById(c.userId)
|
| 495 |
-
.select('username profile.nickname profile.avatar profile.jobTitle')
|
| 496 |
-
.lean();
|
| 497 |
-
}
|
| 498 |
-
|
| 499 |
-
let replies = [];
|
| 500 |
-
if (c.replies && c.replies.length > 0) {
|
| 501 |
-
replies = await Promise.all(c.replies.map(async (r) => {
|
| 502 |
-
let replyUser = null;
|
| 503 |
-
if (r.userId) {
|
| 504 |
-
replyUser = await User.findById(r.userId)
|
| 505 |
-
.select('username profile.nickname profile.avatar profile.jobTitle')
|
| 506 |
-
.lean();
|
| 507 |
-
}
|
| 508 |
-
|
| 509 |
-
return {
|
| 510 |
-
...r,
|
| 511 |
-
userId: replyUser || { _id: r.userId, username: 'Unknown', profile: {} },
|
| 512 |
-
likesCount: r.likes?.length || 0,
|
| 513 |
-
isLiked: r.likes?.some(l => l.userId?.toString() === userId) || false
|
| 514 |
-
};
|
| 515 |
-
}));
|
| 516 |
-
}
|
| 517 |
-
|
| 518 |
-
return {
|
| 519 |
-
...c,
|
| 520 |
-
userId: user || { _id: c.userId, username: 'Unknown', profile: {} },
|
| 521 |
-
likesCount: c.likes?.length || 0,
|
| 522 |
-
isLiked: c.likes?.some(l => l.userId?.toString() === userId) || false,
|
| 523 |
-
replies: replies
|
| 524 |
-
};
|
| 525 |
-
}));
|
| 526 |
-
|
| 527 |
-
return commentData;
|
| 528 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
},
|
|
|
|
| 530 |
// ============================================
|
| 531 |
// 🔹 STORY RESOLVERS
|
| 532 |
// ============================================
|
|
|
|
| 447 |
// 🔹 POST RESOLVERS - ✅ الحل النهائي
|
| 448 |
// ============================================
|
| 449 |
Post: {
|
| 450 |
+
userId: async (parent) => {
|
| 451 |
+
return await User.findById(parent.userId);
|
| 452 |
+
},
|
| 453 |
+
likesCount: (parent) => {
|
| 454 |
+
// ✅ التأكد من وجود likes وإرجاع 0 دائماً
|
| 455 |
+
if (!parent || !parent.likes || !Array.isArray(parent.likes)) {
|
| 456 |
+
return 0;
|
| 457 |
+
}
|
| 458 |
+
return parent.likes.length;
|
| 459 |
+
},
|
| 460 |
+
commentsCount: (parent) => {
|
| 461 |
+
if (!parent || !parent.comments || !Array.isArray(parent.comments)) {
|
| 462 |
+
return 0;
|
| 463 |
+
}
|
| 464 |
+
return parent.comments.length;
|
| 465 |
+
},
|
| 466 |
+
sharesCount: (parent) => {
|
| 467 |
+
if (!parent || !parent.shares || !Array.isArray(parent.shares)) {
|
| 468 |
+
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
}
|
| 470 |
+
return parent.shares.length;
|
| 471 |
+
},
|
| 472 |
+
savesCount: (parent) => {
|
| 473 |
+
if (!parent || !parent.saves || !Array.isArray(parent.saves)) {
|
| 474 |
+
return 0;
|
| 475 |
+
}
|
| 476 |
+
return parent.saves.length;
|
| 477 |
+
},
|
| 478 |
+
isLiked: (parent, args, context) => {
|
| 479 |
+
if (!context.user) return false;
|
| 480 |
+
if (!parent || !parent.likes || !Array.isArray(parent.likes)) return false;
|
| 481 |
+
return parent.likes.some(l => l.userId?.toString() === context.user.userId);
|
| 482 |
+
},
|
| 483 |
+
isSaved: (parent, args, context) => {
|
| 484 |
+
if (!context.user) return false;
|
| 485 |
+
if (!parent || !parent.saves || !Array.isArray(parent.saves)) return false;
|
| 486 |
+
return parent.saves.some(s => s.userId?.toString() === context.user.userId);
|
| 487 |
+
},
|
| 488 |
+
comments: async (parent) => {
|
| 489 |
+
// جلب التعليقات مع isLiked
|
| 490 |
+
const comments = parent.comments || [];
|
| 491 |
+
return comments.map(c => ({
|
| 492 |
+
...c,
|
| 493 |
+
likesCount: c.likes?.length || 0,
|
| 494 |
+
isLiked: c.likes?.some(l => l.userId?.toString() === context.user?.userId) || false
|
| 495 |
+
}));
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
},
|
| 499 |
+
|
| 500 |
// ============================================
|
| 501 |
// 🔹 STORY RESOLVERS
|
| 502 |
// ============================================
|