Nestjs Apollo graphql upload scalar

Use import {GraphQLUpload} from "apollo-server-express"
Not from 'graphql-upload'

import { Resolver, Mutation, Args } from '@nestjs/graphql';
import { createWriteStream } from 'fs';

import {GraphQLUpload} from "apollo-server-express"

@Resolver('Download')
export class DownloadResolver {
    @Mutation(() => Boolean)
    async uploadFile(@Args({name: 'file', type: () => GraphQLUpload})
    {
        createReadStream,
        filename
    }): Promise<boolean> {
        return new Promise(async (resolve, reject) => 
            createReadStream()
                .pipe(createWriteStream(`./uploads/${filename}`))
                .on('finish', () => resolve(true))
                .on('error', () => reject(false))
        );
    }
    
}

enter image description here


I solved it by using graphql-upload library. First i created a class for my scalar using GraphQLUpload from graphql-upload

import { Scalar } from '@nestjs/graphql';

import { GraphQLUpload } from 'graphql-upload';

@Scalar('Upload')
export class Upload {
  description = 'Upload custom scalar type';

  parseValue(value) {
    return GraphQLUpload.parseValue(value);
  }

  serialize(value: any) {
    return GraphQLUpload.serialize(value);
  }

  parseLiteral(ast) {
    return GraphQLUpload.parseLiteral(ast);
  }
}

That i added in my Application module

@Module({
  imports: [
  ...
    DateScalar,
    Upload,
    GraphQLModule.forRoot({
      typePaths: ['./**/*.graphql'],
     ...
      uploads: {
        maxFileSize: 10000000, // 10 MB
        maxFiles: 5,
      },
    }),
  ...
  ],
...
})
export class ApplicationModule {}

i also added Upload scalar in my graphql

scalar Upload
...
type Mutation {
  uploadFile(file: Upload!): String
}

and is worked in my resolver i had acces to the uploaded file.

  @Mutation()
  async uploadFile(@Args('file') file,) {
    console.log('Hello file',file)
    return "Nice !";
  }

(side note : I used https://github.com/jaydenseric/apollo-upload-client#function-createuploadlink to upload the file, in the resolver it is a Node Stream)