Having trouble re-executing a query (Svelte) #1415
-
I'm trying to build a search form in which a query is "paused" and only should be sent on form submission. After looking through the docs Basics -> Svelte, I've come up with something like this: const QUERY = `
query {
data {
field
}
}
`;
const opStore = operationStore(
QUERY,
{ ...initialVariables },
{
requestPolicy: 'cache-first', // Since pause = true, this can be elided?
pause: true, // Seems to not officially be a part of the `OperationContext` object
}
);
query(opStore);
function submitForm() {
$opStore.context = {
requestPolicy: 'network-only',
}; // pause = false is implicit?
} As expected, the query is not run when the component mounts, because the query context has been updated. However, consequent updates do not trigger re-execution of the query. Also, re-assigning an updated copy of variables does not trigger re-execution either. It's not obvious to me whether or not the variables need to be re-assigned to signal an update within Svelte, or if they are bound by reference upon initialization of the `operationStore.
Since I am new to Svelte, I figure that I am misunderstanding some basic mechanics or concepts and not following the correct idioms and practices. What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Alright, just to get started. It would have been a little quicker not to start the thread with pausing, since you've written that that works and it very closely matches the docs 😅 anyway; I suppose, your first question is why the operation doesn't execute again when you reassign something. The answer is why would it? As long as nothing actually changes there's no need to rerun anything. In fact; you already understood that the request policy needs to change so that the cache is skipped. The solution here is that it seems that you want to run the query conditionally. On the one hand, I know that Svelte would support wrapping the Edit: sorry, I forgot to leave a link to the above API: https://formidable.com/open-source/urql/docs/basics/core/#one-off-queries-and-mutations |
Beta Was this translation helpful? Give feedback.
Alright, just to get started. It would have been a little quicker not to start the thread with pausing, since you've written that that works and it very closely matches the docs 😅 anyway;
I suppose, your first question is why the operation doesn't execute again when you reassign something. The answer is why would it? As long as nothing actually changes there's no need to rerun anything.
In fact; you already understood that the request policy needs to change so that the cache is skipped.
The solution here is that it seems that you want to run the query conditionally. On the one hand, I know that Svelte would support wrapping the
query(opStore)
line reactively. However, it may be much easie…