Skip to content

fix: tanstack query integration#418

Open
brenelz wants to merge 2 commits intomainfrom
tanstack-query-fix
Open

fix: tanstack query integration#418
brenelz wants to merge 2 commits intomainfrom
tanstack-query-fix

Conversation

@brenelz
Copy link
Copy Markdown
Contributor

@brenelz brenelz commented Mar 30, 2026

Tested locally with

node ./cli/packages/cli/dist/index.js create my-app3 --add-ons tanstack-query

Summary by CodeRabbit

  • Refactor

    • TanStack Query initialization now happens earlier in app setup; the query client is created during router setup and passed into the provider.
    • Server-side query integration is registered during router creation for improved SSR behavior.
  • Chores

    • Provider integration entry removed from add-on metadata, leaving only the devtools integration.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

📝 Walkthrough

Walkthrough

Refactors TanStack Query integration to stop using a module-level cached context. getContext() always returns a fresh context; callers (router setup) create the context and pass it into TanStackQueryProvider and SSR integration instead of the provider creating it internally.

Changes

Cohort / File(s) Summary
Provider context refactor
packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs
Removed module-scoped cached context and early-return in getContext(). getContext() now builds and returns a fresh context; TanStackQueryProvider signature changed to accept context prop and read queryClient from it.
Integration metadata
packages/create/src/frameworks/react/add-ons/tanstack-query/info.json
Removed the provider integration entry for the root provider, leaving only the devtools integration in the integrations array.
Router setup changes
packages/create/src/frameworks/react/project/base/src/router.tsx.ejs
Router creation now instantiates a QueryClient / context once, passes it to createTanStackRouter via context, wraps with TanStackQueryProvider (passing context), and calls setupRouterSsrQueryIntegration with { router, queryClient }.

Sequence Diagram(s)

sequenceDiagram
    participant RouterSetup as Router Setup
    participant Caller as Caller
    participant GC as getContext()
    participant QC as QueryClient
    participant TRouter as createTanStackRouter
    participant Provider as TanStackQueryProvider
    participant SSR as setupRouterSsrQueryIntegration

    RouterSetup->>GC: call getContext()
    GC-->>RouterSetup: return context (includes QC)
    RouterSetup->>TRouter: createTanStackRouter(..., context)
    RouterSetup->>Provider: render Wrap with context prop
    Provider->>QC: read queryClient from context
    RouterSetup->>SSR: setupRouterSsrQueryIntegration({router, queryClient: QC})
    SSR-->>RouterSetup: SSR integration configured
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐇 I hopped in to tidy the stack,
Fresh context now—no looking back.
Router beckons, provider grins,
QueryClient spins new wins.
A rabbit's cheer for cleanly wrapped sins.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'fix: tanstack query integration' is vague and generic, lacking specificity about what aspect of the integration is being fixed. Consider a more descriptive title that clarifies the specific fix, such as 'fix: move TanStack Query context initialization to caller' or 'fix: remove cached context state from getContext'.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tanstack-query-fix

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs (1)

40-45: ⚠️ Potential issue | 🔴 Critical

Return the new context object directly instead of assigning to an undeclared variable.

The context variable at line 40 is assigned without being declared. This will cause a runtime error in the generated react + tanstack-query + tRPC template.

Suggested fix
 export function getContext() {
   const queryClient = new QueryClient({
     defaultOptions: {
       dehydrate: { serializeData: superjson.serialize },
       hydrate: { deserializeData: superjson.deserialize },
     },
   });

   const serverHelpers = createTRPCOptionsProxy({
     client: trpcClient,
     queryClient: queryClient,
   });
-  context = {
+  return {
     queryClient,
     trpc: serverHelpers,
   }
-
-  return context
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`
around lines 40 - 45, Replace the undeclared assignment to context with a direct
return of the object: instead of setting context = { queryClient, trpc:
serverHelpers } and returning context, return { queryClient, trpc: serverHelpers
} directly from the function (locate the return in root-provider.tsx.ejs where
queryClient and serverHelpers are in scope).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`:
- Around line 40-45: Replace the undeclared assignment to context with a direct
return of the object: instead of setting context = { queryClient, trpc:
serverHelpers } and returning context, return { queryClient, trpc: serverHelpers
} directly from the function (locate the return in root-provider.tsx.ejs where
queryClient and serverHelpers are in scope).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8f586686-6d53-466e-be20-d9ce66d07b10

📥 Commits

Reviewing files that changed from the base of the PR and between 40b9c12 and ac8d561.

📒 Files selected for processing (3)
  • packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs
  • packages/create/src/frameworks/react/add-ons/tanstack-query/info.json
  • packages/create/src/frameworks/react/project/base/src/router.tsx.ejs
💤 Files with no reviewable changes (1)
  • packages/create/src/frameworks/react/add-ons/tanstack-query/info.json

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs (1)

28-45: ⚠️ Potential issue | 🔴 Critical

Return the fresh context directly.

Line 40 assigns to context without any declaration in this template, so the generated code will fail with a reference error at runtime or fail TypeScript compilation with Cannot find name 'context'. The TanStackQueryProvider component expects this function to return the context object.

🔧 Proposed fix
 export function getContext() {
   const queryClient = new QueryClient({
     defaultOptions: {
       dehydrate: { serializeData: superjson.serialize },
       hydrate: { deserializeData: superjson.deserialize },
     },
   });

   const serverHelpers = createTRPCOptionsProxy({
     client: trpcClient,
-    queryClient: queryClient,
+    queryClient,
   });
-  context = {
+  return {
     queryClient,
     trpc: serverHelpers,
   }
-
-  return context
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`
around lines 28 - 45, The getContext function currently assigns to an undeclared
variable "context" causing a reference/TS error; change getContext to return the
fresh context object directly (or declare a local const if you need the
variable), e.g. build a local const (e.g., const context = { queryClient, trpc:
serverHelpers }) and then return that const; update references to
getContext/TanStackQueryProvider to use the returned object and avoid using a
global "context" variable.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`:
- Around line 28-45: The getContext function currently assigns to an undeclared
variable "context" causing a reference/TS error; change getContext to return the
fresh context object directly (or declare a local const if you need the
variable), e.g. build a local const (e.g., const context = { queryClient, trpc:
serverHelpers }) and then return that const; update references to
getContext/TanStackQueryProvider to use the returned object and avoid using a
global "context" variable.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 883dad3c-7b50-4797-a17a-c0b9a701c294

📥 Commits

Reviewing files that changed from the base of the PR and between ac8d561 and 8f9fcc3.

📒 Files selected for processing (2)
  • packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs
  • packages/create/src/frameworks/react/project/base/src/router.tsx.ejs
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/create/src/frameworks/react/project/base/src/router.tsx.ejs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant