在 GraphQL 中,可以使用 ' @unique ' 指令来标记某个字段为唯一的,然后在查询中使用 'distinct' 关键字来去重复查询。

例如,假设有一个 'User' 类型,它有一个唯一的 'email' 字段,可以这样定义:

type User {
  id: ID!
  name: String!
  email: String! @unique
}

然后,在查询中可以使用 'distinct' 关键字来去重复查询:

query {
  users {
    email @distinct
  }
}

这将返回所有不重复的 'email' 值。注意,' @distinct ' 只能用于标记标量字段,不能用于标记对象类型字段。

GraphQL 去重查询:使用 @unique 和 distinct

原文地址: https://www.cveoy.top/t/topic/f2mj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录