Skip to content

Commit a0b407c

Browse files
authored
fix: tanstack query integration (#418)
* fix: tanstack query integration * fix: tanstack query integration * fix: trpc integration * chore: add changeset
1 parent 40b9c12 commit a0b407c

File tree

4 files changed

+40
-59
lines changed

4 files changed

+40
-59
lines changed

.changeset/soft-corners-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/create': patch
3+
---
4+
5+
Fix Tanstack Query Integration

packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,7 @@ export const trpcClient = createTRPCClient<TRPCRouter>({
2525
],
2626
});
2727
28-
let context:
29-
| {
30-
queryClient: QueryClient
31-
trpc: ReturnType<typeof createTRPCOptionsProxy<TRPCRouter>>
32-
}
33-
| undefined
34-
3528
export function getContext() {
36-
if (context) {
37-
return context
38-
}
39-
4029
const queryClient = new QueryClient({
4130
defaultOptions: {
4231
dehydrate: { serializeData: superjson.serialize },
@@ -48,64 +37,38 @@ export function getContext() {
4837
client: trpcClient,
4938
queryClient: queryClient,
5039
});
51-
context = {
40+
const context = {
5241
queryClient,
5342
trpc: serverHelpers,
5443
}
5544
5645
return context
5746
}
5847
59-
export default function TanStackQueryProvider({
48+
export default function TanstackQueryProvider({
6049
children,
50+
context,
6151
}: {
62-
children: ReactNode
52+
children: ReactNode,
53+
context: ReturnType<typeof getContext>
6354
}) {
64-
const { queryClient } = getContext()
55+
const { queryClient } = context
6556
6657
return (
67-
<QueryClientProvider client={queryClient}>
68-
<TRPCProvider trpcClient={trpcClient} queryClient={queryClient}>
69-
{children}
70-
</TRPCProvider>
71-
</QueryClientProvider>
58+
<TRPCProvider trpcClient={trpcClient} queryClient={queryClient}>
59+
{children}
60+
</TRPCProvider>
7261
)
7362
}
7463
<% } else { %>
75-
import type { ReactNode } from 'react'
76-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
77-
78-
let context:
79-
| {
80-
queryClient: QueryClient
81-
}
82-
| undefined
64+
import { QueryClient } from '@tanstack/react-query'
8365
8466
export function getContext() {
85-
if (context) {
86-
return context
87-
}
88-
8967
const queryClient = new QueryClient();
9068
91-
context = {
69+
return {
9270
queryClient,
9371
}
94-
95-
return context
96-
}
97-
98-
export default function TanStackQueryProvider({
99-
children,
100-
}: {
101-
children: ReactNode
102-
}) {
103-
const { queryClient } = getContext()
104-
105-
return (
106-
<QueryClientProvider client={queryClient}>
107-
{children}
108-
</QueryClientProvider>
109-
)
11072
}
73+
export default function TanstackQueryProvider() {}
11174
<% } %>

packages/create/src/frameworks/react/add-ons/tanstack-query/info.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
}
1919
],
2020
"integrations": [
21-
{
22-
"type": "provider",
23-
"path": "src/integrations/tanstack-query/root-provider.tsx",
24-
"jsName": "TanStackQueryProvider"
25-
},
2621
{
2722
"type": "devtools",
2823
"path": "src/integrations/tanstack-query/devtools.tsx",

packages/create/src/frameworks/react/project/base/src/router.tsx.ejs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
22
import { routeTree } from './routeTree.gen'
33
<% if (addOnEnabled['tanstack-query']) { %>
4-
import { getContext } from './integrations/tanstack-query/root-provider'
4+
import type { ReactNode } from 'react'
5+
import { QueryClient } from '@tanstack/react-query'
6+
import { setupRouterSsrQueryIntegration } from "@tanstack/react-router-ssr-query";
7+
import TanstackQueryProvider, { getContext } from './integrations/tanstack-query/root-provider'
58
<% } %>
69

710
export function getRouter() {
11+
<% if (addOnEnabled['tanstack-query']) { %>
12+
const context = getContext();
13+
<% } %>
14+
815
const router = createTanStackRouter({
9-
routeTree,
10-
<% if (addOnEnabled['tanstack-query']) { %>
11-
context: getContext(),
12-
<% } else if (addOnEnabled['apollo-client']) { %>
16+
routeTree,<% if (addOnEnabled['tanstack-query']) { %>
17+
context,<% } else if (addOnEnabled['apollo-client']) { %>
1318
context: {} as any,
1419
<% } %>
1520
scrollRestoration: true,
1621
defaultPreload: 'intent',
1722
defaultPreloadStaleTime: 0,
23+
<% if (addOnEnabled.tRPC) { %>
24+
Wrap: (props: { children: ReactNode }) => {
25+
return (
26+
<TanstackQueryProvider context={context}>
27+
{props.children}
28+
</TanstackQueryProvider>
29+
);
30+
},
31+
<% } %>
1832
})
1933

34+
<% if (addOnEnabled['tanstack-query']) { %>
35+
setupRouterSsrQueryIntegration({ router, queryClient: context.queryClient })
36+
<% } %>
37+
2038
return router
2139
}
2240

0 commit comments

Comments
 (0)