簡體   English   中英

用於特定模式的 Apollo graphql typedef

[英]Apollo graphql typedef for a paticular schema

這是我的帖子架構。 什么應該是合適的 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);

用戶架構僅包含 Name、Email、profile_pic 和 password。

如果我只獲取喜歡特定帖子的用戶的姓名和個人資料圖片,那么查詢應該是什么?

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

我想它會對你有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM