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

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

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-toys-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/svelte': minor
---

Add back the `reexecute` function
4 changes: 1 addition & 3 deletions docs/basics/svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,7 @@ Sometimes we'll need to arbitrarly reexecute a query to check for new data on th
});

function refresh() {
queryStore({
client,
query,
todos.reexecute({
requestPolicy: 'network-only'
});
}
Expand Down
2 changes: 1 addition & 1 deletion examples/with-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"version": "0.0.0",
"scripts": {
"start": "vite",
"start": "vite --force",
"build": "vite build",
"serve": "vite preview"
},
Expand Down
5 changes: 5 additions & 0 deletions examples/with-svelte/src/PokemonList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
function nextPage() {
skip = skip + 10;
}

function reexcute() {
pokemons.reexecute({ requestPolicy: 'network-only' })
}
</script>

<div>
Expand All @@ -34,4 +38,5 @@
</ul>
{/if}
<button on:click={nextPage}>Next Page</button>
<button on:click={reexcute}>Reexec</button>
</div>
40 changes: 30 additions & 10 deletions packages/svelte-urql/src/queryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ export function queryStore<
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> = {
Expand All @@ -132,6 +133,9 @@ export function queryStore<
request,
context
);

const operation$ = writable(operation);

const initialState: OperationResultState<Data, Variables> = {
...initialResult,
operation,
Expand All @@ -151,15 +155,20 @@ export function queryStore<
return concat<Partial<OperationResultState<Data, Variables>>>([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's invert this here so fetching: true applies, i.e. placing concat inside switchMap

fromValue({ fetching: true, stale: false }),
pipe(
args.client.executeRequestOperation(operation),
map(({ stale, data, error, extensions, operation }) => ({
fetching: false,
stale: !!stale,
data,
error,
operation,
extensions,
}))
fromStore(operation$),
switchMap(operation => {
return pipe(
args.client.executeRequestOperation(operation),
map(({ stale, data, error, extensions, operation }) => ({
fetching: false,
stale: !!stale,
data,
error,
operation,
extensions,
}))
);
})
),
fromValue({ fetching: false }),
]);
Expand All @@ -178,10 +187,21 @@ export function queryStore<
).unsubscribe;
});

const reexecute = (context: Partial<OperationContext>) => {
const newContext = { ...context, ...args.context };
const operation = args.client.createRequestOperation(
'query',
request,
newContext
);
operation$.set(operation);
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
};

return {
...derived(result$, (result, set) => {
set(result);
}),
...createPausable(isPaused$),
reexecute,
};
}
Loading