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

查看:38
本文介绍了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.

推荐答案

听起来你想要 Afterware 与允许在发出请求之前进行操作的中间件非常相似,它允许您操作响应中的数据,例如

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天全站免登陆