Apollo Client 4.0:更精簡嘅 GraphQL 客戶端

Back
Category : News

Apollo Client 4.0 係一個好重大嘅更新,專注減低 bundle size,提升 TypeScript 支持,同埋改善開發者體驗。
呢個發布引入 opt-in 架構,令 features 如 local state management 同 HttpLink 變成 optional,咁樣可以排除唔用嘅 code,減低 bundle size。
現代 build targets 針對 2023 年後嘅 browser 同 Node.js 20+,利用 native language features,改善 tree-shaking,達到 20-30% 減低 bundle size,對追求 performance budget 嘅 team 嚟講好有幫助。
TypeScript 支持已經大翻新,types 而家可以同 APIs 一齊發現,透過 namespaces 如 useQuery.Options 同 useQuery.Result。
更新強制 required variables 喺 type level,取代脆弱嘅 TContext generic,用 module augmentation 嚟 custom context types,仲引入 TypeOverrides 嚟 customize partial data 同 streaming responses,維持 type safety。
新嘅 dataState property 帶有 states 如 empty、partial、streaming 同 complete,簡化 TypeScript type narrowing,減低 runtime errors。
錯誤處理更加直觀,取代 monolithic ApolloError 用 specific error classes 如 CombinedGraphQLErrors,清楚區分 GraphQL、network 同 parsing errors。
Hooks 如 useLazyQuery 有更清楚嘅 option boundaries,loading state 而家反映 refetches,因為 notifyOnNetworkStatusChange default 係 true。
呢個發布擁抱現代 JavaScript 標準,由 zen-observable 遷移到 RxJS 作為 peer dependency,提供 access 到 RxJS operators 同 debugging tools。
ESM-first packaging 改善 bundler optimization、dead code elimination 同 source maps,特別係用現代 bundlers 如 Vite 同 Webpack 5。
React-specific exports 而家分離到 @apollo/client/react,確保 top-level exports 係 framework-agnostic。
遷移到 Apollo Client 4.0 有全面 codemod 支持(npx @apollo/client-codemod-migrate-3-to-4 src),自動化 90% 變化對大部分 app 嚟講,仲有 migration guide 同 changelog 提供剩餘 intentional breaking changes。https://www.apollographql.com/blog/announcing-apollo-client-4-0