简体   繁体   English

React-Apollo-GraphQL 通用查询和突变

[英]React-Apollo-GraphQL Generic Queries and Mutations

This question is more about design .这个问题更多的是关于设计 In my heavy app and want to implement generic mutation and query action.在我的繁重应用程序中,想要实现通用突变查询操作。 My first thought was to implement a function that encapsulates all queries or mutations in one place.我的第一个想法是实现一个 function 将所有查询或突变封装在一个地方。 Like this:像这样:

function query(props) {
  const Query = require('dynamic/query/based/on/props.path.gql');
  const { data, loading, ...} = useQuery(Query, ...);
  return { data, loading, ... };
}

then use it然后使用它

import Query form './query';
function SomeMainFunction(props) {
  cont { data, loading, ...} = Query(props);
  return data.map(...);
}

(Similar for mutation) It seems to me like this is causing unnecessary rerendering even with React.memo . (与突变类似)在我看来,即使使用React.memo也会导致不必要的重新渲染。 The second approach was to make use of useContext .第二种方法是利用useContext But I don't really see how to deal with network status in this case.但在这种情况下,我真的不知道如何处理网络状态。 Meaning having loading true and then false .意味着先loading true然后再加载false Or will useQuery handle this by re-rendering the component once the data arrives?还是useQuery会在数据到达后通过重新渲染组件来处理这个问题?

Of cause, the third solution is to implement this logic directly in the function/component.当然,第三种解决方案是直接在函数/组件中实现这个逻辑。 Something I don't want because of code splitting.由于代码拆分,我不想要的东西。

I end up using the above-mentioned design with proper use of memo and it solved my issue.我最终使用了上述设计并正确使用了memo ,它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM