Skip to content

Commit

Permalink
docs(query): 📘 fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal committed Jan 20, 2024
1 parent 7c82603 commit 3507966
Showing 1 changed file with 3 additions and 43 deletions.
46 changes: 3 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,49 +58,6 @@ export class TodosService {
}
```

For methods that require a `queryFn` parameter like
[ensureQueryData](https://tanstack.com/query/latest/docs/react/reference/QueryClient#queryclientensurequerydata), [fetchQuery](https://tanstack.com/query/latest/docs/react/reference/QueryClient#queryclientfetchquery), [prefetchQuery](), [fetchInfiniteQuery]() and [prefetchInfiniteQuery]() it's possible to use both Promises and Observables.

```ts
import { injectQueryClient } from '@ngneat/query';

@Injectable({ providedIn: 'root' })
export class TodosService {
#queryClient = injectQueryClient();

getQueryObservable() {
return {
queryKey: ['todos'] as const,
queryFn: () => {
return this.http.get<Todo[]>(
'https://jsonplaceholder.typicode.com/todos',
);
}
};
}

getQueryPromise() {
return {
queryKey: ['todos'] as const,
queryFn: () => {
return lastValueFrom(this.http.get<Todo[]>(
'https://jsonplaceholder.typicode.com/todos',
));
}
};
}

ensureQueryDataFromPromise(): Promise<Todo[]> {
return this.#queryClient.ensureQueryData(this.getQueryFromPromise());
}

ensureQueryDataFromObservable(): Promise<Todo[]> {
return this.#queryClient.ensureQueryData(this.getQueryFromObservable());
}
}
```


> The function should run inside an injection context
### Query
Expand Down Expand Up @@ -130,6 +87,9 @@ export class TodosService {

> The function should run inside an injection context
For methods that require a `queryFn` parameter like
`ensureQueryData`, `fetchQuery`, `prefetchQuery`, `fetchInfiniteQuery` and `prefetchInfiniteQuery` it's possible to use both Promises and Observables. See an example [here](https://github.com/ngneat/query/blob/main/src/app/prefetch-page/resolve.ts#L9).

#### Component Usage - Observable

To get an observable use the `result$` property:
Expand Down

0 comments on commit 3507966

Please sign in to comment.