Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,32 @@ Still on **React Query v4**? No problem! Check out the v4 docs here: https://tan
- [React Suspense](https://react.dev/reference/react/Suspense) + Fetch-As-You-Render Query Prefetching
- Dedicated Devtools

## Partners

<a href="https://www.speakeasy.com/product/react-query?utm_source=tanstack&utm_campaign=tanstack">
<picture>
<source
srcset="https://tanstack.com/_build/assets/speakeasy-dark-BjP-Hd9M.svg"
media="(prefers-color-scheme: dark)"
/>
<source
srcset="https://tanstack.com/_build/assets/speakeasy-light-UpY7QmwQ.svg"
media="(prefers-color-scheme: light)"
/>
<!-- fallback -->
<img
src="https://tanstack.com/_build/assets/speakeasy-light-UpY7QmwQ.svg"
alt="Speakeasy Logo"
/>
</picture>
</a>

## Contributing

View the contributing guidelines [here](/CONTRIBUTING.md)

### [Become a Sponsor!](https://github.com/sponsors/tannerlinsley/)

<!-- Use the force, Luke -->

[
12 changes: 6 additions & 6 deletions examples/react/suspense/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ function Example() {
)}
onReset={reset}
>
<React.Suspense fallback={<h1>Loading projects...</h1>}>
{showProjects ? (
activeProject ? (
{showProjects ? (
<React.Suspense fallback={<h1>Loading projects...</h1>}>
{activeProject ? (
<Project
activeProject={activeProject}
setActiveProject={setActiveProject}
/>
) : (
<Projects setActiveProject={setActiveProject} />
)
) : null}
</React.Suspense>
)}
</React.Suspense>
) : null}
</ErrorBoundary>
)}
</QueryErrorResetBoundary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { TestBed } from '@angular/core/testing'
import { describe, expect, test } from 'vitest'
import {
InjectionToken,
provideExperimentalZonelessChangeDetection,
} from '@angular/core'
import { QueryClient } from '@tanstack/query-core'
import { provideQueryClient } from '../providers'

describe('provideQueryClient', () => {
test('should provide a QueryClient instance directly', () => {
const queryClient = new QueryClient()

TestBed.configureTestingModule({
providers: [
provideExperimentalZonelessChangeDetection(),
provideQueryClient(queryClient),
],
})

const providedQueryClient = TestBed.inject(QueryClient)
expect(providedQueryClient).toBe(queryClient)
})

test('should provide a QueryClient instance using an InjectionToken', () => {
const queryClient = new QueryClient()
const CUSTOM_QUERY_CLIENT = new InjectionToken<QueryClient>('', {
factory: () => queryClient,
})

TestBed.configureTestingModule({
providers: [
provideExperimentalZonelessChangeDetection(),
provideQueryClient(CUSTOM_QUERY_CLIENT),
],
})

const providedQueryClient = TestBed.inject(QueryClient)
expect(providedQueryClient).toBe(queryClient)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { TestBed } from '@angular/core/testing'
import { describe, expect, test } from 'vitest'
import {
InjectionToken,
provideExperimentalZonelessChangeDetection,
} from '@angular/core'
import { QueryClient } from '@tanstack/query-core'
import { provideTanStackQuery } from '../providers'

describe('provideTanStackQuery', () => {
test('should provide a QueryClient instance directly', () => {
const queryClient = new QueryClient()

TestBed.configureTestingModule({
providers: [
provideExperimentalZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})

const providedQueryClient = TestBed.inject(QueryClient)
expect(providedQueryClient).toBe(queryClient)
})

test('should provide a QueryClient instance using an InjectionToken', () => {
const queryClient = new QueryClient()
const CUSTOM_QUERY_CLIENT = new InjectionToken<QueryClient>('', {
factory: () => queryClient,
})

TestBed.configureTestingModule({
providers: [
provideExperimentalZonelessChangeDetection(),
provideTanStackQuery(CUSTOM_QUERY_CLIENT),
],
})

const providedQueryClient = TestBed.inject(QueryClient)
expect(providedQueryClient).toBe(queryClient)
})
})
35 changes: 25 additions & 10 deletions packages/angular-query-experimental/src/providers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
DestroyRef,
ENVIRONMENT_INITIALIZER,
InjectionToken,
PLATFORM_ID,
computed,
effect,
Expand All @@ -25,10 +26,19 @@ import type {
* for the entire application. You can use `provideQueryClient` to provide a
* different `QueryClient` instance for a part of the application.
* @param queryClient - the `QueryClient` instance to provide.
* @public
* @returns a provider object that can be used to provide the `QueryClient` instance.
*/
export function provideQueryClient(queryClient: QueryClient) {
return { provide: QueryClient, useValue: queryClient }
export function provideQueryClient(
queryClient: QueryClient | InjectionToken<QueryClient>,
): Provider {
return {
provide: QueryClient,
useFactory: () => {
return queryClient instanceof InjectionToken
? inject(queryClient)
: queryClient
},
}
}

/**
Expand Down Expand Up @@ -83,15 +93,14 @@ export function provideQueryClient(queryClient: QueryClient) {
* }
* )
* ```
* @param queryClient - A `QueryClient` instance.
* @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
* @param features - Optional features to configure additional Query functionality.
* @returns A set of providers to set up TanStack Query.
* @public
* @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
* @see withDevtools
*/
export function provideTanStackQuery(
queryClient: QueryClient,
queryClient: QueryClient | InjectionToken<QueryClient>,
...features: Array<QueryFeatures>
): EnvironmentProviders {
return makeEnvironmentProviders([
Expand All @@ -100,10 +109,16 @@ export function provideTanStackQuery(
// Do not use provideEnvironmentInitializer while Angular < v19 is supported
provide: ENVIRONMENT_INITIALIZER,
multi: true,
useValue: () => {
queryClient.mount()
// Unmount the query client on application destroy
inject(DestroyRef).onDestroy(() => queryClient.unmount())
useFactory: () => {
const client =
queryClient instanceof InjectionToken
? inject(queryClient)
: queryClient
return () => {
client.mount()
// Unmount the query client on application destroy
inject(DestroyRef).onDestroy(() => client.unmount())
}
},
},
features.map((feature) => feature.ɵproviders),
Expand Down