Created
April 1, 2020 17:03
-
-
Save karladler/476e4db990a13cdae0a627eb9b462dfe to your computer and use it in GitHub Desktop.
graphql upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Resolver, Mutation, Arg, | |
} from 'type-graphql'; | |
import { GraphQLUpload } from 'apollo-server-koa'; | |
import { Stream } from 'stream'; | |
import { createWriteStream } from 'fs'; | |
interface Upload { | |
filename: string; | |
mimetype: string; | |
encoding: string; | |
createReadStream: () => Stream; | |
} | |
@Resolver() | |
export class UploadResolver { | |
@Mutation(() => Boolean) | |
// eslint-disable-next-line | |
async addProfilePicture(@Arg('upload', type => GraphQLUpload) | |
{ | |
createReadStream, | |
filename, | |
}: Upload): Promise<boolean> { | |
return new Promise((resolve, reject) => createReadStream() | |
.pipe(createWriteStream(`./${filename}`)) | |
.on('finish', () => resolve(true)) | |
.on('error', (error) => reject(error))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you integrate, image data size minimize feature.