简体   繁体   English

打字稿和graphql-codegen之间的枚举不匹配

[英]Enum mismatch between typescript and graphql-codegen

I'm currently trying to wrap my head around the below issue, and not getting very far.我目前正试图解决以下问题,并没有走得太远。 Currently, I'm running an Apollo-Server as my graphQL server, have prisma2 configured with a postgres database, and am utilizing graphql-codegen to generate the types from my schema.目前,我正在运行 Apollo-Server 作为我的 graphQL 服务器,prisma2 配置了 postgres 数据库,并且正在利用 graphql-codegen 从我的模式生成类型。 Currently, I'm trying to update a model that has a many-to-many relationship and getting really strange typescript errors being thrown.目前,我正在尝试更新一个具有多对多关系的模型,并且会抛出非常奇怪的打字稿错误。 I'm pretty sure I'm doing the update incorrectly, but the errors I'm seeing aren't helpful.我很确定我做的更新不正确,但我看到的错误没有帮助。

The following is the pertinent code snippets:以下是相关的代码片段:

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权限.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
}

and finally the offending code in the resolver:最后是解析器中的违规代码:

updatePermission.ts更新权限.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
}

I'm getting the following typescript warning:我收到以下打字稿警告:

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'.

I don't understand why it thinks the PermissionVerb is not assignable - as from what I can see from .prisma/client/index and the generated/graphql, both enums are identical.我不明白为什么它认为 PermissionVerb 不可分配 - 从我从 .prisma/client/index 和生成的/graphql 中可以看到,两个枚举是相同的。 Not sure what I'm missing here.不确定我在这里缺少什么。

Any help would be greatly appreciated!任何帮助将不胜感激! Thanks谢谢

the prisma enum not assignable to the graphql enum .. thats the problem棱镜枚举不可分配给graphql枚举..这就是问题

Cast the return by the type of the graphql..按graphql的类型转换返回..

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

In that way I solve the same problem.这样我就解决了同样的问题。

You can also tell graphql-codegen to create enums as const (string unions) by setting this in codegen.yml :您还可以通过在codegen.yml设置来告诉 graphql-codegen 将枚举创建为 const(字符串联合):

config:
  enumsAsConst: true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Graphql-codegen 在 Typescript 中实现 GraphQL 解析器? - How do I implement GraphQL resolvers in Typescript with Graphql-codegen? 带有插值的 graphql-codegen 动态字段 - graphql-codegen dynamic fields with interpolation 使用 typescript + 生成的核心用作 package 调用 graphql-codegen 查询时,挂钩调用无效 - Invalid hook calls when calling a graphql-codegen query with typescript + generated core used as a package 使用@graphql-codegen/cli 在 graphql 查询中获取子类型 - get sub types in graphql query using @graphql-codegen/cli Graphql打字稿打字和代码生成 - Graphql typescript typing and codegen graphql 响应和 Z1E35BCBA2DBA1089C7BD0805D4517C8 之间的 Typescript 和 GraphQL 不匹配类型 - Typescript and GraphQL mismatch types between graphql response and typescript type 当 Apollo Server 处于生产模式时,如何在 graphql-codegen 的帮助下为 React 生成 Graphql 类型? - How to generate Graphql types for React with help of graphql-codegen when Apollo Server is in production mode? Laravel Lighthouse:如何让 graphql-codegen 从我的模式生成类型? - Laravel Lighthouse: How can I get graphql-codegen to generate typings from my schema? 在客户端解析器中导入类型时,如何避免使用 Apollo 客户端和 `graphql-codegen` 的角度项目中的循环依赖? - How to avoid circular dependency in angular project with Apollo client and `graphql-codegen` when importing types in client resolver? 在 Angular 应用程序中使用 graphql-codegen 时出现错误“对象类型未知” - Error "Object is of type unknown" when using graphql-codegen in the Angular application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM