Skip to content

Commit d35da8f

Browse files
authored
fix(vue): variables lose reactivity (#3614)
1 parent ad29144 commit d35da8f

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

.changeset/curvy-pets-yawn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@urql/vue': patch
3+
---
4+
5+
Fix variables losing reactivity

packages/vue-urql/src/useQuery.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,38 @@ describe('useQuery', () => {
108108
);
109109
});
110110

111+
it('reacts to variables changing', async () => {
112+
const executeQuery = vi
113+
.spyOn(client, 'executeQuery')
114+
.mockImplementation(request => {
115+
return pipe(
116+
fromValue({ operation: request, data: { test: true } }),
117+
delay(1)
118+
) as any;
119+
});
120+
121+
const variables = {
122+
test: ref(1),
123+
};
124+
const query$ = useQuery({
125+
query: '{ test }',
126+
variables,
127+
});
128+
129+
await query$;
130+
131+
expect(executeQuery).toHaveBeenCalledTimes(1);
132+
133+
expect(query$.operation.value).toHaveProperty('variables.test', 1);
134+
135+
variables.test.value = 2;
136+
137+
await query$;
138+
139+
expect(executeQuery).toHaveBeenCalledTimes(2);
140+
expect(query$.operation.value).toHaveProperty('variables.test', 2);
141+
});
142+
111143
it('pauses query when asked to do so', async () => {
112144
const subject = makeSubject<any>();
113145
const executeQuery = vi

packages/vue-urql/src/useQuery.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable react-hooks/rules-of-hooks */
22

33
import type { Ref, WatchStopHandle } from 'vue';
4+
import { reactive } from 'vue';
45
import { isRef, ref, shallowRef, watch, watchEffect } from 'vue';
56

67
import type { Subscription, Source } from 'wonka';
@@ -241,10 +242,12 @@ export function useQuery<T = any, V extends AnyVariables = AnyVariables>(
241242
}
242243

243244
export function callUseQuery<T = any, V extends AnyVariables = AnyVariables>(
244-
args: UseQueryArgs<T, V>,
245+
_args: UseQueryArgs<T, V>,
245246
client: Ref<Client> = useClient(),
246247
stops: WatchStopHandle[] = []
247248
): UseQueryResponse<T, V> {
249+
const args = reactive(_args) as UseQueryArgs<T, V>;
250+
248251
const data: Ref<T | undefined> = ref();
249252
const stale: Ref<boolean> = ref(false);
250253
const fetching: Ref<boolean> = ref(false);

0 commit comments

Comments
 (0)