Resolvers GraphQL yoga 的 TypeScript 类型(在上下文中使用 prisma2)

TypeScript Types for Resolvers GraphQL yoga (use prisma2 on context)

对不起我的英语

所以,我需要在 GraphQL yoga 上为解析器创建类型。我使用 graphql-codegen 创建它,但它的工作方式很奇怪。而且他目前没有为 Prisma2 创建类型上下文。

codegen.yml

schema: ./src/schema.graphql
generates:
  ./src/resolvers/resolvers-types.ts:
    plugins:
      - typescript
      - typescript-resolvers
config:
  contextType: './context#Context'

context.ts

import {PrismaClient} from '@prisma/client'

export type Context = { prisma:PrismaClient }

resolvers.ts

import {IResolvers, Mutation} from './resolvers-types'

interface StringIndexSignatureInterface {
    [index: string]: any
}

type StringIndexed<T> = T & StringIndexSignatureInterface

const resolvers: StringIndexed<IResolvers> = {
Query: {
        users: (parent, args, ctx, info) => {
            return ctx.prisma.user.findMany
        }
    }
};

他抛出类型不兼容的错误

没有 StringIndexed 他不工作

请告诉我该怎么做

我找到解决办法 我添加到 codegen.yml contextType

codegen.yml

generates:
  ./src/resolvers/resolvers-types/resolvers-types.ts:
    plugins:
      - typescript
      - typescript-resolvers
      
config:
  contextType: './context#Context'
  enumsAsTypes: true

context.ts

import {PrismaClient} from '@prisma/client'

export type Context = { prisma:PrismaClient}