Skip to content

Apollo ClientをTanStack Query + graphql-requestへ置き換えてAPI通信レイヤーを軽量化 - #6210

Merged
TinyKitten merged 3 commits into
devfrom
claude/apollo-client-alternatives-dnm0u8
Jun 12, 2026
Merged

TinyKitten merged 3 commits into
devfrom
claude/apollo-client-alternatives-dnm0u8

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Jun 12, 2026 •

Copy link
Copy Markdown
Member

概要

API通信に使用していたApollo Clientを廃止し、TanStack Query + graphql-request構成へ移行しました。本アプリのGraphQL利用はクエリのみ(mutation/subscription/正規化キャッシュ未使用、typePolicies で keyFields: false を全指定済み)でApolloがオーバースペックだったため、より軽量な構成に置き換えています。

変更の種類

  • バグ修正
  • 新機能
  • リファクタリング
  • ドキュメント
  • CI/CD
  • その他

変更内容

  • 依存関係: @apollo/client を削除し、@tanstack/react-query / graphql-request@^6 / graphql-tag を追加
  • src/lib/gql.ts: GraphQLClient + QueryClient に置き換え。staleTime: Infinity でApolloのcache-firstと等価なキャッシュ挙動を維持。フック外からの命令的フェッチ用に gqlClient.query() 互換ファサードを維持(usePresetCarouselData / useStationsCache とそのテストは無変更)
  • 新規フック useLazyGraphQLQuery / useGraphQLQuery: Apolloの useLazyQuery / useQuery と同一のAPI形状をTanStack Query上で再現(executeはエラー時もrejectせず { data, error } で解決する契約を維持)。ユニットテスト9件を追加
  • 呼び出し箇所8ファイル(フック5・画面2・コンポーネント1)はimport行とフック名の置き換えのみ。RouteSearchScreen の cache.evict() + cache.gc() は queryClient.removeQueries() に置換
  • src/lib/graphql/queries.ts: gql タグのimport元を graphql-tag に変更(入れ子フラグメントの重複定義排除のため)
  • src/index.tsx: ApolloProvider → QueryClientProvider
  • 既存テスト4本のモック先を @apollo/client/react → ~/hooks/useLazyGraphQLQuery に変更(テストロジックは無変更)

リグレッションリスクと対策

  • キャッシュ戦略はApolloの cache-first と等価な設定(staleTime: Infinity)で挙動を維持
  • useLazyQuery のexecute契約(エラー時にrejectしない)を維持し、呼び出し側のエラーハンドリングへの影響を排除
  • 全1872テストのパスで回帰を確認

テスト

  • npm run lint が通ること
  • npm test が通ること
  • npm run typecheck が通ること

ローカルで npm run lint && npm test && npm run typecheck を実行し、177スイート・1872テスト全件パスを確認済み。

関連Issue

スクリーンショット(任意)

UI変更なし

https://claude.ai/code/session_01GdNc8HmHmMaYZk5G62hUah


Generated by Claude Code

Summary by CodeRabbit

  • Chores
    • Updated internal data fetching infrastructure to improve system maintainability and performance.
    • Refactored existing GraphQL query implementations across station search, route search, and train type selection features.

@github-actions github-actions Bot added the react label Jun 12, 2026
@coderabbitai

coderabbitai Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@TinyKitten, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 1 minute and 31 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 85e2ea94-5258-4074-9114-b7a2443f4e0c

📥 Commits

Reviewing files that changed from the base of the PR and between 29e0aa7 and 3e5cac7.

📒 Files selected for processing (1)
  • src/components/StationSearchModal.tsx
📝 Walkthrough

Walkthrough

Apollo Client から React Query + graphql-request へのデータ取得層を置き換え、新しいカスタムフック useGraphQLQuery(宣言的)と useLazyGraphQLQuery(命令的)を実装し、全 UI コンポーネント・カスタムフック・スクリーンのクエリ呼び出しを統一的に移行。

Changes

Apollo Client から React Query への GraphQL データ取得層の全面置き換え

Layer / File(s) Summary
依存関係と GraphQL リクエスト基盤の更新
package.json, src/lib/gql.ts, src/lib/graphql/queries.ts
@apollo/client を削除し @tanstack/react-query・graphql-request・graphql-tag を追加。src/lib/gql.ts を完全書き換えし GraphQLClient・queryClient・gqlRequest・graphqlQueryKey を実装。
宣言的フェッチ useGraphQLQuery の実装と検証
src/hooks/useGraphQLQuery.ts, src/hooks/useGraphQLQuery.test.tsx
DocumentNode と変数を受け取り react-query の useQuery で自動再フェッチ・skip 制御・data/loading/error 返却を実装、マウント・変数変更・エラー時のテスト検証。
命令的フェッチ useLazyGraphQLQuery の実装と検証
src/hooks/useLazyGraphQLQuery.ts, src/hooks/useLazyGraphQLQuery.test.tsx
execute 関数による明示的リクエスト・成功/失敗の { data/error } 返却・並行リクエスト順序ガード・variables 差分時の再フェッチ・キャッシュ動作を実装・検証。
新フックの公開エクスポート
src/hooks/index.ts
useGraphQLQuery と useLazyGraphQLQuery を再エクスポート。
アプリケーションルート QueryClientProvider への置き換え
src/index.tsx
ApolloProvider から QueryClientProvider へプロバイダを置き換え、ルート側に配置。
UI コンポーネント・カスタムフック・スクリーン画面の移行
src/components/StationSearchModal.tsx, src/hooks/useDeepLink.ts, src/hooks/useDeepLink.test.tsx, src/hooks/useFetchNearbyStation.ts, src/hooks/useFetchNearbyStation.test.tsx, src/hooks/useLineSelection.ts, src/hooks/useLineSelection.test.tsx, src/hooks/useTrainTypeModal.ts, src/hooks/useTrainTypeModal.test.tsx, src/hooks/useTrainTypeStations.ts, src/screens/Main.tsx, src/screens/RouteSearchScreen.tsx
Apollo の useQuery/useLazyQuery を新フックに置き換え、型パラメータ・destructuring を調整。RouteSearchScreen ではキャッシュクリア処理を Apollo の evict/gc から react-query の removeQueries に変更。テストはモック対象を新フックへ統一。

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • TrainLCD/MobileApp#6003: 本 PR の useDeepLink における Apollo → useLazyGraphQLQuery 移行に伴い、GET_STATIONS_BY_IDS 利用や deep link パラメータ解釈ロジックが変更され、retrieved PR の同ファイルの sids 分岐処理と同じコード領域で修正が発生しています。

Suggested labels

react

Poem

🐰 クエリフック、Apollo を去りて React Query へ、
宣言と命令、二つの道を歩み出し、
キャッシュ戻る季、駅の情報も再び新しく、
型安全に包まれたデータの旅、ぴょんぴょん進む!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed プルリクエストのタイトルは、メインの変更内容(Apollo ClientをTanStack Query + graphql-requestへの置き換え)を明確かつ簡潔に要約しており、changeset全体の主要な目的を正確に反映しています。
Description check ✅ Passed プルリクエストの説明は、リポジトリの説明テンプレートのすべての必須セクション(概要、変更の種類、変更内容、テスト、関連Issue)を適切に記入しており、変更の背景、詳細な実装内容、テスト実行結果が十分に説明されています。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/apollo-client-alternatives-dnm0u8

Comment @coderabbitai help to get the list of available commands and usage tips.

@TinyKitten TinyKitten self-assigned this Jun 12, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/components/StationSearchModal.tsx (1)

200-204: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

useEffect 内での Alert.alert 直接呼び出しを避けてください。

StrictMode 環境では useEffect が開発時に再実行されるため、エラー発生時にアラートが重複表示される可能性があります。コーディングガイドラインに従い、src/utils/alertPresentation.ts の共有 guard を使用してください。

コーディングガイドラインより: "Do not call Alert.alert directly from useEffect or from async functions launched by useEffect under StrictMode; use the shared alert presentation guard in src/utils/alertPresentation.ts"

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/StationSearchModal.tsx` around lines 200 - 204, The useEffect
currently calls Alert.alert directly (see useEffect block referencing
fetchStationsByNameError and fetchStationsNearbyError); replace this direct call
with the shared alert presentation guard exported from
src/utils/alertPresentation.ts by importing the guard and invoking it when
fetchStationsByNameError or fetchStationsNearbyError becomes truthy, so alerts
are presented via the centralized guard instead of Alert.alert inside useEffect.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/components/StationSearchModal.tsx`:
- Around line 200-204: The useEffect currently calls Alert.alert directly (see
useEffect block referencing fetchStationsByNameError and
fetchStationsNearbyError); replace this direct call with the shared alert
presentation guard exported from src/utils/alertPresentation.ts by importing the
guard and invoking it when fetchStationsByNameError or fetchStationsNearbyError
becomes truthy, so alerts are presented via the centralized guard instead of
Alert.alert inside useEffect.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d1e6d18d-f800-4c15-bd3c-b3c3a0fe7b7d

📥 Commits

Reviewing files that changed from the base of the PR and between 7d7dd4c and 29e0aa7.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (21)
  • package.json
  • src/components/StationSearchModal.tsx
  • src/hooks/index.ts
  • src/hooks/useDeepLink.test.tsx
  • src/hooks/useDeepLink.ts
  • src/hooks/useFetchNearbyStation.test.tsx
  • src/hooks/useFetchNearbyStation.ts
  • src/hooks/useGraphQLQuery.test.tsx
  • src/hooks/useGraphQLQuery.ts
  • src/hooks/useLazyGraphQLQuery.test.tsx
  • src/hooks/useLazyGraphQLQuery.ts
  • src/hooks/useLineSelection.test.tsx
  • src/hooks/useLineSelection.ts
  • src/hooks/useTrainTypeModal.test.tsx
  • src/hooks/useTrainTypeModal.ts
  • src/hooks/useTrainTypeStations.ts
  • src/index.tsx
  • src/lib/gql.ts
  • src/lib/graphql/queries.ts
  • src/screens/Main.tsx
  • src/screens/RouteSearchScreen.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants