您需要为 'InputType' class 的 'file' 提供显式类型。 |类型-graphql

You need to provide explicit type for 'file' of 'InputType' class. | Type-graphql

我尝试创建一个 GraphQL 端点,我可以在其中上传文件。

在我的解析器中,我将自定义类型的上下文和数据作为参数

import { ReturnType, InputType } from '@pkg/schemas'
import { Context } from '@pkg/types'
import { GraphQLUpload } from 'graphql-upload'

...
  @Mutation(() => ReturnType)
  uploadFileAndData(
    @Ctx() ctx: Context,
    @Arg('data', () => GraphQLUpload, { description: 'File Upload' }) data: InputType,
  ): Promise<ReturnType> {
    return functionWithMagic({ ctx, data })
  }

我的输入类型如下:

import { FileUpload } from 'graphql-upload'

...
@InputType({ description: 'Upload data input' })
export class InputType {
  @Field({
    nullable: true,
    description: 'File upload',
  })
  @Joiful.any()
  file: FileUpload
}

但是当我尝试 运行 我的代码时,我收到一条错误消息:

Error: Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for 'file' of 'InputType' class.

为了解决这个问题,我更改了以下内容

  @Mutation(() => ReturnType)
  uploadFileAndData(
    @Ctx() ctx: Context,
    @Arg('data', { description: 'File Upload' }) data: InputType,
  ): Promise<ReturnType> {
    return functionWithMagic({ ctx, data })
  }

并在我的 ReturnType 中添加了

import { FileUpload, GraphQLUpload } from 'graphql-upload'

...
@InputType({ description: 'Upload data input' })
export class InputType {
  @Field(() => GraphQLUpload)
  @Joiful.any()
  file: FileUpload
}