@@ -11,123 +11,72 @@ import { catchError, map, tap } from 'rxjs/operators';
11
11
12
12
import { Hero } from './hero' ;
13
13
import { MessageService } from './message.service' ;
14
- import { validateConfig } from '@angular/router/src/config' ;
15
-
16
- const heroesUrl = 'http://localhost:5000/graphql' ; // URL to web api
17
- const allHeroes = gql `query tohAllHero{allHeroes{nodes{id,name}}}` ;
18
- const heroById = gql `query tohHeroByID($id:Int!){heroById(id:$id){id,name}}` ;
19
- const heroWithTerm = gql `query tohHeroWithTerm($term:String!){herowithterm(term:$term){nodes{id,name}}}` ;
20
- const createHero = gql `mutation tohCreateHero($name:String!){createHero(input:{clientMutationId:"toh-createHero",hero:{name:$name}}){clientMutationId,hero{id,name}}}` ;
21
- const updateHero = gql `mutation tohUpdateHeroById($id:Int!,$name:String!){updateHeroById(input:{clientMutationId:"toh-updateHero",id:$id,heroPatch:{name:$name}}){clientMutationId,hero{id,name}}}` ;
22
- const deleteHero = gql `mutation tohDeleteHeroById($id:Int!){deleteHeroById(input:{clientMutationId:"toh-deleteHero",id:$id}){clientMutationId,hero{id,name}}}` ;
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
+ } ;
23
33
24
34
@Injectable ( )
25
35
export class HeroService {
26
36
27
37
constructor (
28
- private messageService : MessageService ,
29
- private apollo : Apollo ,
30
- private httpLink : HttpLink
31
- ) {
32
- apollo . create ( {
33
- link : httpLink . create ( { uri : heroesUrl } ) ,
34
- cache : new InMemoryCache ( )
35
- } ) ;
36
- }
38
+ private graphQLService : GraphQLService
39
+ ) { }
37
40
38
41
/** Get all heroes from the server */
39
42
getHeroes ( ) : Observable < Hero [ ] > {
40
- return this . apollo . subscribe ( { query : allHeroes } ) . pipe (
41
- map ( ( { data} ) => data . allHeroes . nodes ) ,
42
- tap ( heroes => this . log ( `fetched heroes` ) ) ,
43
- catchError ( this . handleError ( 'getHeroes' , [ ] ) )
44
- ) ;
45
- }
46
-
47
- /** Get a hero by id. Return `undefined` when id not found */
48
- getHeroNo404 < Data > ( id : number ) : Observable < Hero > {
49
- return this . apollo . query < { heroById :Hero } > ( { query : heroById , variables : { id : id } } ) . pipe (
50
- map ( ( { data} ) => data . heroById ) , // returns a {0|1} element array
51
- tap ( h => {
52
- const outcome = h ? `fetched` : `did not find` ;
53
- this . log ( `${ outcome } hero id=${ id } ` ) ;
54
- } ) ,
55
- catchError ( this . handleError < Hero > ( `getHero id=${ id } ` ) )
56
- ) ;
43
+ return this . graphQLService . readAll < { allHeroes :{ nodes :Hero [ ] } } > ( options ) . pipe (
44
+ map ( ( data ) => data . allHeroes . nodes ) )
57
45
}
58
46
59
47
/** Get a hero by id. Will 404 if id not found */
60
48
getHero ( id : number ) : Observable < Hero > {
61
- return this . apollo . query < { heroById :Hero } > ( { query : heroById , variables : { id : id } } ) . pipe (
62
- map ( ( { data} ) => data . heroById ) , // returns a {0|1} element array
63
- tap ( _ => this . log ( `fetched hero id=${ id } ` ) ) ,
64
- catchError ( this . handleError < Hero > ( `getHero id=${ id } ` ) )
65
- ) ;
49
+ return this . graphQLService . readById < { heroById :Hero } > ( options , id ) . pipe (
50
+ map ( ( data ) => data . heroById ) )
66
51
}
67
52
68
53
/* Get all heroes whose name contains search term */
69
54
searchHeroes ( term : string ) : Observable < Hero [ ] > {
70
- if ( ! term . trim ( ) ) {
71
- // if not search term, return empty hero array.
72
- return of ( [ ] ) ;
73
- }
74
- return this . apollo . watchQuery < { herowithterm :{ nodes :Hero [ ] } } > ( { query : heroWithTerm , variables : { term : term } } ) . valueChanges . pipe (
75
- map ( ( { data} ) => data . herowithterm . nodes ) ,
76
- tap ( _ => this . log ( `found heroes matching "${ term } "` ) ) ,
77
- catchError ( this . handleError < Hero [ ] > ( 'searchHeroes' , [ ] ) )
78
- ) ;
55
+ 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 ) )
79
58
}
80
59
81
60
//////// Save methods //////////
82
61
83
62
/** Add a new hero to the server */
84
63
addHero ( hero : Hero ) : Observable < Hero > {
85
- return this . apollo . mutate < { createHero :{ hero :Hero } } > ( { mutation : createHero , variables : { name : hero . name } } ) . pipe (
86
- map ( ( { data} ) => data . createHero . hero ) , // returns a {0|1} element array
87
- tap ( _ => this . log ( `added hero id=${ _ . id } ` ) ) ,
88
- catchError ( this . handleError < Hero > ( 'addHero' ) )
89
- ) ;
64
+ return this . graphQLService . create < { createHero :{ hero :Hero } } > ( options , hero ) . pipe (
65
+ map ( ( data ) => data . createHero . hero ) )
90
66
}
91
67
92
68
/** Delete the hero from the server */
93
69
deleteHero ( hero : Hero | number ) : Observable < Hero > {
94
70
const id = typeof hero === 'number' ? hero : hero . id ;
95
- return this . apollo . mutate ( { mutation : deleteHero , variables : { id : id } } ) . pipe (
96
- tap ( _ => this . log ( `deleted hero id=${ id } ` ) ) ,
97
- catchError ( this . handleError < Hero > ( 'deleteHero' ) )
98
- ) ;
71
+ return this . graphQLService . delete < { deleteHeroById :{ hero :Hero } } > ( options , { "id" : id } ) . pipe (
72
+ map ( ( data ) => data . deleteHeroById . hero ) )
99
73
}
100
74
75
+
101
76
/** Update the hero on the server */
102
77
updateHero ( hero : Hero ) : Observable < Hero > {
103
- return this . apollo . mutate ( { mutation :updateHero , variables : hero } ) . pipe (
104
- tap ( _ => this . log ( `updated hero id=${ hero . id } new name=${ hero . name } ` ) ) ,
105
- catchError ( this . handleError < Hero > ( 'updateHero' ) )
106
- ) ;
78
+ return this . graphQLService . update < { updateHeroById :{ hero :Hero } } > ( options , hero ) . pipe (
79
+ map ( ( data ) => data . updateHeroById . hero ) )
107
80
}
108
81
109
- /**
110
- * Handle Http operation that failed.
111
- * Let the app continue.
112
- * @param operation - name of the operation that failed
113
- * @param result - optional value to return as the observable result
114
- */
115
- private handleError < T > ( operation = 'operation' , result ?: T ) {
116
- return ( error : any ) : Observable < T > => {
117
-
118
- // TODO: send the error to remote logging infrastructure
119
- console . error ( error ) ; // log to console instead
120
-
121
- // TODO: better job of transforming error for user consumption
122
- this . log ( `${ operation } failed: ${ error . message } no: ${ error . number } ` ) ;
123
-
124
- // Let the app keep running by returning an empty result.
125
- return of ( result as T ) ;
126
- } ;
127
- }
128
-
129
- /** Log a HeroService message with the MessageService */
130
- private log ( message : string ) {
131
- this . messageService . add ( 'HeroService: ' + message ) ;
132
- }
133
82
}
0 commit comments