Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free

Uploading files


supports file uploads via the GraphQL multipart request specification.

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

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

In this example, the 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 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:

// If you're on Android/JVM, you can turn a File into an upload
val upload = File.toUpload("application/json")
// On multiplatform, you can use `DefaultUpload`
val upload = DefaultUpload.Builder()
.fileName("filename.txt")
.content { sink ->
okioSource.use { sink.writeAll(it) }
}
.build()

And execute your mutation:

val response = apolloClient.mutation(SingleUploadMutation(file = upload)).execute()
Previous
UI Tests
Next
Enforcing non-nullability
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company