Guide on updating the result of a Vue Apollo query within a method

I am looking to update or refetch data in an Apollo query that is used within a method rather than directly through the Apollo object. The challenge I’m facing is that I need to query and update this data after a specific event, so it’s not feasible to use the Apollo object directly in my code.

methods: {
    async fetchEvents() {
      const { data } = await this.$apollo.query({
        query: gql`
            query(
              $someThing: Int,
            ) {
                events (
                  someThing: $someThing,
                ) {
                    total
                    items {
                     ...
                    }
                }
            }
        `,
        variables() {
          return {
            ...
          };
        },
      });
      this.data = data;
    },
  },


  watch: {
    'items.view.organizerId': function callback() {
      this.fetchEvents();
    },
    eventsSearchInputVal: function callback() {
      this.fetchEvents();
    },
    'pagination.statusFilter': function callback() {
      this.fetchEvents();
    },
  },

To summarize, when pagination.statusFilter, eventsSearchInputVal, or items.view.organizerId in the watch section change, the query should be refetched. However, in this current implementation, nothing happens when those variables are modified.

Answer №1

For some reason, I decided to convert the variables() method into an object and surprisingly, it solved the issue. Now this piece of code is functioning as expected:

methods: {
    async fetchEvents() {
      const { data } = await this.$apollo.query({
        query: gql`
            query(
              $someThing: Int,
            ) {
                events (
                  someThing: $someThing,
                ) {
                    total
                    items {
                     ...
                    }
                }
            }
        `,
        variables: { // now treated as an object
            ...
        },
      });
      this.data = data;
    },
  },


  watch: {
    'items.view.organizerId': function callback() {
      this.fetchEvents();
    },
    eventsSearchInputVal: function callback() {
      this.fetchEvents();
    },
    'pagination.statusFilter': function callback() {
      this.fetchEvents();
    },
  },

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

Create an interactive zone on the map

Is there a method to click only within the triangle region without causing any impact on the surrounding areas? I attempted to generate a triangular div using `clip-path`, but unfortunately, it is not compatible with Internet Explorer. Despite thorough r ...

Is there a way to access a computed property within methods?

Currently, I am utilizing this particular package to implement infinite scrolling in Vue. In order to continuously add new elements during each scroll, I fetch JSON data from my API server and store it in a data object. Subsequently, I divide the array in ...

The width and height properties in the element's style are not functioning as expected

let divElement = document.createElement("div"); divElement.style.width = 400; divElement.style.height = 400; divElement.style.backgroundColor = "red"; // num : 1 divElement.innerText = "Hello World "; // num : 2 document.body.append(divElement); // Af ...

Observing, contrasting, and sending revised form information to an API through Axios in Vue 3

Can anyone assist me in finalizing my code? This is the progress I have made so far: I am fetching options from an API, which is why I initially set the state to empty. After receiving a response from the API, I update the options state. The form is disp ...

Modify the DOM element stored in a variable before inserting it

Currently, I am using a function that appends elements to the page by using a template. The code snippet looks like this: var template = '<div class="offer-container"><a class="link" href="${link}" target=" ...

Traversing through Object consisting of dual arrays within VueJS

Currently, I am working on a chat application built in VueJS and encountering an issue when attempting to display messages along with their respective timestamps. The challenge arises from the need to iterate through an object that contains two arrays: one ...

What could be causing my initial Vue component not to compile? / Steps for loading the vue-formio module

As a newcomer to both Vue and Form.io, I seem to be missing something simple here. The error message "Module not found: Error: Can't resolve 'vue-formio'" keeps popping up in my Form.vue component: <template> <formio src="https:/ ...

Waiting for state changes in React by using the UseState Hook

I am currently working on a function that manages video playback when clicked, and I need to set consecutive states using the useState hook. However, I want to ensure that the first state is completed before moving on to the next setState without relying ...

Do bluebird promises require the use of async keyword?

Currently, I am looking to integrate Bluebird promises into my project which is built using NodeJS, Express, and Mongodb. Below is an excerpt from my model file: const mongoose = require('mongoose') // Blue Bird mongoose.Promise = require(&apo ...

Adjusting the height of a Vue component to 100% triggers a change in height whenever a re-render occurs

In my Vue component, there is a class called "tab-body-wrapper" that serves the purpose of displaying the "slot" for the active tab. However, I encountered an issue while troubleshooting where the height of the ".tab-body-wrapper" component reduces during ...

The <servicename> is inaccessible within an error function in Angular2

I encountered an unusual issue in angular2. While the code below is functioning correctly: loginResult.subscribe( (data) => this.response = data, (err) => this._ajaxService.handleError(err, 'A string to summarize th ...

Trouble with AJAX request when trying to connect to a distant server

I am facing an issue with my AJAX request. When I test it on localhost, everything works perfectly fine. However, when I upload the code to a remote server, the request fails without any error messages. Even after checking in Firefox and Chrome, there ar ...

I'm currently facing a conflict problem and I'm struggling to identify the root cause

I have integrated a date picker into my form, using the following resources: for the date picker plugin and 1.9.1 jquery.js for jQuery version. However, I encountered an error in this file: TypeError: $.isPlainObject is not a function if ( $.isPlainObje ...

Ways to determine if your code is written in ES6

After completing a lot of Javascript coding, I just learned about the existence of various JS 'versions' such as ES5 and ES6. My project is now up on Github, and someone advised me to convert my ES6 code to ES5 using Babel. The problem is, I&ap ...

When we modify the prototype of the parent object, where does the __proto__ point to?

Typically, when a new object is created using the "new" keyword, the __proto__ property of the newly created object points to the prototype property of the parent class. This can be verified with the following code: function myfunc(){}; myfunc.prototype.n ...

What is the method for handling JSON data in Node.JS?

I am working with a Node.JS file that includes the following code: var express = require('express'); var app = express(); var http = require('http').Server(app); var cfenv = require("cfenv"); var appEnv = cfenv.getAppEnv(); http.list ...

Tips for sharing data between React components without causing re-renders or relying on JSX, such as through a function within functional components

As a beginner in react, I have been struggling to find answers to this issue. I am trying to figure out how to pass data from one functional component to another in react without actually rendering the child component. Does anyone know a solution for this ...

Filtering Images by Alt Attribute: A Comprehensive Guide

I'm working on an image gallery project that includes a search box feature. My goal is to dynamically hide images that do not have an alt attribute matching the user's search query. Despite several attempts, I keep encountering issues where all e ...

The functionality of event.preventDefault is not working as expected

Using AJAX, I successfully loaded data into a div. <div id="container"> <a class="hello" href="abc.php">hello</a> // content loaded via AJAX <div> Currently, I am trying to implement jQuery: <script type="text/javascript" ch ...

Shadows in THREE.JS are not being affected by the displacement map

Lately, I've been facing some challenges with getting the shadows to work properly when using a displacement map. Despite researching the issue on Google, I still can't seem to figure it out. From what I gather, the displacement map is supposed t ...