简体   繁体   English

尝试使用 Apollo 查询重新获取数据

[英]Trying to refetch the data using Apollo query

I am trying to refetch the data after an action is done but i am failing at refetching and the page is not refreshing with the data.我正在尝试在操作完成后重新获取数据,但我无法重新获取并且页面没有刷新数据。

Below is the code for mutation and fetch queries:下面是突变和获取查询的代码:

const {
    data: designHubProjectData,
    loading: designHubProjectDataLoading,
    error: designHubProjectDataError
  } = useQuery(ALL_DESIGNHUB_PROJECTS, {
    fetchPolicy: 'network-only',
    variables: {
      order: [{ projectNumber: 'DESC' }]
    }
  });

  const [insertEmployeeDesignHubProjectBookmarkMutation] = useMutation(
    INSERT_EMPLOYEE_DESIGNHUB_PROJECT_BOOKMARK,
    {
      refetchQueries: [     // here i am calling two queries after insert
        {
          query: EMPLOYEE_DESIGNHUB_PROJECT_BOOKMARK,
          variables: {
            order: [{ projectNumber: 'DESC' }]
          }
        },
        {
          query: ALL_DESIGNHUB_PROJECTS,
          fetchPolicy: 'network-only',
          variables: {
            order: [{ projectNumber: 'DESC' }]
          }
        }
      ]
    }
  );

and then below is the method where i am calling above mutation然后下面是我在突变上方调用的方法

  const handleAddBookmark = record => {
    insertEmployeeDesignHubProjectBookmarkMutation({
      variables: {
        employeeId: loggedInEmployee.id,
        projectNumber: record.projectNumber
      }
    }).then(({ data }) => {
      if (data.insertEmployeeDesignHubProjectBookmark.ok) {
        notification.success({
          message: 'Success',
          description: 'Successfully bookmarked the project.'
        });
      } else {
        const errors = data.insertEmployeeDesignHubProjectBookmark.errors.join(', ');
        notification.error({
          message: 'Error',
          description: `Adding bookmark to the project failed: ${errors}.`
        });
      }
    });
  };

i am not sure where I am doing wrong with the above code.我不确定上面的代码在哪里做错了。 Could any one please let me know any suggestion or ideas how to refetch make it work, many thanks in advance任何人都可以让我知道如何重新获取使其工作的任何建议或想法,非常感谢提前

I have solved this problem by assigning refetch to Oncompleted method like as below,我通过将refetch分配给Oncompleted方法解决了这个问题,如下所示,

  const {
    data: designHubProjectBookmarkData,
    loading: designHubProjectBookmarkDataLoading,
    error: designHubProjectBookmarkDataError,
    refetch: refetchBookmarkProjects
  } = useQuery(EMPLOYEE_DESIGNHUB_PROJECT_BOOKMARK, {
    fetchPolicy: 'network-only',
    variables: {
      order: { projectNumber: 'DESC' }
    }
  });


const [insertEmployeeDesignHubProjectBookmarkMutation] = useMutation(
    INSERT_EMPLOYEE_DESIGNHUB_PROJECT_BOOKMARK,
    {
      onCompleted: refetchBookmarkProjects
    }
   );

if it incase anyone in the future looking for the same, this is an example.如果它以防万一将来有人寻找相同的东西,这就是一个例子。

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

相关问题 尝试重新获取时 Apollo 客户端上的循环错误 - Circular error on Apollo Client when trying to refetch 从外部使用Apollo GraphQL refetch方法 - Using Apollo GraphQL refetch method from outside 用不同的数据重新获取反应查询 - refetch with different data react query 如何在方法中使用的vue apollo查询中重新获取/更新结果 - How to refetch / update result in vue apollo query that used in a method 使用 Vue Router 重定向到页面后如何重新获取 apollo 查询? - How to refetch apollo query after i redirect to page with Vue Router? Apollo Client:使用修改后的变量重新获取数据,useQuery 还是 useLazyQuery? - Apollo Client: refetch data with modified variables, useQuery or useLazyQuery? 如果使用 react-query 和 next.js 编辑过滤器,则预取初始数据并重新获取 - Prefetching initial data and refetch if filter is edited using react-query and next.js Apollo Client - 当变量发生变化时,可以只重新获取较大查询的片段吗? - Apollo Client - Possible to refetch only a fragment of a larger query when it's variables change? 如何让 apollo-module 自动重新获取有关查询参数更新的查询 - How can I have apollo-module automatically refetch queries on query param updates 在 Javascript 中使用 GraphQL + Apollo 客户端查询具有部分字符串匹配的数据 - Query data with partial string match using GraphQL + Apollo client in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM