Dulara Malindu
Dulara Malindu

Reputation: 1637

apollo client refetching query not passing variables with react-hooks

I am using a query named PLANTS_QUERY I am using react hooks. and using use query hook like this

 const { loading, error, data, refetch } = useQuery(PLANTS_QUERY);

I call refetch function with some variables like this

refetch(
  { where: { name_contains: value }}
);

it refetches, but it doesn't pass variables to the query,

I console logged the results. when running through the playground it passes variables. but this function provided by hooks doesn't pass variables

this is my query

const PLANTS_QUERY = gql`
  query {
    plants{
      plant_name
      is_active
    }
  }
`;

Upvotes: 1

Views: 4423

Answers (2)

Dulara Malindu
Dulara Malindu

Reputation: 1637

although the backend supporting for accepting variables is not enough. changing my gql wrapped query to accepting variables fixed my issue.

const PLANTS_QUERY = gql`
  query PlantQuery($where:PlantWhereUniqueInput){
    plants(where:$where){
      plant_name
      is_active
    }
  }
`;

now backend is receiving the variables that I'm passing

Upvotes: 0

Isala Piyarisi
Isala Piyarisi

Reputation: 81

As far as I know if you are using useQuery you need to pass the variables to query as a parameter to useQuery hook and refetch just execute the original query, if you want change the variables when calling again same query try useLazyQuery it gives function that can be used to trigger a query with new variables.

Upvotes: 3

Related Questions