Graphql 如何转换从@apollo/react hooks接收到的数据
我正在使用``查询:Graphql 如何转换从@apollo/react hooks接收到的数据,graphql,react-hooks,react-apollo,Graphql,React Hooks,React Apollo,我正在使用``查询: const GET_COMMENTS = gql` query getComments { getComments (where: {code: "/about-us" }) { id text createdAt } } ` .... const { loading, error, data } = useQuery(GET_COMMENTS); 在我
const GET_COMMENTS = gql`
query getComments {
getComments (where: {code: "/about-us" }) {
id
text
createdAt
}
}
`
....
const { loading, error, data } = useQuery(GET_COMMENTS);
在我的代码中
代码正在从服务器获取注释,注释对象是date:string代码>属性,我想转换这个日期:字符串
转换为javascript日期(新日期(comment.date)
)如何在获取数据后直接执行
我不想变换调用的每个渲染
有这样的东西吗
const { loading, error, data } =
useQuery(
GET_COMMENTS,
{
transform: (data) => data.map(comment => ({...comment , date: new Date(comment.date)}) )
}
);
谢谢你的帮助 缓存内类型策略就是您要寻找的。它允许您定义将传入数据写入缓存的自定义策略
创建缓存时,可以定义如何写入特定字段。假设date
字段属于Comment
类型:
const cache=new InMemoryCache({
类型策略:{
评论:{
字段:{
日期:{
合并(日期){
返回新日期(日期);
},
},
},
},
},
});
试试这个:
const { loading, error, data } = useQuery(GET_COMMENTS, {
onCompleted: data => {
// Do changes here
}
});
你可以在这里查一下