【发布时间】:2019-07-05 03:53:53
【问题描述】:
我有一个从 GraphQL 服务器访问数据的 Angular 7 应用程序。
我直接实现了文档中的示例: https://www.apollographql.com/docs/react/advanced/caching.html#automatic-updates
这是获取 post 对象并执行 upvote 突变的服务。
export class PostService {
constructor(private apollo: Apollo) { }
public getPost() {
return this.apollo.query({
query: gql`query getPost($id: String!) {
post(id: $id) {
id
score
}
}`,
variables: { id: '123' }
});
}
public upvote() {
return this.apollo.mutate({
mutation: gql`mutation upvote($id: String!) {
upvotePost(id: $id) {
id
score
}
}`,
variables: { id: '123' }
});
}
}
在我的 component.ts 文件中
public post = this.postService.getPost();
public vote() {
this.postService.upvote().subscribe(console.log);
}
在我的 component.html 中
<input type="text" [value]="(post | async).data.post.score" />
<button class="button" (click)="vote()"></button>
框中的值不会改变。
如果我添加一个额外的按钮来调用这个函数
public updateView() {
post = this.postService.getPost();
}
gui 将在不查询服务器的情况下更新,因此很明显来自缓存。
根据规范,此手动刷新步骤是不必要的。
If the id field on both results matches up, then the score field everywhere in our UI will be updated automatically!
我的包的版本:
- 阿波罗角度:1.5.0
- apollo-angular-link-http:1.4.0
- apollo-angular-link-http-common: 1.4.0
- 阿波罗缓存:1.1.25
- apollo-cache-inmemory:1.4.2
- apollo 客户端:2.4.12
我需要更改什么,以便在原始请求返回的 observable 中实际更新结果?
或者我只是不了解预期的机制?
【问题讨论】:
-
你正在编写一个 Angular 应用程序,但你已经发布了 React Apollo 客户端的文档?
-
既然你提到它... :(