Multipart with Java


I was referring to : Uploading files - Apollo GraphQL Docs. Is there a way to handle multipart in java with JVM outside of kotlin?

I tried upload a file but it doesn’t work as we do not work on JSON format. We have a custom format for the upload file, is there a way in which we can handle this usecase? When every i pass File object fails with below error

Error :java.lang.IllegalStateException: Cannot write test.sls to Json
at com.apollographql.apollo3.api.json.-JsonWriters.writeAny(JsonWriters.kt:40)
at com.apollographql.apollo3.api.Adapters$AnyAdapter$1.toJson(Adapters.kt:176)
at com.apollographql.apollo3.api.Adapters$AnyAdapter$1.toJson(Adapters.kt:184)
at com.vmware.guardrails.e2e.adapter.CreateTemplateMutation_VariablesAdapter.toJson(


Hi! :wave:

Did you declare the custom scalar used for uploads with mapScalarToUpload?

Yeah it was tried but face same issue during gralew build because it trying to convert the multipart file to json again. Do we support any other file formats other than json.

Hmm, I’m not entirely sure of what you mean by JSON in this context.

Could you show what you tried in the gradle plugin configuration, and in your code, as well as the GraphQL mutation and the relevant part of your GraphQL schema?


@Benoit_Lubek We are using
customScalarMapping for Date. But now we have a requirement where in we have to start supporting FileUploads.
If I add mapScalarToUpload then my build errors saying that I could either use mapScalarToUpload or
customScalarMapping. Any suggestions how to get past this with the least possible code change?


Hi! Could you paste your Gradle Apollo configuration, as well as the exact error you are seeing? Thanks!

apollo {

    generateApolloMetadata.set(true) // Required for multimodule support
    customScalarsMapping = ["Date": "java.time.OffsetDateTime",]

Error message:-

Apollo: either mapScalar() or customScalarsMapping can be used, but not both

Let me know if you would want the entire Stacktrace.

That is because customScalarsMapping has been deprecated in favor of mapScalar / mapScalarToXyz. Here’s how you should set it up:

mapScalar("Date", "java.time.OffsetDateTime")
1 Like

Perfect. Thanks a bunch!