admin管理员组文章数量:1401673
How can I parse a stored JSON response in React and pass to a component that is expecting graphQL response?
Is there a way to add the __typename
s from the query RatingQuery
?
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,
});
const ratingSummaryData = client.cache.readQuery<RatingQuery>({ query: __Query });
console.log('ratingSummaryData:', ratingSummaryData);
This is how ratingDataWithoutTypename
looks:
const ratingDataWithoutTypename = {
"productRatingSummary": {
"sectionHeadingAccessibilityText": "Reviews",
"summary": {
"primary": "9.0",
"secondary": "Wonderful",
"theme": "positive"
},
"info": null,
}
}
though only root elements are getting read, and the objects are empty - summary
:
{
"productRatingSummary": {
"sectionHeadingAccessibilityText": "Reviews",
"summary": {},
"info": null
}
}
If I add __typename
to each object, this works well. So need to find a way to do that. The JSON is coming from a REST API.
How can I parse a stored JSON response in React and pass to a component that is expecting graphQL response?
Is there a way to add the __typename
s from the query RatingQuery
?
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,
});
const ratingSummaryData = client.cache.readQuery<RatingQuery>({ query: __Query });
console.log('ratingSummaryData:', ratingSummaryData);
This is how ratingDataWithoutTypename
looks:
const ratingDataWithoutTypename = {
"productRatingSummary": {
"sectionHeadingAccessibilityText": "Reviews",
"summary": {
"primary": "9.0",
"secondary": "Wonderful",
"theme": "positive"
},
"info": null,
}
}
though only root elements are getting read, and the objects are empty - summary
:
{
"productRatingSummary": {
"sectionHeadingAccessibilityText": "Reviews",
"summary": {},
"info": null
}
}
If I add __typename
to each object, this works well. So need to find a way to do that. The JSON is coming from a REST API.
1 Answer
Reset to default 0Got answer here: https://github/apollographql/apollo-client/issues/12472?reload=1
The cache would only accept that like you do it in the first post if you already had the
__typename
in. The cache doesn't know about your schema, so it can't make up__typename
properties that don't already come from the server.
本文标签: reactjsParse JSON response to apolloclient queryStack Overflow
版权声明:本文标题:reactjs - Parse JSON response to apollo-client query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744314256a2600183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论