I have a response model for a `mutation` which inc...
# apollo-kotlin
k
I have a response model for a
mutation
which includes a fragment model inside, something like:
Copy code
query getX($deviceBla: ID!) {
    getX(deviceBla: $deviceBla) { 
        ...getY
    }
}
and getY is something like:
Copy code
fragment getY on X
I'm trying to feed a manually generated response from the Apollo Mock Server back to the Apollo Client. In original prod, without the mock server, the entire response on getX returns with getY fragment being replaced with it's inner fields. So getY 's Y class fields gets merged onto X response model But the Query Model itself in kotlin, via adapter, is generated as
Copy code
Data(getX) : Query.Data 

public data class GetX(__typename: String, val getY: Y)
so if I build the response via response builders, or if I build it myself, I end up having to initialise the Y inside getX's response model, and JSON ends up having the key value for Y, when in reality key value for Y is not supposed to be there because fragment replaces the class with it's inner fields and merges it into the upper parent. How can we manually or via response_builders, generate a more complex graphql response like this ? the only difference is Y's inner field methods are merged onto the parent model, I thought this would be easy to achieve, or I expected response builders to intelligently tackle this
b
Hi! > the entire response on getX returns with getY fragment being replaced with it's inner fields Indeed, fragments are a client side syntax, but in the response JSON payloads they will be erased. Querying the fields directly, or via fragments will yield the same response shape. So it is normal and expected that your mock responses will also behave like this and you’ll have to return something that looks like
{"data": {"getX": {"y": {...} } } }
. > How can we manually or via response_builders Are you referring to the data builders or something else?
k
(sdfgsdfg here again) yes, I meant the
data builders
by response builders - I'm trying to JSON serialise these responses but they're not in the structure that they're expected, when I send these responses from mock server to the apollo client. The JSON doesn't end up being a flat structure (fragment model's fields are supposed to be merged onto the parent object) but the resulting JSON of the data builder's response builds the Data models of the queries, there is no response model generation. Data builders seem to generate the queries, not the responses - or I'm missing something here the response structure is not the expected, flat structure that includes the fields of the fragment inside the response, maybe I'm initialising the wrong Data model 😕
b
Data builders are meant to be a way to easily instantiate the models - typically, you would use them with the TestNetworkTransport rather than with the mock server. As you noticed, the models don't have the same shape as the JSON response when it comes to fragments, which is why if you serialize them as-is it won't work. If you prefer using the mock server, what you could do is serialize the models with toJson / toJsonString (JVM/Android only), which uses the generated adapters to produce the expected JSON.