Search code examples
typescriptfile-uploadgraphqltypegraphql

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


I try to create a GraphQL endpoint, where I can upload a file.

In my resolver I get as arguments context and data with a custom type

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

My input type looks like:

import { FileUpload } from 'graphql-upload'

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

But when I try to run my code I get an error message:

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


Solution

  • To fix this issue I changed following

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

    and in my ReturnType I added

    import { FileUpload, GraphQLUpload } from 'graphql-upload'
    
    ...
    @InputType({ description: 'Upload data input' })
    export class InputType {
      @Field(() => GraphQLUpload)
      @Joiful.any()
      file: FileUpload
    }