Parse JSON response to GraphQL

How can I parse a stored JSON response in React and pass to a component that is expecting graphql response?

This is what I am trying, but it is not reading the data correctly:

const client = new ApolloClient({
  cache: new InMemoryCache({ addTypename: true })
});

client.cache.writeQuery({
  query: __Query,
  data: ratingDataWithoutTypename, // JSON response
});
const ratingSummaryData = client.cache.readQuery<RatingQuery>({ query: __Query });
console.log('ratingSummaryData:', ratingSummaryData);

only root elements are getting read, and the objects are empty - summary, metadata:

{
    "productRatingSummary": {
        "sectionHeadingAccessibilityText": "Reviews",
        "summary": {},
        "metadata": {},
        "info": null,
        "impressionAnalytics": null
    }
}

Seems like if the __typename is missing, apollo-client will not parse the data.

  • Is there a way to add the __typename automatically, if the object structure is same?
  • Why are the root level fields being parsed - sectionHeadingAccessibilityText, info etc.