Skip to content

Feat/compt 40 create query factory#2

Closed
a-elkhiraooui-ciscode wants to merge 3 commits intodevelopfrom
feat/COMPT-40-create-query-factory
Closed

Feat/compt 40 create query factory#2
a-elkhiraooui-ciscode wants to merge 3 commits intodevelopfrom
feat/COMPT-40-create-query-factory

Conversation

@a-elkhiraooui-ciscode
Copy link
Copy Markdown

Summary

  • What does this PR change?

Why

  • Why is this change needed?

Checklist

  • Added/updated tests (if behavior changed)
  • npm run lint passes
  • npm run typecheck passes
  • npm test passes
  • npm run build passes
  • Added a changeset (npx changeset) if this affects consumers

Notes

  • Anything reviewers should pay attention to?

- Add createQuery(keyFn, fetcher) returning QueryDefinition<TParams, TData>
- TData and TParams fully inferred from fetcher signature, zero manual annotation
- queryKey returns stable readonly tuple via keyFn
- useQuery shorthand hook wraps useTanstackQuery with typed params
- Export from src/index.ts
- Add @tanstack/react-query as peerDependency (>=5) and devDependency
- Add pnpm cssstyle override to fix Node.js v22 + jsdom@28 ESM compat issue
- Fix duplicate import in vitest.config.ts
- 9 tests, 100% coverage on createQuery.ts, 88.6% overall

Closes COMPT-40
@a-elkhiraooui-ciscode a-elkhiraooui-ciscode requested a review from a team as a code owner April 6, 2026 10:47
Copilot AI review requested due to automatic review settings April 6, 2026 10:47
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a small “query definition” factory around TanStack React Query v5 and exposes it as part of the package’s public API.

Changes:

  • Introduces createQuery (queryKey/queryFn/useQuery) under src/query and exports it from src/index.ts.
  • Adds unit tests for createQuery using React Testing Library + TanStack Query’s QueryClientProvider.
  • Updates dependencies/lockfiles to include @tanstack/react-query and adds pnpm-specific configuration.

Reviewed changes

Copilot reviewed 6 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
vitest.config.ts Removes a duplicate defineConfig import.
src/query/index.ts New barrel export for query utilities.
src/query/createQuery.ts Implements the createQuery factory and related types.
src/query/createQuery.test.tsx Adds tests covering the query definition shape and hook behavior.
src/index.ts Exposes the new query module via the public API.
package.json Adds React Query peer/dev deps; adds pnpm override + packageManager metadata.
package-lock.json Updates lockfile to include React Query and package metadata changes.

Comment on lines +7 to +8
type UseQueryShorthandOptions<TData> = Omit<UseQueryOptions<TData, Error>, 'queryKey' | 'queryFn'>;

Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UseQueryShorthandOptions binds the query function data type and the returned data type to the same TData (because UseQueryOptions<TData, Error> sets both TQueryFnData and TData). This prevents consumers from using options like select to transform the fetched data into a different output type. Consider introducing separate generics (e.g. TQueryFnData vs TData) or defining the options type as UseQueryOptions<TQueryFnData, Error, TData> so select can change the result type safely.

Copilot uses AI. Check for mistakes.
Comment on lines +82 to +99
it('useQuery hook shows loading state before data resolves', () => {
let resolve: (v: { value: number }) => void;
const fetcher = vi.fn(
() =>
new Promise<{ value: number }>((res) => {
resolve = res;
}),
);
const def = createQuery((n: number) => ['lazy', n] as const, fetcher);

const { result } = renderHook(() => def.useQuery(99), {
wrapper: makeWrapper(),
});

expect(result.current.isLoading).toBe(true);
// Resolve to avoid open handles
resolve!({ value: 99 });
});
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the loading-state test, the promise is resolved but the test does not wait for React Query to process the resolution. This can lead to async updates happening after the test finishes (and potential act/open-handle warnings). After calling resolve, wait for the hook to reach a settled state (e.g. isSuccess) or explicitly unmount/clear the QueryClient.

Copilot uses AI. Check for mistakes.
"access": "public"
},
"peerDependencies": {
"@tanstack/react-query": ">=5",
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The peer dependency range for @tanstack/react-query is >=5, which will also allow future major versions (e.g. v6) that may include breaking changes. Since this package targets TanStack Query v5, consider tightening the range (e.g. ^5 or >=5 <6) to avoid accidental incompatibilities for consumers.

Suggested change
"@tanstack/react-query": ">=5",
"@tanstack/react-query": "^5",

Copilot uses AI. Check for mistakes.
package.json Outdated
Comment on lines +90 to +94
"pnpm": {
"overrides": {
"cssstyle": "6.0.1"
}
},
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI uses npm ci (see .github/workflows/pr-validation.yml), so the pnpm.overrides block will not affect dependency resolution in CI or for npm users. If cssstyle must be pinned, use npm's overrides field (or align CI/package manager to pnpm) so the lockfile and install behavior are consistent.

Copilot uses AI. Check for mistakes.
package.json Outdated
Comment on lines +89 to +95
},
"pnpm": {
"overrides": {
"cssstyle": "6.0.1"
}
},
"packageManager": "pnpm@10.20.0+sha512.cf9998222162dd85864d0a8102e7892e7ba4ceadebbf5a31f9c2fce48dfce317a9c53b9f6464d1ef9042cba2e02ae02a9f7c143a2b438cd93c91840f0192b9dd"
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packageManager is set to pnpm@..., but the repository’s CI pipeline installs with npm (npm ci). This mismatch can cause contributors to generate different lockfiles/resolutions locally vs CI. Consider switching workflows to pnpm (and relying on pnpm-lock.yaml), or removing packageManager/pnpm-specific config to keep npm as the source of truth.

Suggested change
},
"pnpm": {
"overrides": {
"cssstyle": "6.0.1"
}
},
"packageManager": "pnpm@10.20.0+sha512.cf9998222162dd85864d0a8102e7892e7ba4ceadebbf5a31f9c2fce48dfce317a9c53b9f6464d1ef9042cba2e02ae02a9f7c143a2b438cd93c91840f0192b9dd"
}

Copilot uses AI. Check for mistakes.
Comment on lines +37 to +46
it('queryKey produces a stable reference for the same params', () => {
const def = createQuery(
(id: number) => ['item', id] as const,
async (id: number) => id,
);

const first = def.queryKey(1);
const second = def.queryKey(1);
expect(first).toEqual(second);
});
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test description says the key function returns a “stable reference”, but the assertion only uses toEqual (deep equality) and does not validate referential stability. Either change the test name/expectation to reflect deep equality, or change the assertion to check reference identity and update the implementation accordingly.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants