How to update query on route change using Vue and Urql

How to update query on route change using Vue and Urql


0

I’m trying to find a way to update my query when the route changes (same component). The query is being selected using route meta data. Between Vue router’s navigation guards, hooks, watching for changes, etc, and Urqls "useQuery," I can’t figure out how to structure it to give me new data when the route changes. I just want it to recognize that "myQuery" has a new value, and to apply that to the query and rerun it.

const route = useRoute();
const myQuery = route.meta.query;

const posts = await useQuery({
  query: gql`
    query posts {
      ${myQuery} {
        title
        description
        interests {
          title
        }
        createdAt
        image_url
        userId {
          username
        }
      }
    }
  `,
  pause: false
});
const data = posts.data;

3

  • 1

    See stackoverflow.com/questions/46402809/… . And the query is supposed to involve reactive variables in order to be able to update it, formidable.com/open-source/urql/docs/basics/vue/#variables

    – Estus Flask

    1 hour ago

  • @EstusFlask thanks. But can those variables provide the query name itself? Or just args that are passed to the query? I'm trying to use a variable for the query name because it's the only thing that changes on the client.

    – Quin

    1 hour ago

  • 1

    might want to set a watcher on the route – also: I think you would not await useQuery, just move on directly. Also, you don't use variables (instead, you re-build the query string) – that might be the root causse. (see formidable.com/open-source/urql/docs/basics/vue) as well

    – Sebastian Rothbucher

    32 mins ago



Load 4 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *