Cannot upload file to Server using Upload mutation

Problem description

I am trying to upload an image using an upload mutation as explained here

but I am always receiving from the server

{"statusCode":500,"code":"ERR_INVALID_ARG_TYPE","error":"Internal Server Error","message":"The \"string\" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Object"}

I am using apollo version 3.7.1

Code and logs

Here is the kotlin code for making the request

val file = File(filePath)
val upload = DefaultUpload.Builder().fileName(file.name).content(file).build()
val response = apolloClient.mutation(UploadAvatarMutation(file = upload)).execute()

NB:

In the code above I couldn’t resolve File.toUpload() as mentioned in the doc

The request:

--> POST https://api.goling.com/v1
Content-Type: multipart/form-data; boundary=0cabe113-64eb-4785-a60a-7a0b1fd1e23f
Content-Length: 701
X-APOLLO-OPERATION-ID: e3250dbd528eb9c0628768ad0a565706bfc9c5f5d5ad9036df2a73dbc20ea6f4
X-APOLLO-OPERATION-NAME: UploadAvatar
20Accept: multipart/mixed; deferSpec=20220824, application/json
 --> END POST (one-shot body omitted)

The response:

<-- 500 https://api.goling.com/v1 (256ms)
date: Sat, 19 Nov 2022 13:18:02 GMT
content-type: application/json; charset=utf-8
cf-ray: 76c93ac00ecc5fbe-MRS
strict-transport-security: max-age=15552000; includeSubDomains
vary: Accept-Encoding
cf-cache-status: DYNAMIC
access-control-allow-credentials: true
content-security-policy: default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests
cross-origin-embedder-policy: require-corp
cross-origin-opener-policy: same-origin
cross-origin-resource-policy: same-origin
 origin-agent-cluster: ?1
referrer-policy: no-referrer
 x-content-type-options: nosniff
 x-dns-prefetch-control: off
 x-download-options: noopen
 x-frame-options: SAMEORIGIN
x-permitted-cross-domain-policies: none
x-xss-protection: 0
server: cloudflare
 alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
{"statusCode":500,"code":"ERR_INVALID_ARG_TYPE","error":"Internal Server Error","message":"The \"string\" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Object"}
 <-- END HTTP (211-byte body)

It’s hard to tell without more details. File upload is not specified by GraphQL so it may be that your backend uses something else than the multipart GraphQL spec. Is there a documentation available somewhere? Or are you able to upload using another client?

It’s working on the web. I am trying to debug this with the backend engineer. I thought maybe it was related to the content type or something.
But why I can’t resolve File.toUpload() on the Android side?

I’m not sure. It’s an extension function. Make sure to import it?

import com.apollographql.apollo3.api.toUpload

If that doesn’t work, I’m not sure. Maybe double check you’re on latest version?

1 Like