How to I resolve below graphql query in mongodb nested array

Viewed 43

my model schema look like this

const mongoose = require("mongoose")
const userSchema = new mongoose.Schema(
  {
    username: {
      type: String,
      required: true,
    },
    password: {
      type: String,
      required: true,
      select: false,
    },
    email: {
      type: String,
      required: true,
      unique: true,
      match: [
        /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
        "Please enter a valid email",
      ],
    },
    followers:[
      {
        type:mongoose.Schema.Types.ObjectId,
        ref:"user"
      }
    ],
    following:[
      {
        type:mongoose.Schema.Types.ObjectId,
        ref:"user"
      }
    ],
    displayName: {
      type: String,
      required: false,
    },
  },
  { timestamps: true }
)

module.exports = mongoose.model("user", userSchema)

in this schema all working good like mutation work fine but when i fetch query of all user then in that query followers and following field return null like bellow image enter image description here

and my graphql query is

const users = {
  type: new GraphQLList(UserType),
  description: "Retrieves list of users",
  resolve(parent, args) {
    return User.find()
  },
}

and typedef is

 const UserType = new GraphQLObjectType({
    name: "User",
    description: "User types",
    fields: () => ({
      id: { type: GraphQLID },
      username: { type: GraphQLString },
      email: { type: GraphQLString },
      post:{
        type: GraphQLList(PostType),
        resolve(parent, args) {
          return Post.find({ authorId: parent.id })
        },
      },
      savePost:{
        type:GraphQLList(savedPosts1),
        resolve(parent, args) {
          return SavePost.findById({ authorId: parent.id })
        },
      },
      followers:{
        type:GraphQLList(UserType),
      },
      following:{
        type:GraphQLList(UserType)
      }
      // displayName: { type: GraphQLString },
    }),
  })

so please tell me how to i resolve that followers and following query in graphql with mongodb and tell me what i write in my userType typedef

0 Answers
Related