-
I saw
I wonder if it is also possible with for example I want to do a search form, when I click on search it should "refetch" the query with the form data. So in the submit event I want to call "refetch" and "reset" my query with new data. I checked the example but i could not find any way to make the query run again:
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
In the form of your first snippet it is not possible to refetch, to achieve this, the base query would need to re-export the refetch and the other utility functions of the query-observer. In the current base-query.ts file you would need to add this line to make this work: ...
}).pipe(
shareReplay({
bufferSize: 1,
refCount: true,
}),
);
let cachedSignal: undefined | Signal<any>;
const isNodeInjector = injector && (injector as any)['_tNode'];
return {
result$,
refetch: queryObserver.refetch.bind(queryObserver), // <-- this line
// @experimental signal support
get result() {
!isNodeInjector &&
assertInInjectionContext(function queryResultSignal() {
// noop
});
if (!cachedSignal) {
cachedSignal = toSignal(this.result$, {
requireSync: true,
// R3Injector isn't good here because it will cause a leak
// We only need the NodeInjector as we want the subscription to be destroyed when the component is destroyed
// We check that it's a NodeInjector by checking if it has a _tNode property
// Otherwise we just pass undefined and it'll use the current injector
// and not the R3Injector that we pass in the service
injector: isNodeInjector ? injector : undefined,
});
}
return cachedSignal;
},
}; @NetanelBasal wouldnt it make sense to re-export all the utility functions that the observer has? Or atleast the really important ones? @jon9090 But to make it work without this |
Beta Was this translation helpful? Give feedback.
-
We initially exposed the observer in version 1, but after reviewing React's implementation, where they don't expose it, I decided to remove it from our setup. Now, when there's a need to refetch data, I utilize the client API instead. |
Beta Was this translation helpful? Give feedback.
-
Btw the result object has a |
Beta Was this translation helpful? Give feedback.
-
But in your case, I'd do something like this: form.valueChanges.pipe(
switchMap(value => {
return this.myService.fetch(value)
})
) |
Beta Was this translation helpful? Give feedback.
-
Regarding your mention of a refetch function in the result object, I'm a bit puzzled. In the context of the @ngneat/query library, I haven't been able to locate a refetch function. The screenshot you provided doesn't seem to show this function either. -- For instance, you suggested something like this:
However, I'm not sure how this fits into the overall structure, especially in relation to the Sure, it seems like Thank you both @NetanelBasal @luii |
Beta Was this translation helpful? Give feedback.
@jon9090 Please take a closer look on your screenshot from above, the type hint shows you that this is a Signal, thus you would need to invoke it first to receive the
refetch
function.