【问题标题】:You need to provide explicit type for 'file' of 'InputType' class. | Type-graphql您需要为“InputType”类的“文件”提供显式类型。 |类型-graphql
【发布时间】:2021-08-18 16:18:30
【问题描述】:

我尝试创建一个 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.

【问题讨论】:

    标签: typescript file-upload graphql typegraphql


    【解决方案1】:

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

      @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
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 2021-08-20
      • 1970-01-01
      • 2021-12-04
      • 2020-03-01
      • 2020-08-24
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      相关资源
      最近更新 更多