Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.

feat(svelte): add first class reexecute to svelte (#3472)

Co-authored-by: Phil Pluckthun <phil@kitten.sh>

Changed files
+48 -19
.changeset
docs
basics
examples
with-svelte
packages
svelte-urql
+5
.changeset/seven-toys-applaud.md
···
+
---
+
'@urql/svelte': minor
+
---
+
+
Add back the `reexecute` function
+1 -3
docs/basics/svelte.md
···
});
function refresh() {
-
queryStore({
-
client,
-
query,
+
todos.reexecute({
requestPolicy: 'network-only'
});
}
+5
examples/with-svelte/src/PokemonList.svelte
···
function nextPage() {
skip = skip + 10;
}
+
+
function reexcute() {
+
pokemons.reexecute({ requestPolicy: 'network-only' })
+
}
</script>
<div>
···
</ul>
{/if}
<button on:click={nextPage}>Next Page</button>
+
<button on:click={reexcute}>Reexec</button>
</div>
+37 -16
packages/svelte-urql/src/queryStore.ts
···
Variables extends AnyVariables = AnyVariables,
>(
args: QueryArgs<Data, Variables>
-
): OperationResultStore<Data, Variables> & Pausable {
+
): OperationResultStore<Data, Variables> &
+
Pausable & { reexecute: (context: Partial<OperationContext>) => void } {
const request = createRequest(args.query, args.variables as Variables);
const context: Partial<OperationContext> = {
···
request,
context
);
+
+
const operation$ = writable(operation);
+
const initialState: OperationResultState<Data, Variables> = {
...initialResult,
operation,
···
return never as any;
}
-
return concat<Partial<OperationResultState<Data, Variables>>>([
-
fromValue({ fetching: true, stale: false }),
-
pipe(
-
args.client.executeRequestOperation(operation),
-
map(({ stale, data, error, extensions, operation }) => ({
-
fetching: false,
-
stale: !!stale,
-
data,
-
error,
-
operation,
-
extensions,
-
}))
-
),
-
fromValue({ fetching: false }),
-
]);
+
return pipe(
+
fromStore(operation$),
+
switchMap(operation => {
+
return concat<Partial<OperationResultState<Data, Variables>>>([
+
fromValue({ fetching: true, stale: false }),
+
pipe(
+
args.client.executeRequestOperation(operation),
+
map(({ stale, data, error, extensions, operation }) => ({
+
fetching: false,
+
stale: !!stale,
+
data,
+
error,
+
operation,
+
extensions,
+
}))
+
),
+
fromValue({ fetching: false }),
+
]);
+
})
+
);
}
),
scan(
···
).unsubscribe;
});
+
const reexecute = (context: Partial<OperationContext>) => {
+
const newContext = { ...context, ...args.context };
+
const operation = args.client.createRequestOperation(
+
'query',
+
request,
+
newContext
+
);
+
isPaused$.set(false);
+
operation$.set(operation);
+
};
+
return {
...derived(result$, (result, set) => {
set(result);
}),
...createPausable(isPaused$),
+
reexecute,
};
}