Multipart with Java

Hi,

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(CreateTemplateMutation_VariablesAdapter.java:51)

Thanks,
Krishna

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?

Thanks!

@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?

Thanks
Arjan

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
    useVersion2Compat()
    alwaysGenerateTypesMatching.set([
           ...
    ])
    customScalarsMapping = ["Date": "java.time.OffsetDateTime",]
    mapScalarToUpload("Upload")
}

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")
mapScalarToUpload("Upload")
1 Like

Perfect. Thanks a bunch!