Skip to content

Instantly share code, notes, and snippets.

@stemmlerjs
Last active February 26, 2020 16:54
Show Gist options
  • Select an option

  • Save stemmlerjs/de1a96126ba278cffa4cbeb147315cff to your computer and use it in GitHub Desktop.

Select an option

Save stemmlerjs/de1a96126ba278cffa4cbeb147315cff to your computer and use it in GitHub Desktop.
async singleFileUploadResolver(
parent,
{ file }: { file: Promise<File> }
): Promise<ApolloServerFileUploads.UploadedFileResponse> {
const { stream, filename, mimetype, encoding } = await file;
// Create the destination file path
const filePath = this.createDestinationFilePath(
filename,
mimetype,
encoding
);
// Create an upload stream that goes to S3
const uploadStream = this.createUploadStream(filePath);
// Pipe the file data into the upload stream
stream.pipe(uploadStream.writeStream);
// Start the stream
const result = await uploadStream.promise;
// Get the link representing the uploaded file
const link = result.Location;
// (optional) save it to our database
return { filename, mimetype, encoding, url: result.Location };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment