Is there a way to get results in other functions? A promise way? A subscribe way? #1254
-
I'm trying to understand how to get results in functions that I neither create nor control. REPL: https://9crg7.csb.app/#/todos Code example: https://codesandbox.io/s/urql-how-to-get-update-in-functions-9crg7?file=/List.svelte:266-816 let condition = null;
function updateConditionAndgetTodosHere() {
const unsubscribe = todos.subscribe((data) => {
condition = { first: 1 };
console.log("newData:", data);
if (data.stale == false && data.fetching == false && data.todos) {
console.log("data.todos:", data.todos);
// unsubscribe(); // how to unsubscribe after all?
}
});
}
$: todos = query(
operationStore(
TODOS_QUERY,
{ input: condition },
{ requestPolicy: "cache-and-network", pause: !condition }
)
); I this example I need to I can't figure out how to handle this. Screen gif: The only way I can get todos now is with const queryClient = getClient();
condition = { first: 1 };
const todos = await queryClient
.query(
TODOS_QUERY,
{ input: condition },
// { requestPolicy: "cache-and-network" } // this doesn't work and I can understand why, but it's not good for me, I need cache-and-network here!
{ requestPolicy: "network-only" }
)
.toPromise();
if (todos.data) {
// ok...
} but I need How to do this? Where am I wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
Yes, using the client imperatively in any binding is recommended if the result is used outside of the normal flow of the UI, i.e. outside of the framework anyway. I also before I get to a conclusion have to mention that overusing Any usage of an imperative method still goes through the usual flow of results, so suppose you have all three options of request policies availed, the usage of When you use However, there's another reason why it doesn't make sense. The intention of why you have an imperative call is likely because you want to use the result after it's available. Hence, a |
Beta Was this translation helpful? Give feedback.
-
Forgive me. In my REPL I would like to console.log() the cached todos and simultaneously fetch the new ones. How can this be done today? |
Beta Was this translation helpful? Give feedback.
Yes, using the client imperatively in any binding is recommended if the result is used outside of the normal flow of the UI, i.e. outside of the framework anyway.
I also before I get to a conclusion have to mention that overusing
cache-and-Network
often does more damage than what it's worth.Any usage of an imperative method still goes through the usual flow of results, so suppose you have all three options of request policies availed, the usage of
cache-and-network
doesn't make sense, because there's nothing to update. You will likely always want to usecache-first
ornetwork-only
imperatively.When you use
cache-and-network
imperatively with a promise it obviously won't work since it wi…