mongodb – Creating a resolver in GraphQL to handle follow/unfollow mutation?

I have been trying to come up with a resolver for GraphQL to be able to implement a follow/unfollow feature for users on my app. The resolver is intended to be designed to follow the user if you are not already following them. If you are, the same resolver will trigger an unfollow.

When I use the following resolver and hit follow, my id is added to their followers list and their id is added to my following list. When I hit the same button to trigger the unfollow, my id is removed from their followers list BUT their id is not removed from my following list.

Here is my code. Is there anything plainly obviously wrong with the logic? Apologies if this is not enough of the code to deduce the answer.

followUnfollow: async (_, { _id }, context) => {
      try {
        _id = Mongoose.Types.ObjectId(_id);
        const otherUser = await User.findById(_id).populate(
          "following followers"
        const user = await User.findById(context.user._id).populate("following followers");
        if (otherUser.followers.find((m) => m._id == user._id.toString())) {
          return await User.findByIdAndUpdate(
              $pull: { followers: user._id },
            { new: true },
            (result) => {
                  $pull: { following: _id },
                { new: true }
              ).populate("following followers");
          ).populate("following followers");
        } else {          
          return user, otherUser;
      } catch (error) {
        throw new Error(error);

Thanks in advance for any advice!

Read more here: Source link