Skip to content

Commit

Permalink
fix(angular-query): support inject(QueryClient)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoud-dv committed Nov 14, 2024
1 parent 7615661 commit 347148c
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 298 deletions.
4 changes: 2 additions & 2 deletions docs/framework/angular/guides/invalidations-from-mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class TodoItemComponent {
[//]: # 'Example2'

```ts
import { injectMutation } from '@tanstack/angular-query-experimental'
import { injectMutation, QueryClient } from '@tanstack/angular-query-experimental'

export class TodosComponent {
queryClient = injectQueryClient()
queryClient = inject(QueryClient)

// When this mutation succeeds, invalidate any queries with the `todos` or `reminders` query key
mutation = injectMutation((client) => ({
Expand Down
2 changes: 1 addition & 1 deletion docs/framework/angular/guides/paginated-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const result = injectQuery(() => ({
})
export class PaginationExampleComponent {
page = signal(0)
#queryClient = injectQueryClient()
queryClient = inject(QueryClient)
query = injectQuery(() => ({
queryKey: ['projects', this.page()],
Expand Down
2 changes: 1 addition & 1 deletion docs/framework/angular/guides/placeholder-query-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class BlogPostComponent {
this.postId.set(value)
}
postId = signal(0)
queryClient = injectQueryClient()
queryClient = inject(QueryClient)

result = injectQuery(() => ({
queryKey: ['blogPost', this.postId()],
Expand Down
2 changes: 1 addition & 1 deletion docs/framework/angular/guides/query-cancellation.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class TodosComponent {
},
}))
queryClient = injectQueryClient()
queryClient = inject(QueryClient)
onCancel() {
this.queryClient.cancelQueries(['todos'])
Expand Down
4 changes: 2 additions & 2 deletions docs/framework/angular/guides/query-invalidation.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ replace: { 'useQuery': 'injectQuery', 'hooks': 'functions' }
```ts
import {
injectQuery,
injectQueryClient,
QueryClient,
} from '@tanstack/angular-query-experimental'

class QueryInvalidationExample {
queryClient = injectQueryClient()
queryClient = inject(QueryClient)

invalidateQueries() {
this.queryClient.invalidateQueries({ queryKey: ['todos'] })
Expand Down
3 changes: 1 addition & 2 deletions docs/framework/angular/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import { lastValueFrom } from 'rxjs'
import {
injectMutation,
injectQuery,
injectQueryClient,
} from '@tanstack/angular-query-experimental'
@Component({
Expand All @@ -70,7 +69,7 @@ import {
})
export class TodosComponent {
todoService = inject(TodoService)
queryClient = injectQueryClient()
queryClient = inject(QueryClient)
query = injectQuery(() => ({
queryKey: ['todos'],
Expand Down
7 changes: 2 additions & 5 deletions examples/angular/basic/src/app/components/post.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import {
inject,
input,
} from '@angular/core'
import {
injectQuery,
injectQueryClient,
} from '@tanstack/angular-query-experimental'
import { QueryClient, injectQuery } from '@tanstack/angular-query-experimental'
import { fromEvent, lastValueFrom, takeUntil } from 'rxjs'
import { PostsService } from '../services/posts-service'

Expand Down Expand Up @@ -38,5 +35,5 @@ export class PostComponent {
},
}))

queryClient = injectQueryClient()
queryClient = inject(QueryClient)
}
8 changes: 2 additions & 6 deletions examples/angular/basic/src/app/components/posts.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import {
Output,
inject,
} from '@angular/core'
import {
injectQuery,
injectQueryClient,
} from '@tanstack/angular-query-experimental'
import { QueryClient , injectQuery } from '@tanstack/angular-query-experimental'
import { lastValueFrom } from 'rxjs'
import { PostsService } from '../services/posts-service'

Expand All @@ -19,6 +16,7 @@ import { PostsService } from '../services/posts-service'
templateUrl: './posts.component.html',
})
export class PostsComponent {
queryClient = inject(QueryClient)
#postsService = inject(PostsService)

@Output() setPostId = new EventEmitter<number>()
Expand All @@ -27,6 +25,4 @@ export class PostsComponent {
queryKey: ['posts'],
queryFn: () => lastValueFrom(this.#postsService.allPosts$()),
}))

queryClient = injectQueryClient()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
untracked,
} from '@angular/core'
import {
QueryClient,
injectQuery,
injectQueryClient,
keepPreviousData,
} from '@tanstack/angular-query-experimental'
import { lastValueFrom } from 'rxjs'
Expand All @@ -21,7 +21,7 @@ import { ProjectsService } from '../services/projects.service'
templateUrl: './example.component.html',
})
export class ExampleComponent {
queryClient = injectQueryClient()
queryClient = inject(QueryClient)
projectsService = inject(ProjectsService)
page = signal(0)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
import { RouterLink } from '@angular/router'
import {
injectQuery,
injectQueryClient,
} from '@tanstack/angular-query-experimental'
import { QueryClient, injectQuery } from '@tanstack/angular-query-experimental'
import { QueriesService } from '../services/queries-service'

@Component({
Expand All @@ -17,5 +14,5 @@ export default class PostsComponent {
private queries = inject(QueriesService)

postsQuery = injectQuery(() => this.queries.posts())
queryClient = injectQueryClient()
queryClient = inject(QueryClient)
}
4 changes: 2 additions & 2 deletions examples/angular/router/src/app/components/posts.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
import { RouterLink } from '@angular/router'
import {
QueryClient,
injectQuery,
injectQueryClient,
} from '@tanstack/angular-query-experimental'
import { lastValueFrom } from 'rxjs'
import { PostsService } from '../services/posts-service'
Expand All @@ -22,5 +22,5 @@ export default class PostsComponent {
queryFn: () => lastValueFrom(this.#postsService.allPosts$()),
}))

queryClient = injectQueryClient()
queryClient = inject(QueryClient)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import {
} from '@angular/core'
import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
import {
injectQueryClient,
QueryClient,
onlineManager,
} from '@tanstack/angular-query-experimental'
import { isPlatformBrowser } from '@angular/common'
import type { QueryClient } from '@tanstack/angular-query-experimental'
import type { ElementRef } from '@angular/core'
import type { DevtoolsErrorType } from '@tanstack/query-devtools'

Expand Down Expand Up @@ -57,10 +56,7 @@ export function injectDevtoolsPanel(
const destroyRef = inject(DestroyRef)

effect(() => {
const injectedClient = injectQueryClient({
optional: true,
injector: currentInjector,
})
const injectedClient = currentInjector.get(QueryClient, null)
const {
client = injectedClient,
errorTypes = [],
Expand Down
6 changes: 2 additions & 4 deletions packages/angular-query-experimental/src/create-base-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import {
signal,
untracked,
} from '@angular/core'
import { notifyManager } from '@tanstack/query-core'
import { QueryClient, notifyManager } from '@tanstack/query-core'
import { signalProxy } from './signal-proxy'
import { shouldThrowError } from './util'
import { lazyInit } from './util/lazy-init/lazy-init'
import { injectQueryClient } from './inject-query-client'
import type {
QueryClient,
QueryKey,
QueryObserver,
QueryObserverResult,
Expand Down Expand Up @@ -47,7 +45,7 @@ export function createBaseQuery<
return lazyInit(() => {
const ngZone = injector.get(NgZone)
const destroyRef = injector.get(DestroyRef)
const queryClient = injectQueryClient({ injector })
const queryClient = injector.get(QueryClient)

/**
* Signal that has the default options from query client applied
Expand Down
5 changes: 2 additions & 3 deletions packages/angular-query-experimental/src/inject-is-fetching.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DestroyRef, NgZone, inject, signal } from '@angular/core'
import { notifyManager } from '@tanstack/query-core'
import { QueryClient, notifyManager } from '@tanstack/query-core'
import { assertInjector } from './util/assert-injector/assert-injector'
import { injectQueryClient } from './inject-query-client'
import type { QueryFilters } from '@tanstack/query-core'
import type { Injector, Signal } from '@angular/core'

Expand All @@ -20,9 +19,9 @@ export function injectIsFetching(
injector?: Injector,
): Signal<number> {
return assertInjector(injectIsFetching, injector, () => {
const queryClient = injectQueryClient()
const destroyRef = inject(DestroyRef)
const ngZone = inject(NgZone)
const queryClient = inject(QueryClient)

const cache = queryClient.getQueryCache()
// isFetching is the prev value initialized on mount *
Expand Down
5 changes: 2 additions & 3 deletions packages/angular-query-experimental/src/inject-is-mutating.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DestroyRef, NgZone, inject, signal } from '@angular/core'
import { notifyManager } from '@tanstack/query-core'
import { QueryClient, notifyManager } from '@tanstack/query-core'
import { assertInjector } from './util/assert-injector/assert-injector'
import { injectQueryClient } from './inject-query-client'
import type { MutationFilters } from '@tanstack/query-core'
import type { Injector, Signal } from '@angular/core'

Expand All @@ -19,9 +18,9 @@ export function injectIsMutating(
injector?: Injector,
): Signal<number> {
return assertInjector(injectIsMutating, injector, () => {
const queryClient = injectQueryClient()
const destroyRef = inject(DestroyRef)
const ngZone = inject(NgZone)
const queryClient = inject(QueryClient)

const cache = queryClient.getMutationCache()
// isMutating is the prev value initialized on mount *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import {
signal,
untracked,
} from '@angular/core'
import { notifyManager, replaceEqualDeep } from '@tanstack/query-core'
import {
QueryClient,
notifyManager,
replaceEqualDeep,
} from '@tanstack/query-core'
import { assertInjector } from './util/assert-injector/assert-injector'
import { injectQueryClient } from './inject-query-client'
import { lazySignalInitializer } from './util/lazy-signal-initializer/lazy-signal-initializer'
import type { Injector, Signal } from '@angular/core'
import type {
Expand Down Expand Up @@ -55,8 +58,8 @@ export function injectMutationState<TResult = MutationState>(
): Signal<Array<TResult>> {
return assertInjector(injectMutationState, options?.injector, () => {
const destroyRef = inject(DestroyRef)
const queryClient = injectQueryClient()
const ngZone = inject(NgZone)
const queryClient = inject(QueryClient)

const mutationCache = queryClient.getMutationCache()

Expand Down
15 changes: 7 additions & 8 deletions packages/angular-query-experimental/src/inject-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import {
runInInjectionContext,
signal,
} from '@angular/core'
import { MutationObserver, notifyManager } from '@tanstack/query-core'
import {
MutationObserver,
QueryClient,
notifyManager,
} from '@tanstack/query-core'
import { assertInjector } from './util/assert-injector/assert-injector'
import { signalProxy } from './signal-proxy'
import { injectQueryClient } from './inject-query-client'
import { noop, shouldThrowError } from './util'

import { lazyInit } from './util/lazy-init/lazy-init'
import type {
DefaultError,
MutationObserverResult,
QueryClient,
} from '@tanstack/query-core'
import type { DefaultError, MutationObserverResult } from '@tanstack/query-core'
import type {
CreateMutateFunction,
CreateMutationOptions,
Expand Down Expand Up @@ -47,10 +46,10 @@ export function injectMutation<
injector?: Injector,
): CreateMutationResult<TData, TError, TVariables, TContext> {
return assertInjector(injectMutation, injector, () => {
const queryClient = injectQueryClient()
const currentInjector = inject(Injector)
const destroyRef = inject(DestroyRef)
const ngZone = inject(NgZone)
const queryClient = inject(QueryClient)

return lazyInit(() =>
runInInjectionContext(currentInjector, () => {
Expand Down
9 changes: 6 additions & 3 deletions packages/angular-query-experimental/src/inject-queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { QueriesObserver, notifyManager } from '@tanstack/query-core'
import {
QueriesObserver,
QueryClient,
notifyManager,
} from '@tanstack/query-core'
import { DestroyRef, computed, effect, inject, signal } from '@angular/core'
import { assertInjector } from './util/assert-injector/assert-injector'
import { injectQueryClient } from './inject-query-client'
import type { Injector, Signal } from '@angular/core'
import type {
DefaultError,
Expand Down Expand Up @@ -199,7 +202,7 @@ export function injectQueries<
injector?: Injector,
): Signal<TCombinedResult> {
return assertInjector(injectQueries, injector, () => {
const queryClient = injectQueryClient()
const queryClient = inject(QueryClient)
const destroyRef = inject(DestroyRef)

const defaultedQueries = computed(() => {
Expand Down
29 changes: 12 additions & 17 deletions packages/angular-query-experimental/src/inject-query-client.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import { createNoopInjectionToken } from './util/create-injection-token/create-injection-token'
import type { QueryClient } from '@tanstack/query-core'

const tokens = createNoopInjectionToken<QueryClient>('QueryClientToken')
import { Injector, inject } from '@angular/core'
import { QueryClient } from '@tanstack/query-core'
import type { InjectOptions } from '@angular/core'

/**
* Injects the `QueryClient` instance into the component or service.
* Injects a `QueryClient` instance and allows passing a custom injector.
*
* You can also use `inject(QueryClient)` if you don't need to provide a custom injector.
*
* **Example**
* ```ts
* const queryClient = injectQueryClient();
* ```
* @param injectOptions - Type of the options argument to inject and optionally a custom injector.
* @returns The `QueryClient` instance.
* @public
*/
export const injectQueryClient = tokens[0]

/**
* Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the
* {@link https://tanstack.com/query/latest/docs/reference/QueryClient|QueryClient}
* for the entire application. You can use `provideQueryClient` to provide a
* different `QueryClient` instance for a part of the application.
* @public
*/
export const provideQueryClient = tokens[1]

export const QUERY_CLIENT = tokens[2]
export function injectQueryClient(
injectOptions: InjectOptions & { injector?: Injector } = {},
) {
return (injectOptions.injector ?? inject(Injector)).get(QueryClient)
}
Loading

0 comments on commit 347148c

Please sign in to comment.