recently started playing with the schedule and Apollo - apollo-client.
I built a web service on top of the graph and it works just fine. The only problem I encountered is the client side of the project. For example (see the code below), after running createVideo (), the property of datamy component, which is observable, that watches the request, is not updated automatically, and calling apollo.query manually on the callback does not look to get any effect, because the request returns cached results, not the ones indicated on the server.
Did I miss something?
app.component.ts
import {Component, OnInit} from '@angular/core';
import {Apollo, ApolloQueryObservable} from 'apollo-angular';
import 'rxjs/Rx';
import gql from 'graphql-tag';
const NewVideoQuery = gql`
mutation AddVideoQuery($title: String!,$duration: Int!, $watched: Boolean!){
createVideo(video: { title: $title, duration: $duration, watched: $watched } ){
id,
title
}
}
`;
const VideoQuery = gql`
{
videos {
id,
title
}
}
`;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
data: ApolloQueryObservable<any>;
video: any = {};
constructor(private apollo: Apollo) {
}
ngOnInit() {
this.data = this.apollo.watchQuery({query: VideoQuery});
}
createVideo() {
this.apollo.mutate({
mutation: NewVideoQuery,
variables: {
'title': this.video.title || 'Some Video' + Math.floor(Math.random() * 10),
'duration': 123213,
'watched': true
}
}).subscribe((afterMutation) => {
console.log(afterMutation);
this.apollo.query({query: VideoQuery})
.subscribe((data) => {
console.log(data);
});
}, (err) => alert(err));
}
}
//app.component.html
<div *ngFor="let x of data | async | select: 'videos'">
<div><b>{{x.id}}</b>{{x.title}}</div>
</div>
<label>
Title
<input type="text" [(ngModel)]="video.title">
</label>
<button (click)="createVideo()">{{title}}</button>
source
share