【问题标题】:Enum mismatch between typescript and graphql-codegentypescript 和 graphql-codegen 之间的枚举不匹配
【发布时间】:2021-09-24 14:23:22
【问题描述】:

我目前正试图解决以下问题,但并没有走得太远。目前,我正在运行一个 Apollo-Server 作为我的 graphQL 服务器,并使用 postgres 数据库配置了 prisma2,并且正在使用 graphql-codegen 从我的模式中生成类型。目前,我正在尝试更新一个具有多对多关系的模型,并且抛出了非常奇怪的打字稿错误。我很确定我的更新不正确,但我看到的错误没有帮助。

以下是相关代码sn-ps:

prisma.schema

model Permission {
  id       Int             @id @default(autoincrement())
  verb     PermissionVerb
  resource PermissionModel
  own      Boolean         @default(true)
  roles    Role[]
}
    
model Role {
  id          Int          @id @default(autoincrement())
  name        String
  permissions Permission[]
}

enum PermissionModel {
  USER
  ORDER
  CUSTOMER
  PERMISSION
  ROLE
}

enum PermissionVerb {
  CREATE
  READ
  UPDATE
  DELETE
}

permission.gql

extend type Mutation {
  updatePermission(input: UpdatePermissionInput): PermissionResult!
}

union PermissionResult = Permission | PermRoleNotFoundError

type Permission {
  id: Int!
  own: Boolean
  resource: PermissionModel!
  verb: PermissionVerb!
  roles: [Role]
}

type Role {
  id: Int!
  name: String!
  permissions: [Permission]
}

type PermRoleNotFoundError {
  message: String!
}

enum PermissionModel {
  USER
  ORDER
  CUSTOMER
  PERMISSION
  ROLE
}

enum PermissionVerb {
  CREATE
  READ
  UPDATE
  DELETE
}

最后是解析器中的违规代码:

updatePermission.ts

export const updatePermission: Resolver<
  ResolversTypes['UpdatePermissionInput'],
  {},
  Context,
  RequireFields<MutationUpdatePermissionArgs, never>
> = async (_parent, args, context, _info) => {
  const { id, verb, resource, own } = args.input

  const oldPermission = await context.prisma.permission.findFirst({
    where: { id },
    include: { roles: true }
  })

  const newPermission = await context.prisma.permission.update({
    where: { id },
    data: {
      id: oldPermission.id,
      verb: verb ? verb : oldPermission.verb,
      resource: resource ? resource : oldPermission.resource,
      own: own ? own : oldPermission.own,
    },
  })

  return newPermission
}

我收到以下打字稿警告:

Type '(_parent: {}, args: RequireFields<MutationUpdatePermissionArgs, never>, context: Context, _info: GraphQLResolveInfo) => Promise<...>' is not assignable to type 'Resolver<UpdatePermissionInput, {}, Context, RequireFields<MutationUpdatePermissionArgs, never>>'.
Type '(_parent: {}, args: RequireFields<MutationUpdatePermissionArgs, never>, context: Context, _info: GraphQLResolveInfo) => Promise<...>' is not assignable to type 'ResolverFn<UpdatePermissionInput, {}, Context, RequireFields<MutationUpdatePermissionArgs, never>>'.
Type 'Promise<Permission>' is not assignable to type 'UpdatePermissionInput | Promise<UpdatePermissionInput>'.
Type 'Promise<Permission>' is not assignable to type 'Promise<UpdatePermissionInput>'.
Type 'Permission' is not assignable to type 'UpdatePermissionInput'.
Types of property 'verb' are incompatible.
Type 'import(\"/Users/jrichardson/Documents/Projects/wsw/node_modules/.prisma/client/index\").PermissionVerb' is not assignable to type 'import(\"/Users/jrichardson/Documents/Projects/wsw/backend/src/generated/graphql\").PermissionVerb'.
Type '\"CREATE\"' is not assignable to type 'PermissionVerb'.

我不明白为什么它认为 PermissionVerb 不可分配 - 正如我从 .prisma/client/index 和 generated/graphql 中看到的那样,两个枚举都是相同的。不确定我在这里缺少什么。

任何帮助将不胜感激!谢谢

【问题讨论】:

    标签: typescript graphql apollo-server prisma graphql-codegen


    【解决方案1】:

    您还可以通过在 codegen.yml 中设置它来告诉 graphql-codegen 创建枚举为 const(字符串联合):

    config:
      enumsAsConst: true
    

    【讨论】:

      【解决方案2】:

      prisma 枚举不能分配给 graphql 枚举.. 这就是问题

      按 graphql 的类型转换返回值..

      import { Permission  as PermissionQL  } from "../../generated/graphql";
      ...
      return newPermission as PermissionQL;
      

      这样我解决了同样的问题。

      【讨论】:

        猜你喜欢
        • 2020-08-10
        • 2021-04-07
        • 1970-01-01
        • 2013-10-27
        • 1970-01-01
        • 2019-03-09
        • 2016-01-02
        • 2019-08-31
        相关资源
        最近更新 更多