Skip to content

Commit

Permalink
Query keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dgca committed Apr 4, 2024
1 parent 5237325 commit d9d3876
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/data-facade/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
UseQueryResult,
useQuery,
} from "@tanstack/react-query";
import { buildQueryKey } from "./utils";

type UseQueryOptions = UndefinedInitialDataOptions<any, Error, any, unknown[]>;
type ResolverFunc<T = any> = (opts: T) => any;
Expand All @@ -22,7 +23,7 @@ function buildUseQuery(baseQueryKey: string) {
return <TResolver extends ResolverFunc>(resolver: TResolver) => ({
useQuery: (args?: unknown) => {
return useQuery({
queryKey: [baseQueryKey],
queryKey: [baseQueryKey, ...buildQueryKey(args)],
queryFn: () => resolver(args),
});
},
Expand Down
15 changes: 15 additions & 0 deletions packages/data-facade/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}

export function buildQueryKey(value: unknown) {
if (Array.isArray(value)) {
return value;
}

if (isPlainObject(value)) {
return Object.entries(value).flat();
}

return [value];
}
3 changes: 3 additions & 0 deletions packages/mobile-app/app/(tabs)/transact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export default function Transact() {
return (
<View>
<Text>Balances</Text>
<Text>{JSON.stringify(getAccountsResult.data)}</Text>
<Text>{JSON.stringify(getAccountsWithZodResult.data)}</Text>
<Text>{JSON.stringify(getAllAccountsResult.data)}</Text>
</View>
);
}
21 changes: 13 additions & 8 deletions packages/mobile-app/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { Stack } from "expo-router";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

const queryClient = new QueryClient();

export default function Layout() {
return (
<Stack>
<Stack.Screen
name="(tabs)"
options={{
headerShown: false,
}}
/>
</Stack>
<QueryClientProvider client={queryClient}>
<Stack>
<Stack.Screen
name="(tabs)"
options={{
headerShown: false,
}}
/>
</Stack>
</QueryClientProvider>
);
}

0 comments on commit d9d3876

Please sign in to comment.