Dealing with File uploading migration 2.0->3.0 ...
# apollo-kotlin
s
Dealing with File uploading migration 2.0->3.0 tl;dr -> Before we were passing in a path string to FileUpload(mimeType, path) but now I’m not sure what’s the functionally equivalent thing to do. We used to do:
Copy code
// graphqls
mutation UploadFile($file: Upload!){
  uploadFile(file: $file) { //stuff }
}
uploadFile(file: Upload!): File!
type File { // stuff }

// kt
private suspend fun uploadFile(path: String, mimeType: String): Response<UploadFileMutation.Data> {
    val uploadFileMutation = UploadFileMutation(
        file = FileUpload(mimeType, path)
    )
    return apolloClient.mutation(uploadFileMutation).execute()
}
and it’d work. Mind you, we were calling this method once with a file that we did
.path
on it, and once on a
Uri
that we did
.path
on it too. (so not all of our cases can be fixed with the Now with the new documentation mentioning “okioSource” as someone who has never worked with okio it’s not obvious what I should do to not break this. Maybe worth a mention in the migration logs? At least a link to okio documentation on this?
m
You can use a
file
on the JVM:
Copy code
// or if you're on the JVM
val upload = DefaultUpload.Builder()
              .content(file)
              .build()
We can add an overload that takes a
path
but it feels a bit superfluous given that you can get a File from a path with
File(path)
:
Copy code
val upload = DefaultUpload.Builder()
              .content(File(path))
              .build()
s
Aha, was that what the 2.0 version was doing when given a path?
m
let me check
Copy code
fun FileUpload.Companion.create(mimetype: String, filePath: String): FileUpload {
  val file = File(filePath)
  return object : FileUpload(mimetype) {
    override fun contentLength() = file.length()
    override fun fileName() = file.name
    override fun writeTo(sink: BufferedSink) {
      sink.writeAll(file.source().buffer())
    }
  }
}
s
Ah thank you a lot!
m
Sure thing