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

76 thoughts on “Trying to refetch the data using Apollo query”

Leave a Comment