Stylianos Gakis
05/19/2022, 7:50 AMOperation.Data
and I would like to get their Json String representation. I have kind of by accident stumbled upon this function which is jvm only [1] and I think this almost is what I am looking for.
However just had a question, there isn’t a toJsonString
equivalent in there which is what I would like to get instead. And I can’t quite build my own version of it locally since I adapter()
is private in that file and I don’t feel like copying that one into our project.
I guess what I can do instead locally is imitate what the Adapter<D>.toJsonString is doing, with something like this right?
fun Operation.Data.toJsonString(
scalarTypeAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
indent: String? = null,
): String {
val buffer = Buffer()
val jsonWriter = BufferedSinkJsonWriter(buffer, indent)
toJson(jsonWriter, scalarTypeAdapters)
return buffer.readUtf8()
}
But optimally, I’d like that function to be available to me from the library, where it can use the private adapter()
function and not have to deal with buffers etc. in my local code. I am definitely not super confident with using them.
So in short, do you think it’d fit to have a function with this body directly under this one, or should I just do the workaround I posted above (or something else if it is wrong of course)
fun Operation.Data.toJsonString(customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, indent: String? = null): String {
return adapter().toJsonString(this, customScalarAdapters, indent)
}
Random question:
[1] Is this due to the getDeclaredField
calls etc? Is that using reflection only available on the JVM or something?bod
05/19/2022, 8:28 AMfun Operation.Data.toJsonString(
scalarTypeAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
indent: String? = null,
): String = buildJsonString(indent) {
toJson(this, scalarTypeAdapters)
}
mbonnin
05/19/2022, 8:30 AMString
and File
and I removed them just before going 3.0 because I wasn't sure how it should all workbod
05/19/2022, 8:35 AMmbonnin
05/19/2022, 8:37 AMbod
05/19/2022, 8:38 AMmbonnin
05/19/2022, 8:39 AMData
class with some extra bytecodeData
is purely ... well... dataMyQuery.Data
to just MyQuery
bod
05/19/2022, 8:42 AMStylianos Gakis
05/19/2022, 8:47 AMbuildJsonString
was internal API and didn’t use it in my local code, and therefore forgot to use it in the apollo-kotlin code too.
I can update my PR, but it seems like you’d optimally want a different approach anyway right? And I am perfectly happy to use the buildJsonString locally anyway.
So I can instead close the PR and reference this chat here, what do you think?bod
05/19/2022, 8:48 AMmbonnin
05/19/2022, 8:49 AM@ApolloExperimental
so that we get a chance to change it if we have other thoughtsbod
05/19/2022, 8:50 AMStylianos Gakis
05/19/2022, 8:51 AMmbonnin
05/19/2022, 8:59 AMdata
without having operation
around? In the Apollo codebase, these 2 are always found together (see ApolloResponse
, ApolloStore.writeOperation
, ...)Stylianos Gakis
05/19/2022, 8:59 AMbuildJsonString
, which one do you prefer?
2. When I added the @ApolloExperimental annotation, the apiDump produced a different result. This wasn’t something I expected, is that okay?mbonnin
05/19/2022, 9:00 AM@ApolloExperimental
APIs because there's no backward compat garanteesStylianos Gakis
05/19/2022, 9:06 AMpublic static synthetic fun toJsonString$default (Lcom/apollographql/apollo3/api/Operation$Data;Lcom/apollographql/apollo3/api/CustomScalarAdapters;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
With function and no annotation 2 lines ->
public static final fun toJsonString (Lcom/apollographql/apollo3/api/Operation$Data;Lcom/apollographql/apollo3/api/CustomScalarAdapters;Ljava/lang/String;)Ljava/lang/String;
public static synthetic fun toJsonString$default (Lcom/apollographql/apollo3/api/Operation$Data;Lcom/apollographql/apollo3/api/CustomScalarAdapters;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
I guess the difference between the two is the “synthetic” keyword in there, but I am not familiar with how to properly read this file format
These may be a bit silly questions, but I haven’t worked with something like this before so I don’t quite understand it all, thanks for bearing with me 🙌mbonnin
05/19/2022, 9:08 AM$default
version is a false positive from https://github.com/Kotlin/binary-compatibility-validator/synthetic
keyword means no code can compile against the symbol (but it's still present in the bytecode)<https://github.com/Kotlin/binary-compatibility-validator/|binary-compatibility-validator>
reads the symbols from the java bytecode and tries to infer what is "public API". I'd say the $default()
method up there isn't public API...Stylianos Gakis
05/19/2022, 9:13 AMmbonnin
05/19/2022, 9:15 AMStylianos Gakis
05/19/2022, 9:39 AM