Docs
Try Apollo Studio

Uploading files


Apollo Kotlin supports file uploads via the GraphQL multipart request specification.

First, add the following custom scalar mapping to the Apollo Gradle plugin configuration:

build.gradle[.kts]
apollo {
mapScalarToUpload("Upload")
}

In this example, the GraphQL schema defines a custom scalar type named Upload. You can use a different name as needed for your schema.

Note: You don't need to register a type adapter for Upload, because the mapScalarToUpload method registers it automatically.

Now let's consider a mutation that takes an Upload as a parameter:

mutation SingleUpload($file: Upload!) {
singleUpload(file: $file) {
id
path
filename
mimetype
}
}

Create an instance of Upload using one of the factory methods:

val upload = DefaultUpload.Builder()
.content(okioSource)
.build()
// or if you're on the JVM
val upload = DefaultUpload.Builder()
.content(file)
.build()

And execute your mutation:

val response = apolloClient.mutation(SingleUploadMutation(file = upload)).execute()
Edit on GitHub
Previous
Batching operations
Next
Enforcing non-nullability