|
1 | 1 | import { Injectable } from '@angular/core';
|
2 |
| -import { HttpClient, HttpHeaders } from '@angular/common/http'; |
3 |
| -import { Apollo } from 'apollo-angular'; |
4 |
| -import { HttpLink } from 'apollo-angular-link-http'; |
5 |
| -import { InMemoryCache } from 'apollo-cache-inmemory'; |
6 | 2 | import gql from 'graphql-tag';
|
7 |
| - |
8 | 3 | import { Observable } from 'rxjs/Observable';
|
9 | 4 | import { of } from 'rxjs/observable/of';
|
10 |
| -import { catchError, map, tap } from 'rxjs/operators'; |
11 |
| - |
| 5 | +import { map, tap } from 'rxjs/operators'; |
12 | 6 | import { Hero } from './hero';
|
13 |
| -import { MessageService } from './message.service'; |
14 |
| -import { GraphQLService, GQLOptions } from './graphql.service'; |
15 |
| - |
16 |
| -const options: GQLOptions = { |
17 |
| - readAll: gql`query readAll{allHeroes{nodes{id,name}}}`, |
18 |
| - readById: gql`query readById($id:Int!){heroById(id:$id){id,name}}`, |
19 |
| - readWithTerm: gql`query readWithTerm($term:String!){allHeroes(term:$term){nodes{id,name}}}`, |
20 |
| - create: gql`mutation create($name:String!) |
21 |
| - {createHero(input:{clientMutationId:"toh-createHero",hero:{name:$name}}) |
22 |
| - {clientMutationId,hero{id,name}}}`, |
23 |
| - update: gql`mutation update($id:Int!,$name:String!) |
24 |
| - {updateHeroById(input:{clientMutationId:"toh-updateHero",id:$id,heroPatch:{name:$name}}) |
25 |
| - {clientMutationId,hero{id,name}}}`, |
26 |
| - delete: gql`mutation delete($id:Int!) |
27 |
| - {deleteHeroById(input:{clientMutationId:"toh-deleteHero",id:$id}) |
28 |
| - {clientMutationId,hero{id,name}}}`, |
29 |
| - deleteById: gql`mutation deleteById($id:Int!) |
30 |
| - {deleteHeroById(input:{clientMutationId:"toh-deleteHeroById",id:$id}) |
31 |
| - {clientMutationId,hero{id,name}}}` |
32 |
| -}; |
| 7 | +import { GraphQLService } from './graphql.service'; |
33 | 8 |
|
34 | 9 | @Injectable()
|
35 | 10 | export class HeroService {
|
36 | 11 |
|
37 | 12 | constructor(
|
38 |
| - private graphQLService: GraphQLService |
| 13 | + private gqlService: GraphQLService |
39 | 14 | ) { }
|
40 | 15 |
|
41 | 16 | /** Get all heroes from the server */
|
42 | 17 | getHeroes (): Observable<Hero[]> {
|
43 |
| - return this.graphQLService.readAll<{allHeroes:{nodes:Hero[]}}>(options).pipe( |
44 |
| - map((data) => data.allHeroes.nodes)) |
45 |
| - } |
| 18 | + return this.gqlService.query<{allHeroes:{nodes:Hero[]}}>( |
| 19 | + gql`query readAllHeroes{allHeroes{nodes{id,name}}}` |
| 20 | + ).pipe(map((data) => data.allHeroes.nodes))} |
46 | 21 |
|
47 | 22 | /** Get a hero by id. Will 404 if id not found */
|
48 | 23 | getHero(id: number): Observable<Hero> {
|
49 |
| - return this.graphQLService.readById<{heroById:Hero}>(options, id).pipe( |
50 |
| - map((data) => data.heroById)) |
51 |
| - } |
| 24 | + return this.gqlService.query<{heroById:Hero}>( |
| 25 | + gql`query readHeroById($id:Int!){heroById(id:$id){id,name}}`, |
| 26 | + {'id': id}, |
| 27 | + 'readHeroById' |
| 28 | + ).pipe(map((data) => data.heroById))} |
52 | 29 |
|
53 | 30 | /* Get all heroes whose name contains search term */
|
54 | 31 | searchHeroes(term: string): Observable<Hero[]> {
|
55 | 32 | if (!term.trim()) {return of([]);} // if not search term, return empty hero array.
|
56 |
| - return this.graphQLService.readWithTerm<{allHeroes:{nodes:Hero[]}}>(options, term).pipe( |
57 |
| - map((data) => data.allHeroes.nodes)) |
58 |
| - } |
| 33 | + return this.gqlService.query<{herowithterm:{nodes:Hero[]}}>( |
| 34 | + gql`query readHeroesWithTerm($term:String!){herowithterm(term:$term){nodes{id,name}}}`, |
| 35 | + {'term': term}, |
| 36 | + 'readHerosWithTerm' |
| 37 | + ).pipe(map((data) => data.herowithterm.nodes))} |
59 | 38 |
|
60 | 39 | //////// Save methods //////////
|
61 | 40 |
|
62 | 41 | /** Add a new hero to the server */
|
63 | 42 | addHero (hero: Hero): Observable<Hero> {
|
64 |
| - return this.graphQLService.create<{createHero:{hero:Hero}}>(options, hero).pipe( |
65 |
| - map((data) => data.createHero.hero)) |
66 |
| - } |
| 43 | + return this.gqlService.mutate<{createHero:{hero:Hero}}>( |
| 44 | + gql`mutation create($name:String!) |
| 45 | + {createHero(input:{hero:{name:$name}}) |
| 46 | + {hero{id,name}}}`, |
| 47 | + hero, 'create').pipe(map((data) => data.createHero.hero))} |
67 | 48 |
|
68 | 49 | /** Delete the hero from the server */
|
69 | 50 | deleteHero (hero: Hero | number): Observable<Hero> {
|
70 | 51 | const id = typeof hero === 'number' ? hero : hero.id;
|
71 |
| - return this.graphQLService.delete<{deleteHeroById:{hero:Hero}}>(options, {"id": id}).pipe( |
72 |
| - map((data) => data.deleteHeroById.hero)) |
73 |
| - } |
74 |
| - |
| 52 | + return this.gqlService.mutate<{deleteHeroById:{hero:Hero}}>( |
| 53 | + gql`mutation delete($id:Int!) |
| 54 | + {deleteHeroById(input:{id:$id}) |
| 55 | + {hero{id,name}}}`, |
| 56 | + {'id': id}, 'delete').pipe(map((data) => data.deleteHeroById.hero))} |
75 | 57 |
|
76 | 58 | /** Update the hero on the server */
|
77 | 59 | updateHero (hero: Hero): Observable<Hero> {
|
78 |
| - return this.graphQLService.update<{updateHeroById:{hero:Hero}}>(options, hero).pipe( |
79 |
| - map((data) => data.updateHeroById.hero)) |
80 |
| - } |
| 60 | + return this.gqlService.mutate<{updateHeroById:{hero:Hero}}>( |
| 61 | + gql`mutation update($id:Int!,$name:String!) |
| 62 | + {updateHeroById(input:{id:$id,heroPatch:{name:$name}}) |
| 63 | + {hero{id,name}}}`, |
| 64 | + hero, 'update').pipe(map((data) => data.updateHeroById.hero))} |
81 | 65 |
|
82 | 66 | }
|
0 commit comments