Skip to content

Commit

Permalink
feat: add useLoadableQuery example (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell committed Feb 2, 2024
1 parent 0564c57 commit 0fb9bb8
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 25 deletions.
75 changes: 58 additions & 17 deletions apollo-client/v3/suspense-hooks/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apollo-client/v3/suspense-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@apollo/client": "3.8.0",
"@apollo/client": "^3.9.0-rc.1",
"@testing-library/jest-dom": "5.17.0",
"@testing-library/react": "14.0.0",
"@testing-library/user-event": "14.4.3",
Expand Down
13 changes: 7 additions & 6 deletions apollo-client/v3/suspense-hooks/src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import UseSuspenseQuery from "./useSuspenseQuery";
import UseSuspenseQueryChangingVariables from "./useSuspenseQuery-changing-variables";
import UseSuspenseQueryPartialData from "./useSuspenseQuery-partialData";
import UseSuspenseQueryErrorHandling from "./useSuspsenseQuery-error-handling";
import UseBackgroundQuery from "./useBackgroundQuery";
import Refetch from "./refetch-fetchMore";
import useLoadableQuery from "./useLoadableQuery";

export const routes = [
{
Expand All @@ -22,11 +22,6 @@ export const routes = [
title: "<code>useSuspenseQuery</code>: rendering partial data",
Element: UseSuspenseQueryPartialData,
},
{
path: "useSuspenseQuery-error-handling",
title: "<code>useSuspenseQuery</code>: error handling",
Element: UseSuspenseQueryErrorHandling,
},
{
path: "useBackgroundQuery-useReadQuery",
title:
Expand All @@ -39,4 +34,10 @@ export const routes = [
"Refetching with <code>useSuspenseQuery</code> and <code>useBackgroundQuery</code>",
Element: Refetch,
},
{
path: "useLoadableQuery",
title:
"Fetching data on user interaction with <code>useLoadableQuery</code>",
Element: useLoadableQuery,
},
];
1 change: 0 additions & 1 deletion apollo-client/v3/suspense-hooks/src/useBackgroundQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ interface Variables {
interface DogProps {
id: string;
queryRef: QueryReference<BreedData>;
// dogQueryRef: QueryReference<DogData>;
}

interface BreedsProps {
Expand Down
88 changes: 88 additions & 0 deletions apollo-client/v3/suspense-hooks/src/useLoadableQuery.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Suspense } from "react";
import {
gql,
QueryReference,
TypedDocumentNode,
useLoadableQuery,
useReadQuery,
useSuspenseQuery,
} from "@apollo/client";

interface DogsData {
dogs: {
id: string;
name: string;
}[];
}

interface DogData {
dog: {
id: string;
name: string;
breed: string;
};
}

interface Variables {
id: string;
}

export const GET_DOG_QUERY: TypedDocumentNode<DogData, Variables> = gql`
query GetDog($id: String) {
dog(id: $id) {
# By default, an object's cache key is a combination of its
# __typename and id fields, so we should always make sure the
# id is in the response so our data can be normalized and cached properly.
id
name
breed
}
}
`;

export const GET_DOGS_QUERY: TypedDocumentNode<DogsData, Variables> = gql`
query GetDogs {
dogs {
id
name
breed
}
}
`;

function App() {
const { data } = useSuspenseQuery(GET_DOGS_QUERY);
const [loadDog, queryRef] = useLoadableQuery(GET_DOG_QUERY);

return (
<>
<select onChange={(e) => loadDog({ id: e.target.value })}>
{data.dogs.map(({ id, name }) => (
<option key={id} value={id}>
{name}
</option>
))}
</select>
<Suspense fallback={<div>Loading...</div>}>
{queryRef && <Dog queryRef={queryRef} />}
</Suspense>
</>
);
}

interface DogProps {
queryRef: QueryReference<DogData>;
}

function Dog({ queryRef }: DogProps) {
const { data } = useReadQuery(queryRef);

return (
<>
<div>Name: {data.dog.name}</div>
<div>Breed: {data.dog.breed}</div>
</>
);
}

export default App;
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const GET_DOG_QUERY: TypedDocumentNode<DogData, Variables> = gql`
# id is in the response so our data can be normalized and cached properly.
id
name
breed
}
}
`;
Expand All @@ -41,6 +42,7 @@ export const GET_DOGS_QUERY: TypedDocumentNode<DogsData, Variables> = gql`
dogs {
id
name
breed
}
}
`;
Expand Down

0 comments on commit 0fb9bb8

Please sign in to comment.