-
Notifications
You must be signed in to change notification settings - Fork 96
feat: introduce TanStack Query integration for bounties, including query options, keys, and prefetching utilities. #32
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
Merged
0xdevcollins
merged 2 commits into
boundlessfi:main
from
Dprof-in-tech:feat-create-query-keys-and-query-functions
Jan 25, 2026
Merged
Changes from all commits
Commits
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
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,49 @@ | ||
| import { queryOptions, infiniteQueryOptions } from '@tanstack/react-query'; | ||
| import { bountiesApi, type Bounty, type BountyListParams, type PaginatedResponse } from '@/lib/api'; | ||
| import { bountyKeys } from './query-keys'; | ||
|
|
||
| const DEFAULT_LIMIT = 20; | ||
|
|
||
| /** | ||
| * Query options factory for bounty list | ||
| */ | ||
| export function bountyListQueryOptions(params?: BountyListParams) { | ||
| return queryOptions<PaginatedResponse<Bounty>>({ | ||
| queryKey: bountyKeys.list(params), | ||
| queryFn: () => bountiesApi.list(params), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Query options factory for single bounty | ||
| */ | ||
| export function bountyDetailQueryOptions(id: string) { | ||
| return queryOptions<Bounty>({ | ||
| queryKey: bountyKeys.detail(id), | ||
| queryFn: () => bountiesApi.getById(id), | ||
| enabled: !!id, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Infinite query options for bounty pagination | ||
| */ | ||
| export function bountyInfiniteQueryOptions(params?: Omit<BountyListParams, 'page'>) { | ||
| return infiniteQueryOptions<PaginatedResponse<Bounty>>({ | ||
| queryKey: bountyKeys.infinite(params), | ||
| queryFn: ({ pageParam }) => | ||
| bountiesApi.list({ ...params, page: pageParam as number, limit: params?.limit ?? DEFAULT_LIMIT }), | ||
| initialPageParam: 1, | ||
| getNextPageParam: (lastPage) => { | ||
| const { page, totalPages } = lastPage.pagination; | ||
| return page < totalPages ? page + 1 : undefined; | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Helper to flatten infinite query pages | ||
| */ | ||
| export function flattenBountyPages(pages: PaginatedResponse<Bounty>[] | undefined): Bounty[] { | ||
| return pages?.flatMap((page) => page.data) ?? []; | ||
| } |
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,18 @@ | ||
| // Query Keys | ||
| export { bountyKeys, type BountyQueryKey } from './query-keys'; | ||
|
|
||
| // Query Options | ||
| export { | ||
| bountyListQueryOptions, | ||
| bountyDetailQueryOptions, | ||
| bountyInfiniteQueryOptions, | ||
| flattenBountyPages, | ||
| } from './bounty-queries'; | ||
|
|
||
| // Prefetch Utilities | ||
| export { | ||
| createQueryClient, | ||
| prefetchBountyList, | ||
| prefetchBounty, | ||
| prefetchBounties, | ||
| } from './prefetch'; |
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,48 @@ | ||
| import { QueryClient } from '@tanstack/react-query'; | ||
| import type { BountyListParams } from '@/lib/api'; | ||
| import { bountyListQueryOptions, bountyDetailQueryOptions } from './bounty-queries'; | ||
|
|
||
| /** | ||
| * Create a QueryClient for server components | ||
| * Each request should create a new instance to avoid sharing state | ||
| */ | ||
| export function createQueryClient(): QueryClient { | ||
| return new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { | ||
| staleTime: 60 * 1000, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Prefetch bounty list for server components | ||
| */ | ||
| export async function prefetchBountyList( | ||
| queryClient: QueryClient, | ||
| params?: BountyListParams | ||
| ): Promise<void> { | ||
| await queryClient.prefetchQuery(bountyListQueryOptions(params)); | ||
| } | ||
|
|
||
| /** | ||
| * Prefetch single bounty for server components | ||
| */ | ||
| export async function prefetchBounty( | ||
| queryClient: QueryClient, | ||
| id: string | ||
| ): Promise<void> { | ||
| if (!id) return; | ||
| await queryClient.prefetchQuery(bountyDetailQueryOptions(id)); | ||
| } | ||
|
|
||
| /** | ||
| * Prefetch multiple bounties by ID (for list pages with detail prefetch) | ||
| */ | ||
| export async function prefetchBounties( | ||
| queryClient: QueryClient, | ||
| ids: string[] | ||
| ): Promise<void> { | ||
| await Promise.all(ids.map((id) => prefetchBounty(queryClient, id))); | ||
| } | ||
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,27 @@ | ||
| import type { BountyListParams } from '@/lib/api'; | ||
|
|
||
| /** | ||
| * Query Key Factory for Bounties | ||
| * | ||
| * Hierarchical structure enables granular cache invalidation: | ||
| * - bountyKeys.all → invalidates everything | ||
| * - bountyKeys.lists() → invalidates all lists, keeps details | ||
| * - bountyKeys.list(filters) → invalidates specific filtered list | ||
| * - bountyKeys.details() → invalidates all details, keeps lists | ||
| * - bountyKeys.detail(id) → invalidates specific bounty | ||
| */ | ||
| export const bountyKeys = { | ||
| all: ['bounties'] as const, | ||
| lists: () => [...bountyKeys.all, 'list'] as const, | ||
| list: (filters?: BountyListParams) => [...bountyKeys.lists(), filters] as const, | ||
| infinite: (filters?: Omit<BountyListParams, 'page'>) => [...bountyKeys.lists(), 'infinite', filters] as const, | ||
| details: () => [...bountyKeys.all, 'detail'] as const, | ||
| detail: (id: string) => [...bountyKeys.details(), id] as const, | ||
| }; | ||
|
|
||
| // Type helpers for query keys | ||
| export type BountyQueryKey = | ||
| | ReturnType<typeof bountyKeys.list> | ||
| | ReturnType<typeof bountyKeys.infinite> | ||
| | ReturnType<typeof bountyKeys.detail>; | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.