Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. onSave(): void {
  2. let variables = {};
  3. let mutation = {};
  4. let msg = "";
  5.  
  6. if (this.model.id) {
  7. // update
  8. variables = {
  9. id: this.model.id,
  10. name: this.model.name,
  11. genre: this.model.genre,
  12. authorId: this.model.authorId
  13. };
  14. mutation = updateBookMutation;
  15. msg = "Book updated";
  16. } else {
  17. // create
  18. variables = {
  19. name: this.model.name,
  20. genre: this.model.genre,
  21. authorId: this.model.authorId
  22. };
  23. mutation = addBookMutation;
  24. msg = "Book Added";
  25. }
  26.  
  27. this.apollo.mutate({
  28. mutation: mutation,
  29. variables:variables,
  30. refetchQueries: [{
  31. query: getBooksQuery
  32. }]
  33. }).pipe(
  34. map ( results => mutation === updateBookMutation ? results.data['updateBook'] : results.data['addBook'] )
  35. ).subscribe( ({ id, name }) => {
  36. console.log(`${msg}:\n -Id (${id}) \n -Name (${name})`);
  37. });
  38. this.reset();
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement