0

When using a Fragment in my query for Graphql call, I am facing below issue with Fragment during Moshi conversion.

I have query like this

fetchData($userId: String!) 
{
  ...UserDataFields
}
fragment UserDataFields on UserData {
id,
name,
profileImage
}

When I make Api call and copy the response from Android logcat, convert it to json file and use that with Moshi converter in my unit tests to mock the response, I see the response does not include UserDataFields, it has below:

{
"data": {
"fetchData": {
  "__typename": "UserData",
  "id": "23455",
  "name": "example",
  "profile": {
   ....
   }
 }
}

But my kotlin generated types has

Data(
public val fetchData: FetchData?
):Query.Data

FetchData(
public val __typename: String,
public val userDataFields: UserDataFields
)

UserDataFields(
public val id: String,
public val name: String,
public val profile: Profile,
): Fragment.Data

It is looking for the UserDataFields data inside the response json but I am getting the properties directly in response without being nested inside UserDataFields.

Here is the Moshi Conversion code:

val mockResponse = typeFromLocalJson<UserDetailsSearchQuery.Data>(
"my json response file path",
Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
).getOrhandle {throw it }

It is not throwing an exception, it is looking for UserDataFields property inside fetchData..which is not available in the json, Hence returning null.

Can someone please help me fix this, thanks in advance.

1 Answer 1

1

When using fragments, it is expected that the shape of the generated model is different from the shape of the JSON payloads.

In the generated models, each fragment has its own field, whereas in JSON the fragment's fields are embedded in the main object.

Because of this you cannot serialize the generated models into JSON via Moshi and expect the generated parser to be able to read them.

Instead, you can serialize them using the Apollo generated adapters, which knows how to handle these fragment fields, using operation.composeJsonResponse() (see the doc).

Alternatively for your tests you could use the TestNetworkTransport.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your response, This solution works for the test module of my project, we also have a mock module in which we store json response files in assets and override API calls to return response from this mock assets. Say ProfileService is where we have list of API calls in main module..in mock module we inherit ProfileService and override all the API call functions and make them read json file from assets using Moshi with the type that API function returns. Since I am using frgaments, Moshi is not working for me. Can you help me fix it?
Hi! I think this is the same problem, and can use the same solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.