Graphql 模式类型

发布于 2025-01-14 19:38:25 字数 235 浏览 2 评论 0原文

我们可以将关注者和关注者的类型定义为用户吗?或者如果我只在关注者和追随者上创建不同的模式会更好吗?

type User {
        id: ID!
        name: String!
        email: String!
        posts: [Post!]
        following: [User]
        followers: [User]
    }

Can we define the type of followers and following as User? or is it better if I will just create a differenct schema on both following and followers?

type User {
        id: ID!
        name: String!
        email: String!
        posts: [Post!]
        following: [User]
        followers: [User]
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2025-01-21 19:38:25

您的方式很好,没有任何问题。但是,如果您需要这些字段中的更多信息,您可以执行以下操作:

type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post!]
    following: [Follower]
    followers: [Follower]
}

type Follower {
   user: User
   dateFollowed: String
}

这允许您重用 User 类型,同时还可以合并与追随者。就像我说的,如果你不需要额外的信息,那么你的方法就可以了。祝你好运!

Your way is fine and there's nothing wrong with it. However, if you need more info in those fields you could do something like:

type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post!]
    following: [Follower]
    followers: [Follower]
}

type Follower {
   user: User
   dateFollowed: String
}

That allows you to reuse the User type while also incorporating additional information related to a follower. Like I said though, if you don't need that extra info then your way works fine. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文