简体   繁体   English

Graphql 调用 Axios 多次调用性能/重构

[英]Graphql Calling Axios Multiple Calls Performance/Refactoring

I'm able to delete and add data into my JSON file, but if I keep pressing the button (toggling add/delete), I get a network failure.我可以删除数据并将数据添加到我的 JSON 文件中,但是如果我一直按下按钮(切换添加/删除),我会遇到网络故障。 I'm assuming that I can't make multiple requests like this?我假设我不能像这样提出多个请求?

const handleSave = save => {
  if (save.toLowerCase() === 'save') {
    props
      .addSaved({
        variables: {
          id: props.id,
        },
      })
      .then(res => {
        setSaved('Unsave');
        setSavedPropData(res.data.addSaved);
      });
  } else {
    props
      .deleteSaved({
        variables: {
          id: savedPropData.id,
        },
      })
      .then(() => setSaved('Save'));
  }
};

The above is the snippet of my handle saving data.以上是我的句柄保存数据的片段。 So each press it toggle's Save or Unsave, and then it will add or delete data into my JSON.所以每次按下它切换保存或取消保存,然后它会在我的 JSON 中添加或删除数据。 I'm currently using json server to test the application.我目前正在使用 json 服务器来测试应用程序。

const savedMutate = gql`
  mutation addSaved($id: String) {
    addSaved(id: $id) {
      id
    }
  }
`;

const deletedMutate = gql`
  mutation deleteSaved($id: String) {
    deleteSaved(id: $id) {
      id
    }
  }
`;

export default _.flowRight(
  graphql(savedMutate, {name: 'addSaved'}),
  graphql(deletedMutate, {name: 'deleteSaved'}),
)(withNavigation(Card));

Is there a better way to doing this if I deploy the application into production?如果我将应用程序部署到生产中,有没有更好的方法来做到这一点? Currently I get the network failure when I'm toggling fast.目前,当我快速切换时,我遇到了网络故障。 I just tried again, is it because my app refreshes every time and while its refreshing, I'm trying to save into my database, but it hasn't been fully mounted?我刚刚又试了一次,是不是因为我的应用程序每次都刷新,并且在刷新时,我试图保存到我的数据库中,但它没有完全安装? Is this only happening in development?这只发生在开发中吗?

mutation:突变:

addSaved: {
  type: SavedType,
  args: {
    id: {type: GraphQLString},
  },
  resolve(parentValue, {id}) {
    return axios
      .post(`http://localhost:3000/saved`, {id})
      .then(res => res.data);
  },
},
deleteSaved: {
  type: SavedType,
  args: {
    id: {type: GraphQLString}
  },
  resolve(parentValue, {id}) {
    return axios
      .delete(`http://localhost:3000/saved/${id}`)
      .then(res => res.data);
  },
},

If you're running your server with nodemon and you're writing to some file in response to requests and this file is being watched by nodemon, then your requests will always trigger a server restart.如果您使用 nodemon 运行您的服务器,并且您正在写入某个文件以响应请求并且该文件正在被 nodemon 监视,那么您的请求将始终触发服务器重新启动。 Since the first request restarts the server, any requests that follow immediately after will fail because your server will now be down while it is starting back up.由于第一个请求会重新启动服务器,因此紧随其后的任何请求都将失败,因为您的服务器现在将在启动备份时关闭。

You need to configure nodemon to ignore the appropriate files or directories .您需要配置 nodemon 以忽略相应的文件或目录 This way, any changes to the files won't cause nodemon to restart your server.这样,对文件的任何更改都不会导致 nodemon 重新启动您的服务器。

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

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