Skip to content

Commit 5b800d0

Browse files
jherrphryneas
andauthored
feat: addon apollo client (#262)
* apollo client add-on * add rjxs * use countries api * more checksums --------- Co-authored-by: Lenz Weber-Tronic <[email protected]>
1 parent 9c1f2fa commit 5b800d0

File tree

19 files changed

+373
-33
lines changed

19 files changed

+373
-33
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Apollo Client Integration
2+
3+
This add-on integrates Apollo Client with TanStack Start to provide modern streaming SSR support for GraphQL data fetching.
4+
5+
## Dependencies
6+
7+
The following packages are automatically installed:
8+
9+
- `@apollo/client` - Apollo Client core
10+
- `@apollo/client-integration-tanstack-start` - TanStack Start integration
11+
- `graphql` - GraphQL implementation
12+
13+
## Configuration
14+
15+
### 1. GraphQL Endpoint
16+
17+
Configure your GraphQL API endpoint in `src/router.tsx`:
18+
19+
```tsx
20+
// Configure Apollo Client
21+
const apolloClient = new ApolloClient({
22+
cache: new InMemoryCache(),
23+
link: new HttpLink({
24+
uri: 'https://your-graphql-api.example.com/graphql', // Update this!
25+
}),
26+
})
27+
```
28+
29+
You can use environment variables by creating a `.env.local` file:
30+
31+
```bash
32+
VITE_GRAPHQL_ENDPOINT=https://your-api.com/graphql
33+
```
34+
35+
The default configuration already uses this pattern:
36+
37+
```tsx
38+
uri: import.meta.env.VITE_GRAPHQL_ENDPOINT ||
39+
'https://your-graphql-api.example.com/graphql'
40+
```
41+
42+
## Usage Patterns
43+
44+
### Pattern 1: Loader with preloadQuery (Recommended for SSR)
45+
46+
Use `preloadQuery` in route loaders for optimal streaming SSR performance:
47+
48+
```tsx
49+
import { gql, TypedDocumentNode } from '@apollo/client'
50+
import { useReadQuery } from '@apollo/client/react'
51+
import { createFileRoute } from '@tanstack/react-router'
52+
53+
const MY_QUERY: TypedDocumentNode<{
54+
posts: { id: string; title: string; content: string }[]
55+
}> = gql`
56+
query GetData {
57+
posts {
58+
id
59+
title
60+
content
61+
}
62+
}
63+
`
64+
65+
export const Route = createFileRoute('/my-route')({
66+
component: RouteComponent,
67+
loader: ({ context: { preloadQuery } }) => {
68+
const queryRef = preloadQuery(MY_QUERY, {
69+
variables: {},
70+
})
71+
return { queryRef }
72+
},
73+
})
74+
75+
function RouteComponent() {
76+
const { queryRef } = Route.useLoaderData()
77+
const { data } = useReadQuery(queryRef)
78+
79+
return <div>{/* render your data */}</div>
80+
}
81+
```
82+
83+
### Pattern 2: useSuspenseQuery
84+
85+
Use `useSuspenseQuery` directly in components with automatic suspense support:
86+
87+
```tsx
88+
import { gql, TypedDocumentNode } from '@apollo/client'
89+
import { useSuspenseQuery } from '@apollo/client/react'
90+
import { createFileRoute } from '@tanstack/react-router'
91+
92+
const MY_QUERY: TypedDocumentNode<{
93+
posts: { id: string; title: string }[]
94+
}> = gql`
95+
query GetData {
96+
posts {
97+
id
98+
title
99+
}
100+
}
101+
`
102+
103+
export const Route = createFileRoute('/my-route')({
104+
component: RouteComponent,
105+
})
106+
107+
function RouteComponent() {
108+
const { data } = useSuspenseQuery(MY_QUERY)
109+
110+
return <div>{/* render your data */}</div>
111+
}
112+
```
113+
114+
### Pattern 3: Manual Refetching
115+
116+
```tsx
117+
import { useQueryRefHandlers, useReadQuery } from '@apollo/client/react'
118+
119+
function MyComponent() {
120+
const { queryRef } = Route.useLoaderData()
121+
const { refetch } = useQueryRefHandlers(queryRef)
122+
const { data } = useReadQuery(queryRef)
123+
124+
return (
125+
<div>
126+
<button onClick={() => refetch()}>Refresh</button>
127+
{/* render data */}
128+
</div>
129+
)
130+
}
131+
```
132+
133+
## Important Notes
134+
135+
### SSR Optimization
136+
137+
The integration automatically handles:
138+
139+
- Query deduplication across server and client
140+
- Streaming SSR with `@defer` directive support
141+
- Proper cache hydration
142+
143+
## Learn More
144+
145+
- [Apollo Client Documentation](https://www.apollographql.com/docs/react)
146+
- [@apollo/client-integration-tanstack-start](https://www.npmjs.com/package/@apollo/client-integration-tanstack-start)
147+
148+
## Demo
149+
150+
Visit `/demo/apollo-client` in your application to see a working example of Apollo Client integration.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { gql, TypedDocumentNode } from '@apollo/client'
2+
import { useReadQuery } from '@apollo/client/react'
3+
import { createFileRoute } from '@tanstack/react-router'
4+
import React from 'react'
5+
6+
// Example GraphQL query - replace with your own schema
7+
const EXAMPLE_QUERY: TypedDocumentNode<{
8+
continents: { __typename: string; code: string; name: string }
9+
}> = gql`
10+
query ExampleQuery {
11+
continents {
12+
code
13+
name
14+
}
15+
}
16+
`
17+
18+
export const Route = createFileRoute('/demo/apollo-client')({
19+
component: RouteComponent,
20+
loader: ({ context: { preloadQuery } }) => {
21+
// Preload the query in the loader for optimal performance
22+
const queryRef = preloadQuery(EXAMPLE_QUERY, {
23+
variables: {},
24+
})
25+
return {
26+
queryRef,
27+
}
28+
},
29+
})
30+
31+
function RouteComponent() {
32+
const { queryRef } = Route.useLoaderData()
33+
const { data } = useReadQuery(queryRef)
34+
35+
return (
36+
<div className="p-4">
37+
<h2 className="text-2xl font-bold mb-4">Apollo Client Demo</h2>
38+
<div className="bg-blue-100 border border-blue-400 text-blue-700 px-4 py-3 rounded mb-4">
39+
<p className="font-bold">Apollo Client is configured!</p>
40+
<p className="text-sm mt-2">
41+
This demo uses{' '}
42+
<code className="bg-blue-200 px-1 rounded">preloadQuery</code> in the
43+
loader and{' '}
44+
<code className="bg-blue-200 px-1 rounded">useReadQuery</code> in the
45+
component for optimal streaming SSR performance.
46+
</p>
47+
</div>
48+
<div className="bg-gray-100 p-4 rounded">
49+
<h3 className="font-bold mb-2">Query Result:</h3>
50+
<pre className="text-sm overflow-x-auto">
51+
{JSON.stringify(data, null, 2)}
52+
</pre>
53+
</div>
54+
<div className="mt-4 text-sm text-gray-600">
55+
<p>📝 Next steps:</p>
56+
<ul className="list-disc ml-6 mt-2">
57+
<li>
58+
Configure your GraphQL endpoint in{' '}
59+
<code className="bg-gray-200 px-1 rounded">src/router.tsx</code>
60+
</li>
61+
<li>Replace the example query with your actual GraphQL schema</li>
62+
<li>
63+
Learn more:{' '}
64+
<a
65+
href="https://www.apollographql.com/docs/react"
66+
className="text-blue-600 hover:underline"
67+
>
68+
Apollo Client Docs
69+
</a>
70+
</li>
71+
</ul>
72+
</div>
73+
</div>
74+
)
75+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "Apollo Client",
3+
"description": "Integrate Apollo Client with streaming SSR support for GraphQL data fetching.",
4+
"phase": "add-on",
5+
"modes": ["file-router"],
6+
"type": "add-on",
7+
"priority": 15,
8+
"link": "https://github.com/apollographql/apollo-client-integrations/tree/main/packages/tanstack-start",
9+
"dependsOn": ["start"],
10+
"routes": [
11+
{
12+
"icon": "Network",
13+
"url": "/demo/apollo-client",
14+
"name": "Apollo Client",
15+
"path": "src/routes/demo.apollo-client.tsx",
16+
"jsName": "ApolloClientDemo"
17+
}
18+
]
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"dependencies": {
3+
"@apollo/client": "^4.0.0",
4+
"@apollo/client-integration-tanstack-start": "^0.14.2-rc.0",
5+
"graphql": "^16.10.0",
6+
"rxjs": "^7.8.2"
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Loading
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dependencies": {
3-
"@convex-dev/react-query": "0.0.0-alpha.11",
3+
"@convex-dev/react-query": "0.1.0",
44
"convex": "^1.27.3",
5-
"lucide-react": "^0.544.0"
5+
"lucide-react": "^0.561.0"
66
}
77
}

frameworks/react-cra/add-ons/db/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"dependencies": {
3-
"@tanstack/query-db-collection": "^0.2.0",
3+
"@tanstack/query-db-collection": "^1.0.8",
44
"@tanstack/react-db": "^0.1.1",
55
"zod": "^4.0.14"
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"dependencies": {
33
"@modelcontextprotocol/sdk": "^1.17.0",
4-
"zod": "4.1.11"
4+
"zod": "4.2.1"
55
}
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"dependencies": {
33
"@neondatabase/serverless": "^1.0.0",
4-
"@neondatabase/vite-plugin-postgres": "^0.2.0"
4+
"@neondatabase/vite-plugin-postgres": "^0.5.0"
55
}
66
}

frameworks/react-cra/add-ons/shadcn/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"dependencies": {
33
"class-variance-authority": "^0.7.1",
44
"clsx": "^2.1.1",
5-
"lucide-react": "^0.544.0",
5+
"lucide-react": "^0.561.0",
66
"tailwind-merge": "^3.0.2",
77
"tw-animate-css": "^1.3.6"
88
}

0 commit comments

Comments
 (0)