question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle dynamic variables in useQuery?

See original GitHub issue

Started fiddeling with this package and it looks really awsome. I am trying to understand how to automatically refetch when variables changes in a useQuery hook. I don’t know if i misunderstood something but this is what I have tried, and it only fetches once.`

On first render, it fetches correctly and logs correct data and variables. When setVariables it runs it updates those correctly, but no re-render is done and it logs new variables but same old data. Am i missing something here? There is no example for this.

function foo() {
  const [variables, setVariables] = useState({});
  const { data, loading } = useQuery(SOME_GRAPHQL_SCHEMA, {
    variables,
    suspend: false,
  });

  if(loading) return <p>Loading</p>;
  console.log(data, variables);
  return (...);
}
```

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:9
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
trojanowskicommented, Feb 15, 2019

@rsjolundchas the first example should work. Please look at https://codesandbox.io/s/n4o02oz6jm - you should see different images for cats and dogs.

3reactions
EricGrudziencommented, Oct 4, 2019

Hi,

Encountered this too. Issue of setting the value of title did not cause a re-query of the API.

Here is the way I was able to resolve.

Before (not work):

const BOOKS_QUERY = gql`
  {
    books {
      author
      title
    }
  }
`;

// note, the below lines of code are in the React function
const [title, setTitle] = useState("");

const { loading, error, data } = useQuery(BOOKS_QUERY, {
    variables: { title }
  });

Resolution (works!) - modified the query:

const BOOKS_QUERY = gql`
  query getBooks($title: String) {
    books(title: $title) {
      author
      title
    }
  }
`;

And then was able to achieve the desired outcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The useQuery hook - with variables | Lift-off III - Apollo GraphQL
Use the useQuery hook to send the GET_SPACECAT query to the server. It takes a spaceCatId as a variable. Destructure the loading ,...
Read more >
Make a GraphQL Query Dynamic with Variables and Urqls ...
The first thing we need to do is update our coursesQuery to accept a variable. We will declare an $offset: Int variable in...
Read more >
react-query useQuery inside a provider with with dynamic ...
1 Answer 1 · I get that but what if I have a getTemplates call at the parent level. And I want all...
Read more >
Getting the Most Out of Apollo's useQuery - Medium
So first you setup the query by passing query name and variables using useLazyQuery . It is different from useQuery because on top...
Read more >
useQuery | TanStack Query Docs
useQuery · Optional · If set to true , queries that are set to continuously refetch with a refetchInterval will continue to refetch...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found