简体   繁体   中英

Apollo graphql typedef for a paticular schema

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?

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(id: ID!): 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM