AgentSkillsCN

graphql-client-patterns

在前端应用中集成 GraphQL API 时使用。内容涵盖客户端选型、代码生成、缓存规范化、乐观更新,以及订阅功能。

SKILL.md
--- frontmatter
name: graphql-client-patterns
description: "Use when integrating GraphQL APIs in frontend apps. Covers client selection, code generation, cache normalization, optimistic updates, and subscriptions."

GraphQL Client Patterns

Client Selection

CriteriaApollo Clienturqlgraphql-requestRelay
CacheNormalizedDocument/normalizedNoneNormalized
Bundle size~35kb~8kb~3kb~30kb
SSRBuilt-inBuilt-inManualBuilt-in
SubscriptionsYesYesNoYes
Learning curveMediumLowMinimalHigh
Best forFull-featured appsBalanced needsSimple fetchingMeta-scale apps

Default: Apollo for complex apps with cache needs, urql for lighter footprint, graphql-request for simple query-only use cases.

Code Generation

Use @graphql-codegen/cli with client preset. Define schema URL + document glob. Colocate .graphql files with features. Generated types flow into useQuery(DocumentNode).

Cache Normalization (Apollo)

Apollo InMemoryCache with typePolicies: define keyArgs for paginated fields, custom merge for appending, computed read fields.

Optimistic Updates

Pass optimisticResponse to useMutation. In update, use cache.modify to insert the optimistic entry. Rollback onError with snapshotted previous data.

Subscription Handling

Same cache.modify pattern as optimistic updates. Use onData callback to merge incoming subscription data into cache.

Gotchas

  • N+1 on server: Use DataLoader per-request; client cannot solve this
  • Cache invalidation: refetchQueries is simpler than manual cache.modify -- use it unless perf-critical
  • Fragment colocation: Keep fragments next to components that consume them; avoids stale field selections
  • Over-fetching: Use @defer directive for heavy fields; split queries by viewport priority
  • SSR hydration mismatch: Extract and rehydrate cache state; Apollo's getDataFromTree or urql's ssrExchange

Cross-References

  • architecture:api-design-principles -- GraphQL schema design and API conventions
  • architecture:api-client-sdk-design -- Client abstraction layers and retry/error handling