问题描述
我正在为我们的项目之一使用Angular Apollo.我正在将apollo客户端创建为:
this.apollo.create({
link: this.httpLink.create({ uri: `${environment.apiBase}/graphql?access_token=${this.tokenService.token}`}),
cache: new InMemoryCache()
})
由于我们的uri有一个访问令牌,该令牌也会在某个时间点后过期,因此我们必须刷新它并获取新的访问令牌,从而得到一个新的uri.
我无法在apollo文档中找到任何有关更新uri的方法(也许我错过了吗?),如果我创建了这样的新实例,也会抛出一个错误,提示apollo客户端已经存在礼物.
有什么方法可以更新uri吗?
我知道它不是Apollo Angular文档的一部分(欢迎PR!),但我在apollo-angular-link-http
的自述文件中对此进行了描述.
有两种方法:
使用Apollo链接拦截查询并在上下文中设置uri
属性
const authMiddleware = setContext((operation, { uri }) => {
return refreshToken().then(res => ({
uri: this.getURI()
})
}))
或者使用Angular的HttpClient拦截器拦截请求并更改端点.
I'm using Angular Apollo for one of our project. I'm creating the apollo client as:
this.apollo.create({
link: this.httpLink.create({ uri: `${environment.apiBase}/graphql?access_token=${this.tokenService.token}`}),
cache: new InMemoryCache()
})
Since our uri has an access token which also gets expired after a certain point of time, we had to refresh it and get the new access token, hence a new uri.
I was unable find any method in apollo docs regarding the way to update the uri(maybe I've missed it out?), also if I create a new instance like that, it throws me an error that apollo client is already present.
Is there any way to update the uri?
I know it's not a part of Apollo Angular documentation (PR welcome!) but I described it in the README of apollo-angular-link-http
.
There are two ways:
Use an Apollo Link to intercept the query and set uri
property on the context
const authMiddleware = setContext((operation, { uri }) => {
return refreshToken().then(res => ({
uri: this.getURI()
})
}))
Or intercept the request with Angular's HttpClient interceptor and change the endpoint.
这篇关于更新Apollo客户端实例的uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!