简体   繁体   English

从后端 graphql 获取通知以在调用突变时对前端做出反应

[英]Get notifications form backend graphql to frontend react while calling mutation

In my code I have done a mutation call from frontend (react) to backend which run the 5 file of SQL query.在我的代码中,我做了一个从前端(反应)到后端的突变调用,它运行 SQL 查询的 5 个文件。 But in frontend I am showing loading...但是在前端我正在显示加载...

//frontend
<Button onClick={()=>addData()} />
Const addData = async ()=>{
const result = await AddDataMutation({data:somedata});
console.log(result)
}
const mutation = graphql`
  mutation AddDataMutation($input: [String]) {
   runQuery (input: $input)
  }
`;
export default (data, callback) => {
  return new Promise((resolve, reject) => {
    const variables = {
      input: fileName,
    };
    commitMutation(environment, {
      mutation,
      variables,
      onCompleted: (response, error) => {
        console.log("update done")
      },
      onError: (err) => console.error(err),
    });
  });
};
//Backend
function runQuery(arr) {
      log.info('backend called', arr);
      let fileResult;
      for (let j = 0; j < arr.length; j++) {
      const output = pgpool.query(sqlQueryFile)      
      }
      return fileResult;
  };

After run all queries frontend get "update done" msg but I want to show a progress bar like 1 of 5 file updated .运行所有查询前端后得到“更新完成”消息,但我想显示一个进度条,如1 of 5 file updated

After googling i found socket io and websocket but I don't get any think.. how to use this in my project and others way to do this谷歌搜索后,我发现socket iowebsocket但我没有任何想法......如何在我的项目中使用它以及其他方式来做到这一点

Graphql subscriptions can be helpful in solving this issue. Graphql 订阅有助于解决此问题。 You can create a subscription which provides update on every single sql query run.您可以创建一个订阅,为每个 sql 查询运行提供更新。 Here is the reference link https://www.howtographql.com/graphql-js/7-subscriptions/ .这是参考链接https://www.howtographql.com/graphql-js/7-subscriptions/

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

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