tRPC 类型安全的 API 层 has been discussed many times in the community, but as versions iterate, many conclusions need updating. This article revisits the topic based on the latest version.
Getting Started
Building on this foundation, we can further optimize:
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] }) }
}
}
}
})
})
This pattern is very practical in large projects and can significantly reduce maintenance costs.
Source Code Analysis
实际项目中的用法会更复杂一些:
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] }) }
}
}
}
})
})
Through this approach, both the testability and scalability of the code are improved.
Real-World Applications
Here is a complete example:
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] }) }
}
}
}
})
})
Pay attention to boundary condition handling, which is critical in production.
Optimization Tips
The key lies in understanding the core logic:
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] }) }
}
}
}
})
})
Performance optimization should be tailored to specific scenarios; not all cases require over-optimization.
Pitfall Guide
We can improve it in the following ways:
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] }) }
}
}
}
})
})
This approach has been running stably in production for over six months and has been practically validated.
Summary
- Code examples are for reference only and need to be adjusted according to your business scenario
- tRPC 类型安全的 API 层不是银弹,需要根据项目规模和技术栈选择
- Understanding underlying principles is more important than memorizing APIs
- Always verify compatibility before using in production