query icon indicating copy to clipboard operation
query copied to clipboard

[WIP] Angular injectQueries fixes

Open crutchcorn opened this issue 10 months ago • 6 comments

This PR:

  • Fixes the types of injectQueries
  • Add type tests for injectQueries
  • Adds initial tests for injectQueries runtime
  • Fixes runtime behavior of injectQueries

TODO:

  • [ ] Figure out where to add signalProxy

crutchcorn avatar Feb 22 '25 23:02 crutchcorn

View your CI Pipeline Execution ↗ for commit 26d7f36575dcc568a2c110d7b4497361a8f270f6

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 41s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 8s View ↗

☁️ Nx Cloud last updated this comment at 2025-09-18 17:42:21 UTC

nx-cloud[bot] avatar Feb 22 '25 23:02 nx-cloud[bot]

More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@8690
@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@8690
@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@8690
@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@8690
@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@8690
@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@8690
@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@8690
@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@8690
@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@8690
@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@8690
@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@8690
@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@8690
@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@8690
@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@8690
@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@8690
@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@8690
@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@8690
@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@8690
@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@8690
@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@8690

commit: 26d7f36

pkg-pr-new[bot] avatar Feb 22 '25 23:02 pkg-pr-new[bot]

Sizes for commit 26d7f36575dcc568a2c110d7b4497361a8f270f6:

Branch Bundle Size
Main
This PR

github-actions[bot] avatar Feb 22 '25 23:02 github-actions[bot]

Hey @crutchcorn! I just noticed you dropped a PR to fix injectQueries. I've been using a custom build of @tanstack/angular-query-experimental from this PR (https://github.com/TanStack/query/pull/8007) that has been working great for me in production, as I really needed injectQueries for my application.

I'm not sure if that MR can serve as an inspiration for your work, or possibly even avoid work altogether?

Timebutt avatar Feb 27 '25 10:02 Timebutt

Should be something like this to proxify the individual queries, but I'm getting errors. I will look into this more.

    const resultSignal = computed(() => {
      const subscriberResult = resultFromSubscriberSignal()
      const optimisticResult = optimisticCombinedResultSignal()
      return subscriberResult ?? optimisticResult
    })

    return computed(() => {
      const result = resultSignal()
      if (Array.isArray(result)) {
        return result.map((query) => signalProxy(query))
      }
      return result
    })

  }) as unknown as Signal<TCombinedResult>

arnoud-dv avatar Mar 07 '25 15:03 arnoud-dv

More todos:

  • [ ] Should be able to throw an error when throwOnError is true
  • [ ] Using map on result gives error TS2339: Property map does not exist on type TCombinedResult.
  • [ ] The computed with proxified queries needs to be improved so that new signals are not created for each query when the result changes.
  • [ ] Align signature with other functions: parameters injectQueriesFn and options
  • [ ] Persistence

arnoud-dv avatar Mar 08 '25 21:03 arnoud-dv

@arnoud-dv @crutchcorn Are you still working on the feature? Do you need some help?

tomalaforge avatar Sep 01 '25 15:09 tomalaforge

Yes, help with injectQueries is welcome Thomas. I don't have enough time at the moment.

arnoud-dv avatar Sep 01 '25 21:09 arnoud-dv

Walkthrough

Refactors injectQueries to accept a function-based options provider and updates public types (InjectQueriesOptions, QueriesOptions, QueriesResults). Implements reactive observer wiring with signals and signalProxy. Adds unit and d.ts tests for new typing and behavior. Updates Angular testing library dev dependency versions in two packages.

Changes

Cohort / File(s) Summary of changes
Dev testing deps
packages/angular-query-experimental/package.json, packages/angular-query-persist-client/package.json
Add/upgrade dev dependency @testing-library/angular to ^18.0.0.
API and reactive flow refactor
packages/angular-query-experimental/src/inject-queries.ts
Change injectQueries signature to accept optionsFn: () => InjectQueriesOptions; add/export InjectQueriesOptions; revise QueriesOptions and QueriesResults typing to per-query mapping; rework runtime to derive options, observer, and results via signals and signalProxy.
Type-level tests
packages/angular-query-experimental/src/__tests__/inject-queries.test-d.ts
New d.ts tests validating TData inference, overloads, skipToken handling, and dynamic query result typing for injectQueries.
Runtime tests
packages/angular-query-experimental/src/__tests__/inject-queries.test.ts
New integration test verifying two async queries sequence and signal-driven state progression using Angular Testing Library and QueryClient.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor C as Component
  participant IQ as injectQueries
  participant OF as optionsFn()
  participant QC as QueryClient
  participant OBS as observerSignal
  participant RS as resultSignal

  C->>IQ: call with optionsFn, injector?
  IQ->>OF: compute options (queries, combine)
  IQ->>QC: apply defaultQueryOptions
  IQ->>OBS: create reactive QueriesObserver
  OBS-->>IQ: optimistic results (signal)
  OBS-->>IQ: subscribe for updates (untracked)
  IQ->>RS: merge subscriber/optimistic results
  RS-->>C: Signal<TCombinedResult> (proxied per-query)

  note over IQ,OBS: Each query result wrapped via signalProxy

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related issues

  • TanStack/query#8703 — Tracks the injectQueries refactor and API/type changes implemented here.

Possibly related PRs

  • TanStack/query#9655 — Touches the same injectQueries API surface, relocating/exporting it under experimental paths.
  • TanStack/query#9270 — Modifies injectQueries signature, types, and associated tests/docs, overlapping with this refactor.

Poem

A rabbit twitched its whiskered nose,
New signals hum where options rose;
Queries hop in tidy lines,
Types align like perfect pines;
Tests burrow deep to check the trace—
Then pop! two carrots, data in place. 🥕🐇

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "[WIP] Angular injectQueries fixes" directly reflects the primary change set (fixes to injectQueries in the Angular package) and is concise and relevant for reviewers; the only drawback is the "[WIP]" prefix which is extraneous for a merge-ready title.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • [ ] 📝 Generate Docstrings
🧪 Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment
  • [ ] Commit unit tests in branch angular-inject-queries-fixes

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3f5156db7e6c6cb7eb6b3a7033498b402d80cde7 and 26d7f36575dcc568a2c110d7b4497361a8f270f6.

📒 Files selected for processing (1)
  • packages/angular-query-experimental/src/__tests__/inject-queries.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/angular-query-experimental/src/tests/inject-queries.test.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Preview
  • GitHub Check: Test

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Sep 15 '25 00:09 coderabbitai[bot]

Codecov Report

:x: Patch coverage is 94.44444% with 2 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 46.19%. Comparing base (a2151d2) to head (26d7f36). :warning: Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8690      +/-   ##
==========================================
+ Coverage   45.77%   46.19%   +0.42%     
==========================================
  Files         213      213              
  Lines        8429     8453      +24     
  Branches     1916     1920       +4     
==========================================
+ Hits         3858     3905      +47     
+ Misses       4124     4105      -19     
+ Partials      447      443       -4     
Components Coverage Δ
@tanstack/angular-query-experimental 94.08% <94.44%> (+7.01%) :arrow_up:
@tanstack/eslint-plugin-query 83.24% <ø> (ø)
@tanstack/query-async-storage-persister 43.85% <ø> (ø)
@tanstack/query-broadcast-client-experimental 24.39% <ø> (ø)
@tanstack/query-codemods 0.00% <ø> (ø)
@tanstack/query-core 97.48% <ø> (ø)
@tanstack/query-devtools 3.48% <ø> (ø)
@tanstack/query-persist-client-core 79.60% <ø> (ø)
@tanstack/query-sync-storage-persister 84.61% <ø> (ø)
@tanstack/query-test-utils 77.77% <ø> (ø)
@tanstack/react-query 96.00% <ø> (ø)
@tanstack/react-query-devtools 10.00% <ø> (ø)
@tanstack/react-query-next-experimental ∅ <ø> (∅)
@tanstack/react-query-persist-client 100.00% <ø> (ø)
@tanstack/solid-query 78.06% <ø> (ø)
@tanstack/solid-query-devtools ∅ <ø> (∅)
@tanstack/solid-query-persist-client 100.00% <ø> (ø)
@tanstack/svelte-query 87.58% <ø> (ø)
@tanstack/svelte-query-devtools ∅ <ø> (∅)
@tanstack/svelte-query-persist-client 100.00% <ø> (ø)
@tanstack/vue-query 71.10% <ø> (ø)
@tanstack/vue-query-devtools ∅ <ø> (∅)
:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Sep 18 '25 15:09 codecov[bot]

@tomalaforge I merged this PR as it's definitely an improvement and long lived branches are a lot of work to keep up to date. However injectQueries isn't functional yet so can still be worked on. I moved it to a separate export so injectQueries won't hold up a stable release of the other APIs.

arnoud-dv avatar Sep 18 '25 17:09 arnoud-dv