Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce broken in useQuery #1271

Closed
karladler opened this issue Oct 19, 2021 · 2 comments
Closed

Debounce broken in useQuery #1271

karladler opened this issue Oct 19, 2021 · 2 comments

Comments

@karladler
Copy link

karladler commented Oct 19, 2021

Describe the bug
Setting the debounce option in useQuery seems to have no affect and a query is executed for each variable change

To Reproduce
Steps to reproduce the behaviour:

  1. create any query with composition API using useQuery ...
  2. set debounce option to some seconds e.g. { debounce: 15000 }
  3. load page. GQL query is executed instantly, but should be executed after 15 seconds
  4. change any variable in query
  5. GQL query is again executed instantly, but should be executed after 15 seconds

Expected behaviour
GQL query is always executed instantly, but should be executed after 15 seconds

Versions
vue: 2.6.14
vue-apollo: 3.0.8
@apollo/client: 3.4.16
@vue/apollo-composable: 4.0.0-alpha.15
@vue/apollo-util: 4.0.0-alpha.15

Additional context
Also using graphql codegen, but I checked the generated code and an simple example without codegen too, which should suffice.

example

const { result } = useQuery(getEntitiesDoc, () => ({ uuid: uuidRef.value?.uuid }), { fetchPolicy: 'cache-and-network', debounce: 15000 });

codegen versions:
"@graphql-codegen/add": "^3.1.0",
"@graphql-codegen/cli": "^2.2.1",
"@graphql-codegen/named-operations-object": "^2.1.0",
"@graphql-codegen/typescript-operations": "^2.1.8",
"@graphql-codegen/typescript-vue-apollo": "^3.1.6"

@STARSCrazy
Copy link

STARSCrazy commented Jan 25, 2022

@karladler @Akryum
I have the same problem and have dug through it with the debugger. I found two ways to fix the problem. I have edited locally directly in my index.esm.js. For a proper fix, of course, useQuery.ts would have to be modified.

Variant 1 is certainly not in the sense of the inventors. Just comment out the isRestartDebounceSetup check.

function restart () {
    //if (!isRestartDebounceSetup) updateRestartFn()
    debouncedRestart()
  }

More correct seems an extension at the "watch" near the comment "Applying variables".

if (serialized !== currentVariablesSerialized) {
	if (currentOptions.value && ( currentOptions.value.throttle || currentOptions.value.debounce)) {
		updateRestartFn()
	}
	currentVariables = value
	restart()
}

I think the check can be improved or the updateRestartFn logic.

@ferm10n
Copy link

ferm10n commented Feb 14, 2022

Here's more info on why this bug happens:

This effectively means that options will only be applied on change, and not when initially calling useQuery()

@Akryum Akryum closed this as completed in 082acf9 May 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants