Skip to content

Commit 5b6449e

Browse files
JoviDeCroockkitten
andauthored
feat(svelte): add first class reexecute to svelte (#3472)
Co-authored-by: Phil Pluckthun <phil@kitten.sh>
1 parent 3df80f3 commit 5b6449e

4 files changed

Lines changed: 48 additions & 19 deletions

File tree

.changeset/seven-toys-applaud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@urql/svelte': minor
3+
---
4+
5+
Add back the `reexecute` function

docs/basics/svelte.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,7 @@ Sometimes we'll need to arbitrarly reexecute a query to check for new data on th
362362
});
363363

364364
function refresh() {
365-
queryStore({
366-
client,
367-
query,
365+
todos.reexecute({
368366
requestPolicy: 'network-only'
369367
});
370368
}

examples/with-svelte/src/PokemonList.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
function nextPage() {
2020
skip = skip + 10;
2121
}
22+
23+
function reexcute() {
24+
pokemons.reexecute({ requestPolicy: 'network-only' })
25+
}
2226
</script>
2327

2428
<div>
@@ -34,4 +38,5 @@
3438
</ul>
3539
{/if}
3640
<button on:click={nextPage}>Next Page</button>
41+
<button on:click={reexcute}>Reexec</button>
3742
</div>

packages/svelte-urql/src/queryStore.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ export function queryStore<
119119
Variables extends AnyVariables = AnyVariables,
120120
>(
121121
args: QueryArgs<Data, Variables>
122-
): OperationResultStore<Data, Variables> & Pausable {
122+
): OperationResultStore<Data, Variables> &
123+
Pausable & { reexecute: (context: Partial<OperationContext>) => void } {
123124
const request = createRequest(args.query, args.variables as Variables);
124125

125126
const context: Partial<OperationContext> = {
@@ -132,6 +133,9 @@ export function queryStore<
132133
request,
133134
context
134135
);
136+
137+
const operation$ = writable(operation);
138+
135139
const initialState: OperationResultState<Data, Variables> = {
136140
...initialResult,
137141
operation,
@@ -148,21 +152,26 @@ export function queryStore<
148152
return never as any;
149153
}
150154

151-
return concat<Partial<OperationResultState<Data, Variables>>>([
152-
fromValue({ fetching: true, stale: false }),
153-
pipe(
154-
args.client.executeRequestOperation(operation),
155-
map(({ stale, data, error, extensions, operation }) => ({
156-
fetching: false,
157-
stale: !!stale,
158-
data,
159-
error,
160-
operation,
161-
extensions,
162-
}))
163-
),
164-
fromValue({ fetching: false }),
165-
]);
155+
return pipe(
156+
fromStore(operation$),
157+
switchMap(operation => {
158+
return concat<Partial<OperationResultState<Data, Variables>>>([
159+
fromValue({ fetching: true, stale: false }),
160+
pipe(
161+
args.client.executeRequestOperation(operation),
162+
map(({ stale, data, error, extensions, operation }) => ({
163+
fetching: false,
164+
stale: !!stale,
165+
data,
166+
error,
167+
operation,
168+
extensions,
169+
}))
170+
),
171+
fromValue({ fetching: false }),
172+
]);
173+
})
174+
);
166175
}
167176
),
168177
scan(
@@ -178,10 +187,22 @@ export function queryStore<
178187
).unsubscribe;
179188
});
180189

190+
const reexecute = (context: Partial<OperationContext>) => {
191+
const newContext = { ...context, ...args.context };
192+
const operation = args.client.createRequestOperation(
193+
'query',
194+
request,
195+
newContext
196+
);
197+
isPaused$.set(false);
198+
operation$.set(operation);
199+
};
200+
181201
return {
182202
...derived(result$, (result, set) => {
183203
set(result);
184204
}),
185205
...createPausable(isPaused$),
206+
reexecute,
186207
};
187208
}

0 commit comments

Comments
 (0)