|
| 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. |
0 commit comments