Vue Apollo Composable useQuery Does Not Respond to Dynamic Changes in Filter Variables Despite Triggering a Refetch

Currently utilizing Vue Apollo Composable along with useQuery for executing a GraphQL query. Pagination is functioning correctly; however, encountering an issue with the filters.verified variable. Although the variable appears to toggle on the client-side (confirmed through logging), the network request continues to display the initial value as true.

Challenge:

Upon clicking the "RPRs" button, a fetch should be initiated with filters.verified = false. Despite observing the value as false in the console just before the API response, the network tab consistently shows true as the value.

Snippet of pertinent code:

// reactive state controlling pagination
const state = reactive({
  pagination: {
    limit: 50,
    skip: 0
  }
});

// reactive state managing filters.verified value: Boolean
const filters = reactive({
  verified: true
});

const { result, error, loading } = useQuery(
  gql`
    query GetMatches($pagination: PaginationInput, $filters: matchFilterInput) {
      // ...insert query here
    }
  `,
     {
       pagination: state.pagination,
        filters: {
          verified: {
            condition: 'EQUAL',
            value: filters.verified
          }
        }
      },
  {
    fetchPolicy: 'cache-and-network'
  }
);

// logic for prev and next page - pagination
const prevPage = () => {
  if (state.pagination.skip > 0) {
    state.pagination.skip -= 50;
  }
};

const nextPage = () => {
  // ... (logging and pagination logic)
};

// filter buttons actions
const handleRPR = (event?: MouseEvent) => {
  if (filters.verified === false) filters.verified = true;
  // ... (other logic)
};

const handleRPRs = (event?: MouseEvent) => {
  if (filters.verified === true) filters.verified = false;
  // ... (other logic)
};

// ... (Vue watches)

Attempted Solutions:

I've tried manually refetching when pressing the "RPRs" button using the following code snippet:

const handleRPRs = (event?: MouseEvent) => {
      if (filters.verified === true) filters.verified = false
      activeButton.value = 'RPR-s'
      refetch({
        pagination: state.pagination,
        filters: { verified: { condition: 'EQUAL', value: filters.verified } }
      })
    }

This method works initially but disrupts the functionality of prevPage and nextPage. Pressing on prevPage resets the query to its initial state with verified: true. It seems like a separate stack is being added independently from the context, working well individually but losing overall structure.

Inquiry:

Based on my understanding, useQuery reacts to changes in its properties. Removing the filters allows prevPage and nextPage to function smoothly. Simply modifying the skip value triggers a refetch. However, when invoking handleRPRs which switches the value of verified to false, an automatic refetch occurs indicating internal change in the query. Nonetheless, the network tab still reflects:

{
  "variables": {
    "pagination": { "limit": 50, "skip": 0 },
    "filters": { "verified": { "condition": "EQUAL", "value": true } }
  }
}

Is there something specific I may have overlooked? Can you identify why the request never displays value: false, despite the console reflecting the change to false? Could it possibly relate to execution order? Is the request sent prior to changing the value?

Unable to pinpoint the exact issue or troubleshoot further. Any assistance would be greatly appreciated, thank you.

Answer №1

I successfully resolved the issue by implementing the following solution:

// Utilizing reactive state for pagination and filters
    const variables = reactive({
      pagination: {
        limit: 50,
        skip: 0
      },
      filters: {
        verified: {
          condition: 'EQUAL',
          value: true
        }
      }
    })


  const { result, error, loading, refetch } = useQuery(
      gql`
        query GetMatches(
          $pagination: PaginationInput
          $filters: matchFilterInput
        ) {
          getMatches(pagination: $pagination, filters: $filters) {
            _id
            category
           // remaining queried fields here
          }
        }
      `,
      variables,
      {
        notifyOnNetworkStatusChange: true,
        fetchPolicy: 'cache-and-network'
      }
    )

With this implementation, everything is now functioning as intended. By wrapping each variable in a `reactive()` method, any changes to internal reactive properties will trigger useQuery with updated values.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for editing a JSON file and preserving the modifications?

I have a JSON file with a key (food_image) and I am looking to dynamically assign the index value in a loop to it, such as food_image0, food_image1 ... food_image{loop index}. After that, I aim to save this modified JSON file under a new name. All values ...

Guide to concealing pop-up with animation when clicking outside

My question is: Clicking on OPEN should open the popup. Clicking on BACK should close the popup. Clicking outside the popup should also close it. Note: Clicking inside the popup when it is open should not close it. The popup should only close on an outs ...

Rendering a Vue select list before receiving data from a Meteor callback

I am currently facing an issue with populating my events array from a meteor call so that it appears in a select list. The 'get.upcoming' Meteor function returns an array of JSON objects, but it seems like the select list is being rendered before ...

Filling out Django Form Using JavaScript

Trying to pass latitude and longitude values using HTML5 geolocation to auto-fill two form fields in a Django form with JavaScript. Found some solutions, but dealing with a Meta class form that generates HTML automatically, making it difficult to add an "i ...

The Vue component is successfully rendering on localhost, but is not displaying on the server when using Laravel 5.4 with Passport

I attempted to configure an Oauth2.0 server using Passport within Laravel 5.4 by following the steps outlined at . I successfully set it up on my local host, but encountered issues when deploying the code to my production server. Upon accessing the applica ...

"An issue was encountered in the original callback" while utilizing ffi-napi within electron and electron-builder

I am encountering a challenge with my electron app. I am attempting to use ffi-napi to call a dll file, but when I run the electron build, I encounter an "Error in native callback." Here is the setup I am working with: package.json { "name": & ...

Mysterious Loop in JavaScript Unfolding with Three.Js

In order to expand my knowledge of Angular and Three.Js, I am currently working on a prototype SPA that showcases different 3D elements rotating. There are several Angular templates accessible through a navigation menu, each displaying unique rotating elem ...

Modifying the default label for each bubble on a bubble chart with chartjs-plugin-datalabels

Is there a way to add labels to each bubble in the bubble chart using chartjs-plugin-datalabels? For every bubble, I'd like to display the label property of each object within the data.dataset array, such as "Grapefruit" or "Lime". Currently, I'm ...

AngularJS: Understanding the difference between ng-show and using display:none

I recently encountered a scenario where I needed to hide an HTML element by default using CSS. Here's how I did it: HTML: <div class="box"> </div> CSS: .box { display: none; } However, I wanted to be able to show and hide the elem ...

Using an if/else statement to detect if the iFrame is devoid of content

I have a customized youtube video section on my website template that can display either an embedded video or an image based on client preference. Currently, I am trying to implement code that will detect if the youtube video source is empty and then displ ...

preventing the triggering of the event in the code-behind of the form with a modal popup

Hello there! I am working on a .net website with C# codebehind. I have a LinkButton control that, when clicked, should trigger some backend code. However, I also want to display a modal pop confirmation on the front end when the button is clicked. The moda ...

Utilizing Node.js in Phonegap

Currently, I am in the process of creating an iOS application with PhoneGap and I have an interest in incorporating node.js into a specific aspect of it. Is it possible to integrate an instance of node.js within the app using PhoneGap? ...

Encountered a MongoDB connection issue during deployment on ZEIT platform

Hey there! I'm a newcomer to React and I've been working on deploying my app on Zeit. Everything seems to be going smoothly, except for one issue that has popped up on Zeit regarding the error: /usr/src/app/bundle/programs/server/node_modules/ ...

Why won't the CSS update in Next.js when the local state variable changes on page load?

I seem to be facing an issue with a variable stored in localStorage that changes when using a toggle button. The color changes correctly upon toggling the button, but upon page refresh, it doesn't display the correct color saved in local storage. Eve ...

Using Angularjs to dynamically generate and submit tables

I can't seem to figure out this specific situation Data: $scope.MyItem = [ { "__v": 0, "myItemId": "55ed819caefe18e81ffbd2d2", "itemId": "56fec8abb192c870117ed393", "january": 1, "february": 1, ...

Using backslashes to escape JSON values within a value in Angular

When retrieving JSON data from the backend, I often encounter an issue where the value is set to "key": "\$hello" and it results in an "Unexpected token d". Is there a way in Angular to handle or escape these characters once received from the server? ...

Developing a collection of reusable components in a Javascript bundle for enhanced efficiency

I currently have a backend rendered page (using Django) that I want to enhance by incorporating components from PrimeVue and a markdown editor wrapped as a Vue component. Previously, we utilized some simple animations with jQuery which we included directly ...

What is the best way to send ServerSideProps to a different page in Next.js using TypeScript?

import type { NextPage } from 'next' import Head from 'next/head' import Feed from './components/Feed'; import News from './components/News'; import Link from 'next/link'; import axios from 'axios&apo ...

Error message appears due to a timeout during a JQuery JSON request, resulting in an

My local web server is running on port 4444 and serving HTML pages and other files successfully. However, I encounter a timeout issue with the error "ERR_EMPTY_RESPONSE" in the JavaScript console when trying to retrieve JSON data from the server. Interesti ...

Can a sophisticated text editor be utilized without a content management system?

Many website builders utilize rich text editors as plugins to enhance content creation, such as in CMS platforms like Joomla and WordPress. However, can these same editors be easily integrated into a custom website built from scratch using just HTML, PHP ...