-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Angular persist query client experimental #8324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arnoud-dv
merged 57 commits into
TanStack:main
from
OmerGronich:angular-persist-query-client-experimental
Apr 13, 2025
Merged
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
f327a5d
feat(angular-query):
OmerGronich dd92bfc
feat(angular-query): created package for experimental persistence sup…
OmerGronich b8afd8a
test(persistQuery): add tests for withPersistQueryClient
OmerGronich 547ae9e
docs(angular-query): add basic persister example
OmerGronich 7130913
fix(angular-query): synced angular versions in new package
OmerGronich 5709184
fix(angular-query): commit before ng update
OmerGronich 9e6ef24
feat(angular-query): updated example to v18
OmerGronich 962cf32
fix(angular-query): fixed project name to basic-persister
OmerGronich 4d305a5
feat(angular-query): updated example basic-persister to v19
OmerGronich 630e462
feat(angular-query): included back the persister package after angula…
OmerGronich fed1caa
feat(angular-query): migrate to provideEnvironmentInitializer
OmerGronich 63831fb
fix(angular-query): fix eslint issues in inject-queries.ts
OmerGronich 059b620
Revert "feat(angular-query): migrate to provideEnvironmentInitializer"
OmerGronich 05848e2
fix(angular-query): only track the isRestoring in the effect
OmerGronich a0d3111
fix(angular-query): removed conditional _isRestoring
OmerGronich e2f579b
ci: apply automated fixes
autofix-ci[bot] 929b114
fix(angular-query): used effect onCleanup instead of destroy ref
OmerGronich 9dcdd1c
Update examples/angular/basic-persister/.devcontainer/devcontainer.json
OmerGronich f208029
Update examples/angular/basic-persister/package.json
OmerGronich 1d3c67a
Update examples/angular/basic-persister/src/index.html
OmerGronich db2b2a3
Update examples/angular/basic-persister/tsconfig.json
OmerGronich de5b14f
Update examples/angular/basic-persister/tsconfig.json
OmerGronich 03b9c02
Update examples/angular/basic-persister/tsconfig.json
OmerGronich 67d3f38
Update packages/angular-query-experimental/src/inject-is-restoring.ts
OmerGronich b30b21b
fix(angular-query): fixed lock file
OmerGronich c9ec26a
fix(angular-query): aligned package.json versions
OmerGronich b858927
added missing dep from basic-persister example
OmerGronich 325b2a7
Added a more complex example for persistence. This examples demonstra…
OmerGronich c908304
Merge remote-tracking branch 'upstream/main' into angular-persist-que…
OmerGronich 5da9025
fix(angular-query) bumped versions to 5.62.4
OmerGronich 36605e6
refactor(angular-query) refactored to implicit return
OmerGronich a5f7942
Merge remote-tracking branch 'upstream/main' into angular-persist-que…
OmerGronich 5d80309
fix(angular-query) bump angular persister package and examples version
OmerGronich ffa43a8
fix(angular-query) fixed eslint errors
OmerGronich 235bbab
refactor(angular-query) set initial `isRestoring` to true to match th…
OmerGronich 1f33c28
Revert "refactor(angular-query) set initial `isRestoring` to true to …
OmerGronich 23d6ee1
fix(angular-query) fixed formatting of JSDOC comment ruined by eslint
OmerGronich 2e8bfe9
Merge branch 'main' into angular-persist-query-client-experimental
OmerGronich ea4f8bb
Merge branch 'main' into angular-persist-query-client-experimental
OmerGronich 08b903c
Merge remote-tracking branch 'upstream/main' into angular-persist-que…
arnoud-dv 1df9802
update build configs
arnoud-dv 112d35b
Merge branch 'main' into angular-persist-query-client-experimental
arnoud-dv e8eb3cf
zoneless unit tests
arnoud-dv 4e042b7
replace deprecated injectQueryClient
arnoud-dv 43f3500
createBaseQuery is always run in injection context, remove inject(Inj…
arnoud-dv 3393d0c
remove api extractor config
arnoud-dv ddf477f
update example dependencies
arnoud-dv 9ac5fd3
comment out from publish script for now
arnoud-dv 102a951
Merge branch 'main' into angular-persist-query-client-experimental
OmerGronich 28cce6c
refactor(persist-query-client): simplify configuration by removing ar…
OmerGronich 2afedee
refactor(angular-query-persist-client): removed experimental from pac…
OmerGronich 12e9b00
fix example
arnoud-dv 957670c
Merge remote-tracking branch 'upstream/main' into angular-persist-que…
arnoud-dv 0e30dd9
fix outdated comment
arnoud-dv 118cd24
exclude from preview for now
arnoud-dv 14509a2
mark package as private instead
arnoud-dv 254a978
configure injectIsRestoring injector through options object
arnoud-dv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
docs(angular-query): add basic persister example
- Loading branch information
commit 547ae9e71211e6b8788fe0c166242c607eabd913
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
examples/angular/basic-persister/.devcontainer/devcontainer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "Node.js", | ||
"image": "mcr.microsoft.com/devcontainers/javascript-node:18" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @ts-check | ||
|
||
/** @type {import('eslint').Linter.Config} */ | ||
const config = {} | ||
|
||
module.exports = config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# TanStack Query Angular basic example | ||
|
||
To run this example: | ||
|
||
- `npm install` or `yarn` or `pnpm i` or `bun i` | ||
- `npm run start` or `yarn start` or `pnpm start` or `bun start` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
{ | ||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json", | ||
"version": 1, | ||
"cli": { | ||
"packageManager": "pnpm", | ||
"analytics": false, | ||
"cache": { | ||
"enabled": false | ||
} | ||
}, | ||
"newProjectRoot": "projects", | ||
"projects": { | ||
"basic": { | ||
"projectType": "application", | ||
"schematics": { | ||
"@schematics/angular:component": { | ||
"inlineTemplate": true, | ||
"inlineStyle": true, | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:class": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:directive": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:guard": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:interceptor": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:pipe": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:resolver": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:service": { | ||
"skipTests": true | ||
} | ||
}, | ||
"root": "", | ||
"sourceRoot": "src", | ||
"prefix": "app", | ||
"architect": { | ||
"build": { | ||
"builder": "@angular-devkit/build-angular:application", | ||
"options": { | ||
"outputPath": "dist/basic", | ||
"index": "src/index.html", | ||
"browser": "src/main.ts", | ||
"polyfills": ["zone.js"], | ||
"tsConfig": "tsconfig.app.json", | ||
"assets": ["src/favicon.ico", "src/assets"], | ||
"styles": [], | ||
"scripts": [] | ||
}, | ||
"configurations": { | ||
"production": { | ||
"budgets": [ | ||
{ | ||
"type": "initial", | ||
"maximumWarning": "500kb", | ||
"maximumError": "1mb" | ||
}, | ||
{ | ||
"type": "anyComponentStyle", | ||
"maximumWarning": "2kb", | ||
"maximumError": "4kb" | ||
} | ||
], | ||
"outputHashing": "all" | ||
}, | ||
"development": { | ||
"optimization": false, | ||
"extractLicenses": false, | ||
"sourceMap": true | ||
} | ||
}, | ||
"defaultConfiguration": "production" | ||
}, | ||
"serve": { | ||
"builder": "@angular-devkit/build-angular:dev-server", | ||
"configurations": { | ||
"production": { | ||
"buildTarget": "basic:build:production" | ||
}, | ||
"development": { | ||
"buildTarget": "basic:build:development" | ||
} | ||
}, | ||
"defaultConfiguration": "development" | ||
}, | ||
"extract-i18n": { | ||
"builder": "@angular-devkit/build-angular:extract-i18n", | ||
"options": { | ||
"buildTarget": "basic:build" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "@tanstack/query-example-angular-basic-persister", | ||
"type": "module", | ||
"scripts": { | ||
"ng": "ng", | ||
"start": "ng serve", | ||
"build": "ng build", | ||
"watch": "ng build --watch --configuration development" | ||
}, | ||
"private": true, | ||
"dependencies": { | ||
"@angular/common": "^17.3.12", | ||
"@angular/compiler": "^17.3.12", | ||
"@angular/core": "^17.3.12", | ||
"@angular/platform-browser": "^17.3.12", | ||
"@angular/platform-browser-dynamic": "^17.3.12", | ||
"@tanstack/angular-query-experimental": "^5.60.5", | ||
"@tanstack/angular-query-persist-client-experimental": "^5.60.5", | ||
"@tanstack/query-sync-storage-persister": "^5.60.6", | ||
"rxjs": "^7.8.1", | ||
"tslib": "^2.6.3", | ||
"zone.js": "^0.14.8" | ||
}, | ||
"devDependencies": { | ||
"@angular-devkit/build-angular": "^17.3.8", | ||
"@angular/cli": "^17.3.8", | ||
"@angular/compiler-cli": "^17.3.12", | ||
"typescript": "5.3.3" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/angular/basic-persister/src/app/app.component.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<p> | ||
Try to mock offline behavior with the button in the devtools. You can | ||
navigate around as long as there is already data in the cache. You'll get | ||
a refetch as soon as you go "online" again. | ||
</p> | ||
@if (postId() > -1) { | ||
<post [postId]="postId()" (setPostId)="postId.set($event)"></post> | ||
} @else { | ||
<posts (setPostId)="postId.set($event)" /> | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ChangeDetectionStrategy, Component, signal } from '@angular/core' | ||
import { PostComponent } from './components/post.component' | ||
import { PostsComponent } from './components/posts.component' | ||
|
||
@Component({ | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
selector: 'basic-example', | ||
standalone: true, | ||
templateUrl: './app.component.html', | ||
imports: [PostComponent, PostsComponent], | ||
}) | ||
export class BasicExampleComponent { | ||
postId = signal(-1) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { provideHttpClient, withFetch } from '@angular/common/http' | ||
import { | ||
QueryClient, | ||
provideTanStackQuery, | ||
withDevtools, | ||
} from '@tanstack/angular-query-experimental' | ||
import { withPersistQueryClient } from '@tanstack/angular-query-persist-client-experimental' | ||
import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister' | ||
import type { ApplicationConfig } from '@angular/core' | ||
|
||
const localStoragePersister = createSyncStoragePersister({ | ||
storage: window.localStorage, | ||
}) | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [ | ||
provideHttpClient(withFetch()), | ||
provideTanStackQuery( | ||
new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
staleTime: 1000 * 60, | ||
gcTime: 1000 * 60 * 60 * 24, // 24 hours | ||
}, | ||
}, | ||
}), | ||
withDevtools(), | ||
withPersistQueryClient([ | ||
OmerGronich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
persistOptions: { | ||
persister: localStoragePersister, | ||
}, | ||
}, | ||
]), | ||
), | ||
], | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/angular/basic-persister/src/app/components/post.component.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<div> | ||
<div> | ||
<a (click)="setPostId.emit(-1)" href="#"> Back </a> | ||
</div> | ||
@if (postQuery.isPending()) { | ||
Loading... | ||
} @else if (postQuery.isError()) { | ||
Error: {{ postQuery.error().message }} | ||
} | ||
@if (postQuery.data(); as post) { | ||
<h1>{{ post.title }}</h1> | ||
<div> | ||
<p>{{ post.body }}</p> | ||
</div> | ||
@if (postQuery.isFetching()) { | ||
Background Updating... | ||
} | ||
} | ||
</div> |
39 changes: 39 additions & 0 deletions
39
examples/angular/basic-persister/src/app/components/post.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
EventEmitter, | ||
Output, | ||
inject, | ||
input, | ||
} from '@angular/core' | ||
import { QueryClient, injectQuery } from '@tanstack/angular-query-experimental' | ||
import { fromEvent, lastValueFrom, takeUntil } from 'rxjs' | ||
import { PostsService } from '../services/posts-service' | ||
|
||
@Component({ | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
selector: 'post', | ||
standalone: true, | ||
templateUrl: './post.component.html', | ||
}) | ||
export class PostComponent { | ||
#postsService = inject(PostsService) | ||
|
||
@Output() setPostId = new EventEmitter<number>() | ||
|
||
postId = input(0) | ||
|
||
postQuery = injectQuery(() => ({ | ||
enabled: this.postId() > 0, | ||
queryKey: ['post', this.postId()], | ||
queryFn: async (context) => { | ||
// Cancels the request when component is destroyed before the request finishes | ||
const abort$ = fromEvent(context.signal, 'abort') | ||
return lastValueFrom( | ||
this.#postsService.postById$(this.postId()).pipe(takeUntil(abort$)), | ||
) | ||
}, | ||
})) | ||
|
||
queryClient = inject(QueryClient) | ||
} |
39 changes: 39 additions & 0 deletions
39
examples/angular/basic-persister/src/app/components/posts.component.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<div> | ||
<h1>Posts</h1> | ||
@switch (postsQuery.status()) { | ||
@case ('pending') { | ||
Loading... | ||
} | ||
@case ('error') { | ||
Error: {{ postsQuery.error()?.message }} | ||
} | ||
@default { | ||
<div class="todo-container"> | ||
@for (post of postsQuery.data(); track post.id) { | ||
<p> | ||
<!-- We can access the query data here to show bold links for--> | ||
<!-- ones that are cached--> | ||
<a | ||
href="#" | ||
(click)="setPostId.emit(post.id)" | ||
[style]=" | ||
queryClient.getQueryData(['post', post.id]) | ||
? { | ||
fontWeight: 'bold', | ||
color: 'green', | ||
} | ||
: {} | ||
" | ||
>{{ post.title }}</a | ||
> | ||
</p> | ||
} | ||
</div> | ||
} | ||
} | ||
<div> | ||
@if (postsQuery.isFetching()) { | ||
Background Updating... | ||
} | ||
</div> | ||
</div> |
28 changes: 28 additions & 0 deletions
28
examples/angular/basic-persister/src/app/components/posts.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
EventEmitter, | ||
Output, | ||
inject, | ||
} from '@angular/core' | ||
import { QueryClient, injectQuery } from '@tanstack/angular-query-experimental' | ||
import { lastValueFrom } from 'rxjs' | ||
import { PostsService } from '../services/posts-service' | ||
|
||
@Component({ | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
selector: 'posts', | ||
standalone: true, | ||
templateUrl: './posts.component.html', | ||
}) | ||
export class PostsComponent { | ||
queryClient = inject(QueryClient) | ||
#postsService = inject(PostsService) | ||
|
||
@Output() setPostId = new EventEmitter<number>() | ||
|
||
postsQuery = injectQuery(() => ({ | ||
queryKey: ['posts'], | ||
queryFn: () => lastValueFrom(this.#postsService.allPosts$()), | ||
})) | ||
} |
21 changes: 21 additions & 0 deletions
21
examples/angular/basic-persister/src/app/services/posts-service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { HttpClient } from '@angular/common/http' | ||
import { Injectable, inject } from '@angular/core' | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class PostsService { | ||
#http = inject(HttpClient) | ||
|
||
postById$ = (postId: number) => | ||
this.#http.get<Post>(`https://jsonplaceholder.typicode.com/posts/${postId}`) | ||
|
||
allPosts$ = () => | ||
this.#http.get<Array<Post>>('https://jsonplaceholder.typicode.com/posts') | ||
} | ||
|
||
export interface Post { | ||
id: number | ||
title: string | ||
body: string | ||
} |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>TanStack Query Angular basic example</title> | ||
OmerGronich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<base href="/" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="icon" type="image/x-icon" href="favicon.ico" /> | ||
</head> | ||
<body> | ||
<basic-example></basic-example> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { bootstrapApplication } from '@angular/platform-browser' | ||
import { appConfig } from './app/app.config' | ||
import { BasicExampleComponent } from './app/app.component' | ||
|
||
bootstrapApplication(BasicExampleComponent, appConfig).catch((err) => | ||
console.error(err), | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.