Repository navigation
路線推定の候補路線駅取得をlineListStationsに変更しN+1問題を解消 - #5755
Conversation
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
📝 WalkthroughWalkthrough単一のバルクGraphQLクエリ( Changes
Sequence Diagram(s)sequenceDiagram
participant Hook as "useRouteEstimation\n(hook)"
participant Apollo as "Apollo\nuseLazyQuery"
participant Server as "GraphQL\nServer"
participant Grouper as "In-memory\nGrouper"
Hook->>Apollo: execute GET_LINE_LIST_STATIONS(lineIds)
Apollo->>Server: GraphQL request\nGetLineListStations(lineIds)
Server-->>Apollo: response: lineListStations[{station, lines[]},...]
Apollo-->>Hook: return lineListStations
Hook->>Grouper: group stations by station.lines[].id
Grouper-->>Hook: per-line station lists
Hook->>Hook: build candidates and update state
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/hooks/useRouteEstimation.ts (1)
178-204: N+1問題の解消を確認しました。一括取得と路線IDごとのグルーピングロジックは正しく実装されています。
station.linesをイテレートして複数路線に駅を振り分けるアプローチは適切です。軽微な考慮点として、
lineIdsが空配列の場合でもクエリが実行されます。現状の実装ではnearbyStationsが空でない限り問題ありませんが、早期リターンを追加することで不要なネットワークリクエストを防げます。♻️ 空配列の場合の早期リターン(オプション)
// 全候補路線の駅リストを一括取得 const lineIds = Array.from(lineIdSet); + if (lineIds.length === 0) { + setState((prev) => ({ ...prev, status: 'collecting' })); + return; + } const lineListResult = await fetchLineListStations({ variables: { lineIds }, });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/hooks/useRouteEstimation.ts` around lines 178 - 204, If lineIds (derived from lineIdSet) is empty you should avoid calling fetchLineListStations and return early; inside useRouteEstimation (where lineIdSet, lineIds, and fetchLineListStations are used) add a guard like if (lineIds.length === 0) return an appropriate empty candidates result (or skip the block) so no unnecessary network request is made when there are no line IDs to query.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/hooks/useRouteEstimation.ts`:
- Around line 178-204: If lineIds (derived from lineIdSet) is empty you should
avoid calling fetchLineListStations and return early; inside useRouteEstimation
(where lineIdSet, lineIds, and fetchLineListStations are used) add a guard like
if (lineIds.length === 0) return an appropriate empty candidates result (or skip
the block) so no unnecessary network request is made when there are no line IDs
to query.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 933e6ea1-7168-4319-8456-4472f6e9a205
📒 Files selected for processing (2)
src/hooks/useRouteEstimation.tssrc/lib/graphql/queries.ts
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/hooks/useRouteEstimation.test.tsx (1)
119-298: N+1解消の回帰を防ぐ検証が不足していますこのPRの主目的(
lineListStationsへの一括取得移行)に対して、呼び出し回数・引数(lineIds)・結果の路線別振り分けを直接検証するアサーションがありません。ここを1ケース追加しておくと、将来の退行を確実に検出できます。🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/hooks/useRouteEstimation.test.tsx` around lines 119 - 298, Add a test that verifies the batched fetch to lineListStations is called once with the aggregated lineIds and that returned stations are correctly mapped per line to prevent N+1 regressions: mock the lineListStations query used in setupQueries (or the module exposing lineListStations), prepare multiple lines and expected station lists keyed by line id, call the hook via HookBridge (using setupAtoms/setupQueries as in other tests) to trigger the fetch (e.g. start estimation or whatever action in useRouteEstimation causes lineListStations to run), then assert the mocked lineListStations was called exactly once with the combined lineIds and that the hook result (hookRef.current?.candidates or the state updated by selectCandidate) receives stations correctly partitioned by line id. Ensure you reference setupQueries, lineListStations, HookBridge and useRouteEstimation in the test so future changes will fail if N+1 behavior is reintroduced.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/hooks/useRouteEstimation.test.tsx`:
- Around line 86-89: The cyclic mock for mockUseSetAtom (using setters and
setterIndex) should be tightened so extra calls fail instead of wrapping via
modulus; change the mockImplementation for mockUseSetAtom to return
setters[setterIndex] only while setterIndex < setters.length and otherwise throw
an error (increment setterIndex after returning), and apply the same stricter
behavior to the mockUseLazyQuery mock (the analogous arrays/indices used at
lines ~107-109) so any unexpected call count causes the test to fail rather than
silently reuse mocks.
---
Nitpick comments:
In `@src/hooks/useRouteEstimation.test.tsx`:
- Around line 119-298: Add a test that verifies the batched fetch to
lineListStations is called once with the aggregated lineIds and that returned
stations are correctly mapped per line to prevent N+1 regressions: mock the
lineListStations query used in setupQueries (or the module exposing
lineListStations), prepare multiple lines and expected station lists keyed by
line id, call the hook via HookBridge (using setupAtoms/setupQueries as in other
tests) to trigger the fetch (e.g. start estimation or whatever action in
useRouteEstimation causes lineListStations to run), then assert the mocked
lineListStations was called exactly once with the combined lineIds and that the
hook result (hookRef.current?.candidates or the state updated by
selectCandidate) receives stations correctly partitioned by line id. Ensure you
reference setupQueries, lineListStations, HookBridge and useRouteEstimation in
the test so future changes will fail if N+1 behavior is reintroduced.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 5a903ed3-d8c3-4671-b7d8-17ac77cb7f93
📒 Files selected for processing (1)
src/hooks/useRouteEstimation.test.tsx
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/hooks/useRouteEstimation.test.tsx (1)
100-127:lineListStations一括取得の回帰を直接検証するケースを1件追加したいです。現状は
useLazyQueryの呼び出し順と状態遷移は検証できていますが、今回の主目的である「候補路線をまとめて1回取得し、返却駅を路線IDごとに振り分ける」挙動を直接アサートできていません。ここを1ケース足すと、N+1再発の検知力が上がります。As per coding guidelines:
**/*.{test,spec}.{ts,tsx,js,jsx,py,java,go,cs}: Test agents in isolation and with integration tests to ensure correctness and compatibility.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/hooks/useRouteEstimation.test.tsx` around lines 100 - 127, The test setup in setupQueries (mockFetchNearbyStart, mockFetchNearbyEnd, mockFetchLineListStations and mockUseLazyQuery) lacks a concrete case that asserts lineListStations are fetched once in batch and that returned stations are partitioned by line ID; add a new test that configures mockFetchLineListStations to return a combined list of stations with multiple lineId values, invoke the hook (useRouteEstimation) flow to trigger all lazy queries, then assert mockFetchLineListStations was called exactly once (no N+1) and that the hook's result assigns stations to each route/lineId correctly (verify counts/contents per lineId), and also assert mockFetchNearbyStart/mockFetchNearbyEnd call expectations remain unchanged. Ensure you reference the existing helpers (setupQueries, mockUseLazyQuery, mockFetchLineListStations) when implementing the test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/hooks/useRouteEstimation.test.tsx`:
- Around line 100-127: The test setup in setupQueries (mockFetchNearbyStart,
mockFetchNearbyEnd, mockFetchLineListStations and mockUseLazyQuery) lacks a
concrete case that asserts lineListStations are fetched once in batch and that
returned stations are partitioned by line ID; add a new test that configures
mockFetchLineListStations to return a combined list of stations with multiple
lineId values, invoke the hook (useRouteEstimation) flow to trigger all lazy
queries, then assert mockFetchLineListStations was called exactly once (no N+1)
and that the hook's result assigns stations to each route/lineId correctly
(verify counts/contents per lineId), and also assert
mockFetchNearbyStart/mockFetchNearbyEnd call expectations remain unchanged.
Ensure you reference the existing helpers (setupQueries, mockUseLazyQuery,
mockFetchLineListStations) when implementing the test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: beb700eb-cc10-4f8b-b327-ea3892c03165
📒 Files selected for processing (1)
src/hooks/useRouteEstimation.test.tsx
Summary
useRouteEstimationで候補路線の駅リスト取得にlineStationsを路線数分ループ呼び出し(N+1)していたのを、lineListStationsによる一括取得に変更GET_LINE_LIST_STATIONSクエリ(StationFieldsフラグメント使用)を新規追加CandidateLine[]を構築Test plan
npm run typecheckパス済み🤖 Generated with Claude Code
Summary by CodeRabbit