React Apollo Client-在缓存之前修改查询数据 [英] React Apollo Client - modify query data before it goes to cache

查看:79
本文介绍了React Apollo Client-在缓存之前修改查询数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在将查询响应数据保存到内部缓存之前对其进行修改?我正在使用apollo钩子,但是这个问题与使用apollo客户端(以及HOC&组件)的任何前端方法都有关.

Is there a way to modify query response data before it is saved in the internal cache? I'm using apollo hooks, but this question is relevant to any of front-end approaches using apollo client (HOC & Components as well).

const { data, updateQuery } = useQuery(QUERY, {
  onBeforeDataGoesToCache: originalResponseData => {
    // modify data before it is cached? Can I have something like this? 
    return modifiedData;
  }
});

显然 onBeforeDataGoesToCache 不存在,但这正是我要寻找的行为.结果中有一个 updateQuery 函数,该函数基本上可以完成所需的工作,但是时间不正确.我正在寻找某种东西来作为查询突变中的一个钩子或中间件.

Obviously onBeforeDataGoesToCache does not exist, but that's exactly the behavior I'm looking for. There's an updateQuery function in the result, which basically does what is needed, but in the wrong time. I'm looking for something to work as a hook or a middleware inside the query mutation.

推荐答案

听起来像您想要后件(类似于中间件,它允许在发出请求之前进行操作),使您可以操纵响应中的数据,例如

It sounds like you want Afterware which, much like Middleware that allows operations before the request is made, allows you to manipulate data in the response e.g.

const modifyDataLink = new ApolloLink((operation, forward) => {
  return forward(operation).map(response => {
    // Modify response.data...

    return response;
  });
});

// use with apollo-client
const link = modifyDataLink.concat(httpLink);

这篇关于React Apollo Client-在缓存之前修改查询数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆