Home > OS >  Apollo graphql typedef for a paticular schema
Apollo graphql typedef for a paticular schema

Time:10-16

This is my post schema . what should be the appropriate gql typedef for it.

    const postSchema = new mongoose.Schema(
  {
    author: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "user",
      required: true,
    },
    description: {
      type: String,
    },
    image: { type: String, required: true },
    likes: [{ type: mongoose.Schema.Types.ObjectId, ref: "user" }],
  },
  {
    timestamps: true,
  }
);
module.exports = mongoose.model("post", postSchema);

The user schema only contains Name, Email , profile_pic and password.

Also what should be the query if i only what to get the Name and Profile_pic of the users who have liked a particular post?

CodePudding user response:

In typeDefs you have to write this code for type definition.

First of all import gql-

const {gql} = require("apollo-server-express");

Then add this-

module.exports = gql`
    extend type Query {
        // if you want to get all post, You must give array of PostInfo
        Post: [PostInfo]
        //If you want to get by Id
        PostById: PostInfo
    }
    type PostInfo{
        author: User
        description: String
        image: String
        likes: User
        createdAt: Date
        updatedAt: Date
    }
    type User {
        name: String
        email: String
        profile_pic: String
        // You shouldn't give password any where.
    }
`;

I think it will be helpful for you!

  • Related