用于特定模式的 Apollo graphql typedef
Apollo graphql typedef for a paticular schema
这是我的 post 架构。适合它的 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 和密码。
如果我只获取喜欢特定 post 的用户的名称和 Profile_pic,那么查询应该是什么?
在 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.
}
`;
我想这对你会有帮助!
这是我的 post 架构。适合它的 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 和密码。
如果我只获取喜欢特定 post 的用户的名称和 Profile_pic,那么查询应该是什么?
在 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.
}
`;
我想这对你会有帮助!