【问题标题】:GRAPHQL find items based on EnumGRAPHQL 基于 Enum 查找项目
【发布时间】:2020-05-24 18:01:14
【问题描述】:

我正在尝试查找与某个枚举匹配的结果,这是我的type

type User {
  id: ID! @id
  fname:String!
  lname:String!
  email:String! @unique
  password: String
  school_ID: String #@relation(Link: INLINE)
  role: [Role] @scalarList(strategy: RELATION)
  resetToken: String
  resetTokenExpiry: String
}

我可以搜索电子邮件,因为它有 @unique 键,但 Role 是一个枚举,我不能向它添加 @unique 键,这是有道理的,因为结果不会是独一无二的。

但我不断收到此消息:

“变量 \"$_v0_where\" 的值无效 {\"role\":\"TEACHER\"};字段 \"role\" 未由 UserWhereUniqueInput 类型定义。”

如果我想使用电子邮件查找项目,使用电子邮件可以得到我想要的结果。

我所追求的是返回一个数组,其中包含与所选枚举结果匹配的所有对象。

在我的架构中,我有这个:

users: [User]!
user(where: UserWhereInput!): User
findUser(role: Role, id: ID, email: String): User

在我的查询解析器中,我正在使用以下内容:

  users: forwardTo('db'),
  user: forwardTo('db'),

  async findUser(parent, args, ctx, info) {
    return ctx.db.query.user({
      where: {email: args.email, id: args.id, role: args.role}
    }, info)
  },

我正在使用 prisma (1.17.1) 来生成我的架构

如何更改我的代码或findUser 函数以获得所需的结果?

【问题讨论】:

  • 这个问题似乎特定于 Prisma,而不是 GraphQL 或 Apollo。您应该相应地更新您的标签并指出您正在运行的 Prisma 版本。
  • @DanielRearden 完成 :-) - 我仍然认为它与 GraphQL 有关,因为我正在整理我的架构

标签: enums graphql prisma graphiql


【解决方案1】:

您正在使用具有以下签名的ctx.db.query.user 函数:

user: (where: UserWhereUniqueInput) => UserNullablePromise;

如您所见,它需要 UserWhereUniqueInput 类型的 where 条件。为什么?因为用户函数应该始终只返回一个不同的唯一对象。另一方面还有一个ctx.db.query.users函数:

  users: (args?: {
    where?: UserWhereInput;
    orderBy?: UserOrderByInput;
    skip?: Int;
    after?: String;
    before?: String;
    first?: Int;
    last?: Int;
  }) => FragmentableArray<User>;

如您所见,此函数有更多选项,并使用UserWhereInput 来定义 where 条件。这还将包括用户在您的案例中的角色。此函数可以返回多个对象,并由您进行相应的过滤/处理。

提示:您也可以自己检查生成的 prisma 客户端。路径通常类似于src\generated\prisma-client\index.ts。在那里你可以看到所有函数的接口定义,例如UserWhereUniqueInputUserWhereInput。通过这种方式,您可以检查什么是可能的,什么是不可能的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-15
    • 2013-08-25
    • 2019-09-22
    • 1970-01-01
    • 2022-08-18
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多