[英]How do I implement GraphQL resolvers in Typescript with Graphql-codegen?
[英]Enum mismatch between typescript and graphql-codegen
我目前正試圖解決以下問題,並沒有走得太遠。 目前,我正在運行 Apollo-Server 作為我的 graphQL 服務器,prisma2 配置了 postgres 數據庫,並且正在利用 graphql-codegen 從我的模式生成類型。 目前,我正在嘗試更新一個具有多對多關系的模型,並且會拋出非常奇怪的打字稿錯誤。 我很確定我做的更新不正確,但我看到的錯誤沒有幫助。
以下是相關的代碼片段:
棱鏡模式
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
}
權限.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
}
最后是解析器中的違規代碼:
更新權限.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 和生成的/graphql 中可以看到,兩個枚舉是相同的。 不確定我在這里缺少什么。
任何幫助將不勝感激! 謝謝
棱鏡枚舉不可分配給graphql枚舉..這就是問題
按graphql的類型轉換返回..
import { Permission as PermissionQL } from "../../generated/graphql";
...
return newPermission as PermissionQL;
這樣我就解決了同樣的問題。
您還可以通過在codegen.yml
設置來告訴 graphql-codegen 將枚舉創建為 const(字符串聯合):
config:
enumsAsConst: true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.