深色模式
tRPC 类型安全的 API 层这个话题社区讨论了很多次,但随着版本迭代,很多结论需要更新。本文基于最新版本重新梳理。
入门指南
在这个基础上,我们可以进一步优化:
javascript
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client'
import { setContext } from '@apollo/client/link/context'
const httpLink = createHttpLink({ uri: '/graphql' })
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem('token')
return { headers: { ...headers, authorization: `Bearer ${token}` } }
})
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
users: { keyArgs: ['filter'], merge: (e, i) => ({ ...i, edges: [...(e?.edges||[]), ...i.edges] }) }
}
}
}
})
})这种模式在大型项目中非常实用,能显著降低维护成本。
源码分析
实际项目中的用法会更复杂一些:
javascript
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client'
import { setContext } from '@apollo/client/link/context'
const httpLink = createHttpLink({ uri: '/graphql' })
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem('token')
return { headers: { ...headers, authorization: `Bearer ${token}` } }
})
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
users: { keyArgs: ['filter'], merge: (e, i) => ({ ...i, edges: [...(e?.edges||[]), ...i.edges] }) }
}
}
}
})
})通过这种方式,代码的可测试性和可扩展性都得到了提升。
真实场景应用
以下是一个完整的示例:
javascript
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client'
import { setContext } from '@apollo/client/link/context'
const httpLink = createHttpLink({ uri: '/graphql' })
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem('token')
return { headers: { ...headers, authorization: `Bearer ${token}` } }
})
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
users: { keyArgs: ['filter'], merge: (e, i) => ({ ...i, edges: [...(e?.edges||[]), ...i.edges] }) }
}
}
}
})
})注意边界条件处理,这在生产环境中至关重要。
优化技巧
关键在于理解核心逻辑:
javascript
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client'
import { setContext } from '@apollo/client/link/context'
const httpLink = createHttpLink({ uri: '/graphql' })
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem('token')
return { headers: { ...headers, authorization: `Bearer ${token}` } }
})
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
users: { keyArgs: ['filter'], merge: (e, i) => ({ ...i, edges: [...(e?.edges||[]), ...i.edges] }) }
}
}
}
})
})性能优化需要结合具体场景,不是所有情况都需要过度优化。
避坑指南
我们可以通过以下方式来改进:
javascript
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client'
import { setContext } from '@apollo/client/link/context'
const httpLink = createHttpLink({ uri: '/graphql' })
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem('token')
return { headers: { ...headers, authorization: `Bearer ${token}` } }
})
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
users: { keyArgs: ['filter'], merge: (e, i) => ({ ...i, edges: [...(e?.edges||[]), ...i.edges] }) }
}
}
}
})
})这套方案已经在线上稳定运行了半年以上,经过了实际验证。
小结
- 代码示例仅供参考,需根据业务场景调整
- tRPC 类型安全的 API 层不是银弹,需要根据项目规模和技术栈选择
- 理解底层原理比记住 API 更重要
- 生产环境使用前务必做好兼容性验证