【问题标题】:Apollo graphql typedef for a paticular schema用于特定模式的 Apollo graphql typedef
【发布时间】:2021-11-28 10:55:25
【问题描述】:

这是我的帖子架构。合适的 gql typedef 应该是什么。

    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);

用户架构仅包含名称、电子邮件、profile_pic 和密码。

如果我只获取喜欢特定帖子的用户的名称和 Profile_pic,那么查询应该是什么?

【问题讨论】:

    标签: node.js mongodb mongoose graphql apollo-server


    【解决方案1】:

    在 typeDefs 中,您必须编写此代码来定义类型。

    首先导入gql-

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

    然后添加这个-

    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.
        }
    `;
    

    我觉得对你有帮助!

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 2019-03-01
      • 2019-10-06
      • 2019-04-22
      • 2019-05-10
      • 2018-04-23
      • 2020-09-07
      • 2019-12-28
      • 2020-12-21
      相关资源
      最近更新 更多