-
-
Notifications
You must be signed in to change notification settings - Fork 108
apollo client add-on #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+374
−34
Closed
apollo client add-on #261
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| # Apollo Client Integration | ||
|
|
||
| This add-on integrates Apollo Client with TanStack Start to provide modern streaming SSR support for GraphQL data fetching. | ||
|
|
||
| ## Dependencies | ||
|
|
||
| The following packages are automatically installed: | ||
|
|
||
| - `@apollo/client` - Apollo Client core | ||
| - `@apollo/client-integration-tanstack-start` - TanStack Start integration | ||
| - `graphql` - GraphQL implementation | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### 1. GraphQL Endpoint | ||
|
|
||
| Configure your GraphQL API endpoint in `src/router.tsx`: | ||
|
|
||
| ```tsx | ||
| // Configure Apollo Client | ||
| const apolloClient = new ApolloClient({ | ||
| cache: new InMemoryCache(), | ||
| link: new HttpLink({ | ||
| uri: 'https://your-graphql-api.example.com/graphql', // Update this! | ||
| }), | ||
| }) | ||
| ``` | ||
|
|
||
| You can use environment variables by creating a `.env.local` file: | ||
|
|
||
| ```bash | ||
| VITE_GRAPHQL_ENDPOINT=https://your-api.com/graphql | ||
| ``` | ||
|
|
||
| The default configuration already uses this pattern: | ||
|
|
||
| ```tsx | ||
| uri: import.meta.env.VITE_GRAPHQL_ENDPOINT || | ||
| 'https://your-graphql-api.example.com/graphql' | ||
| ``` | ||
|
|
||
| ## Usage Patterns | ||
|
|
||
| ### Pattern 1: Loader with preloadQuery (Recommended for SSR) | ||
|
|
||
| Use `preloadQuery` in route loaders for optimal streaming SSR performance: | ||
|
|
||
| ```tsx | ||
| import { gql, TypedDocumentNode } from '@apollo/client' | ||
| import { useReadQuery } from '@apollo/client/react' | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| const MY_QUERY: TypedDocumentNode<{ | ||
| posts: { id: string; title: string; content: string }[] | ||
| }> = gql` | ||
| query GetData { | ||
| posts { | ||
| id | ||
| title | ||
| content | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| export const Route = createFileRoute('/my-route')({ | ||
| component: RouteComponent, | ||
| loader: ({ context: { preloadQuery } }) => { | ||
| const queryRef = preloadQuery(MY_QUERY, { | ||
| variables: {}, | ||
| }) | ||
| return { queryRef } | ||
| }, | ||
| }) | ||
|
|
||
| function RouteComponent() { | ||
| const { queryRef } = Route.useLoaderData() | ||
| const { data } = useReadQuery(queryRef) | ||
|
|
||
| return <div>{/* render your data */}</div> | ||
| } | ||
| ``` | ||
|
|
||
| ### Pattern 2: useSuspenseQuery | ||
|
|
||
| Use `useSuspenseQuery` directly in components with automatic suspense support: | ||
|
|
||
| ```tsx | ||
| import { gql, TypedDocumentNode } from '@apollo/client' | ||
| import { useSuspenseQuery } from '@apollo/client/react' | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| const MY_QUERY: TypedDocumentNode<{ | ||
| posts: { id: string; title: string }[] | ||
| }> = gql` | ||
| query GetData { | ||
| posts { | ||
| id | ||
| title | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| export const Route = createFileRoute('/my-route')({ | ||
| component: RouteComponent, | ||
| }) | ||
|
|
||
| function RouteComponent() { | ||
| const { data } = useSuspenseQuery(MY_QUERY) | ||
|
|
||
| return <div>{/* render your data */}</div> | ||
| } | ||
| ``` | ||
|
|
||
| ### Pattern 3: Manual Refetching | ||
|
|
||
| ```tsx | ||
| import { useQueryRefHandlers, useReadQuery } from '@apollo/client/react' | ||
|
|
||
| function MyComponent() { | ||
| const { queryRef } = Route.useLoaderData() | ||
| const { refetch } = useQueryRefHandlers(queryRef) | ||
| const { data } = useReadQuery(queryRef) | ||
|
|
||
| return ( | ||
| <div> | ||
| <button onClick={() => refetch()}>Refresh</button> | ||
| {/* render data */} | ||
| </div> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ## Important Notes | ||
|
|
||
| ### SSR Optimization | ||
|
|
||
| The integration automatically handles: | ||
|
|
||
| - Query deduplication across server and client | ||
| - Streaming SSR with `@defer` directive support | ||
| - Proper cache hydration | ||
|
|
||
| ## Learn More | ||
|
|
||
| - [Apollo Client Documentation](https://www.apollographql.com/docs/react) | ||
| - [@apollo/client-integration-tanstack-start](https://www.npmjs.com/package/@apollo/client-integration-tanstack-start) | ||
|
|
||
| ## Demo | ||
|
|
||
| Visit `/demo/apollo-client` in your application to see a working example of Apollo Client integration. |
75 changes: 75 additions & 0 deletions
75
frameworks/react-cra/add-ons/apollo-client/assets/src/routes/demo.apollo-client.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { gql, TypedDocumentNode } from '@apollo/client' | ||
| import { useReadQuery } from '@apollo/client/react' | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
| import React from 'react' | ||
|
|
||
| // Example GraphQL query - replace with your own schema | ||
| const EXAMPLE_QUERY: TypedDocumentNode<{ | ||
| continents: { __typename: string; code: string; name: string } | ||
| }> = gql` | ||
| query ExampleQuery { | ||
| continents { | ||
| code | ||
| name | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| export const Route = createFileRoute('/demo/apollo-client')({ | ||
| component: RouteComponent, | ||
| loader: ({ context: { preloadQuery } }) => { | ||
| // Preload the query in the loader for optimal performance | ||
| const queryRef = preloadQuery(EXAMPLE_QUERY, { | ||
| variables: {}, | ||
| }) | ||
| return { | ||
| queryRef, | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| function RouteComponent() { | ||
| const { queryRef } = Route.useLoaderData() | ||
| const { data } = useReadQuery(queryRef) | ||
|
|
||
| return ( | ||
| <div className="p-4"> | ||
| <h2 className="text-2xl font-bold mb-4">Apollo Client Demo</h2> | ||
| <div className="bg-blue-100 border border-blue-400 text-blue-700 px-4 py-3 rounded mb-4"> | ||
| <p className="font-bold">Apollo Client is configured!</p> | ||
| <p className="text-sm mt-2"> | ||
| This demo uses{' '} | ||
| <code className="bg-blue-200 px-1 rounded">preloadQuery</code> in the | ||
| loader and{' '} | ||
| <code className="bg-blue-200 px-1 rounded">useReadQuery</code> in the | ||
| component for optimal streaming SSR performance. | ||
| </p> | ||
| </div> | ||
| <div className="bg-gray-100 p-4 rounded"> | ||
| <h3 className="font-bold mb-2">Query Result:</h3> | ||
| <pre className="text-sm overflow-x-auto"> | ||
| {JSON.stringify(data, null, 2)} | ||
| </pre> | ||
| </div> | ||
| <div className="mt-4 text-sm text-gray-600"> | ||
| <p>📝 Next steps:</p> | ||
| <ul className="list-disc ml-6 mt-2"> | ||
| <li> | ||
| Configure your GraphQL endpoint in{' '} | ||
| <code className="bg-gray-200 px-1 rounded">src/router.tsx</code> | ||
| </li> | ||
| <li>Replace the example query with your actual GraphQL schema</li> | ||
| <li> | ||
| Learn more:{' '} | ||
| <a | ||
| href="https://www.apollographql.com/docs/react" | ||
| className="text-blue-600 hover:underline" | ||
| > | ||
| Apollo Client Docs | ||
| </a> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "name": "Apollo Client", | ||
| "description": "Integrate Apollo Client with streaming SSR support for GraphQL data fetching.", | ||
| "phase": "add-on", | ||
| "modes": ["file-router"], | ||
| "type": "add-on", | ||
| "priority": 15, | ||
| "link": "https://github.com/apollographql/apollo-client-integrations/tree/main/packages/tanstack-start", | ||
| "dependsOn": ["start"], | ||
| "routes": [ | ||
| { | ||
| "icon": "Network", | ||
| "url": "/demo/apollo-client", | ||
| "name": "Apollo Client", | ||
| "path": "src/routes/demo.apollo-client.tsx", | ||
| "jsName": "ApolloClientDemo" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "dependencies": { | ||
| "@apollo/client": "^4.0.0", | ||
| "@apollo/client-integration-tanstack-start": "^0.14.2-rc.0", | ||
| "graphql": "^16.10.0", | ||
| "rxjs": "^7.8.2" | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| { | ||
| "dependencies": { | ||
| "@convex-dev/react-query": "0.0.0-alpha.11", | ||
| "@convex-dev/react-query": "0.1.0", | ||
| "convex": "^1.27.3", | ||
| "lucide-react": "^0.544.0" | ||
| "lucide-react": "^0.561.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "dependencies": { | ||
| "@modelcontextprotocol/sdk": "^1.17.0", | ||
| "zod": "4.1.11" | ||
| "zod": "4.2.1" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "dependencies": { | ||
| "@neondatabase/serverless": "^1.0.0", | ||
| "@neondatabase/vite-plugin-postgres": "^0.2.0" | ||
| "@neondatabase/vite-plugin-postgres": "^0.5.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if these options are supposed to be different when RQ is enabled vs not enabled or if they just ran out of sync in the previous template structure.