Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 37ecc0f

Browse files
committed
docs(cookbook/graphql): Add GraphQL search heroes
1 parent 823d03c commit 37ecc0f

File tree

4 files changed

+57
-48
lines changed

4 files changed

+57
-48
lines changed

public/docs/_examples/heroes-graphql/ts/app/hero-search.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h4>Hero Search</h4>
44
<input #searchBox id="search-box" (keyup)="search(searchBox.value)" />
55
<div>
6-
<div *ngFor="let hero of heroes | async"
6+
<div *ngFor="let hero of heroes | async | select: 'heroes'"
77
(click)="gotoDetail(hero)" class="search-result" >
88
{{hero.name}}
99
</div>

public/docs/_examples/heroes-graphql/ts/app/hero-search.component.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@ import { Router } from '@angular/router';
55
import { Observable } from 'rxjs/Observable';
66
import { Subject } from 'rxjs/Subject';
77

8-
import { HeroSearchService } from './hero-search.service';
98
import { Hero } from './hero';
109

10+
import { Angular2Apollo, ApolloQueryObservable } from 'angular2-apollo';
11+
import gql from 'graphql-tag';
12+
1113
@Component({
1214
moduleId: module.id,
1315
selector: 'hero-search',
1416
templateUrl: 'hero-search.component.html',
15-
styleUrls: [ 'hero-search.component.css' ],
16-
providers: [HeroSearchService]
17+
styleUrls: [ 'hero-search.component.css' ]
1718
})
1819
export class HeroSearchComponent implements OnInit {
1920
// #docregion search
20-
heroes: Observable<Hero[]>;
21+
heroes: ApolloQueryObservable<any>;
2122
// #enddocregion search
2223
// #docregion searchTerms
2324
private searchTerms = new Subject<string>();
2425
// #enddocregion searchTerms
2526

2627
constructor(
27-
private heroSearchService: HeroSearchService,
28+
private apollo: Angular2Apollo,
2829
private router: Router) {}
2930
// #docregion searchTerms
3031

@@ -36,19 +37,23 @@ export class HeroSearchComponent implements OnInit {
3637
// #docregion search
3738

3839
ngOnInit(): void {
39-
this.heroes = this.searchTerms
40-
.debounceTime(300) // wait for 300ms pause in events
41-
.distinctUntilChanged() // ignore if next search term is same as previous
42-
.switchMap(term => term // switch to new observable each time
43-
// return the http search observable
44-
? this.heroSearchService.search(term)
45-
// or the observable of empty heroes if no search term
46-
: Observable.of<Hero[]>([]))
47-
.catch(error => {
48-
// TODO: real error handling
49-
console.log(error);
50-
return Observable.of<Hero[]>([]);
51-
});
40+
41+
this.heroes = this.apollo.watchQuery({
42+
query: gql`
43+
query searchHeroes ($search: String) {
44+
heroes (search: $search) {
45+
id
46+
name
47+
}
48+
}
49+
`,
50+
variables: {
51+
search: this.searchTerms
52+
.debounceTime(300) // wait for 300ms pause in events
53+
.distinctUntilChanged() // ignore if next search term is same as previous
54+
},
55+
forceFetch: true
56+
})
5257
}
5358
// #enddocregion search
5459

public/docs/_examples/heroes-graphql/ts/app/hero-search.service.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

public/docs/_examples/heroes-graphql/ts/app/in-memory-graphql.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,30 @@ import { execute } from 'graphql';
1212
const typeDefinitions = `
1313
type Hero {
1414
id: Int!
15-
name: String
15+
name: String!
1616
}
1717
1818
# the schema allows the following query:
1919
type Query {
20-
heroes: [Hero]
20+
heroes(search: String): [Hero]
2121
2222
hero(heroId: Int!): Hero
2323
}
2424
2525
# this schema allows the following mutation:
2626
type Mutation {
2727
updateHero (
28-
heroId: Int!
28+
id: Int!
29+
name: String!
2930
): Hero
3031
3132
addHero (
3233
heroName: String!
3334
): Hero
35+
36+
deleteHero (
37+
id: Int!
38+
): Hero
3439
}
3540
3641
# we need to tell the server which types represent the root query
@@ -42,7 +47,7 @@ schema {
4247
`;
4348
// #enddocregion graphql-schema
4449
// #docregion heroes-array
45-
const heroes = [
50+
let heroes = [
4651
{id: 11, name: 'Mr. Nice'},
4752
{id: 12, name: 'Narco'},
4853
{id: 13, name: 'Bombasto'},
@@ -59,20 +64,26 @@ const heroes = [
5964
// #docregion resolvers
6065
const resolveFunctions = {
6166
Query: {
62-
heroes() {
63-
return heroes;
67+
heroes(obj: any, args: any) {
68+
if (args.search) {
69+
return heroes.filter(function (currentHero) {
70+
return currentHero.name.search(args.search) != -1;
71+
});
72+
} else {
73+
return heroes;
74+
}
6475
},
6576
hero(obj: any, args: any, context: any) {
6677
return find(heroes, { id: args.heroId });
6778
}
6879
},
6980
Mutation: {
7081
updateHero(root: any, args: any) {
71-
let hero = find(heroes, { id: args.heroId });
82+
let hero = find(heroes, { id: args.id });
7283
if (!hero) {
73-
throw new Error(`Couldn't find post with id ${args.heroId}`);
84+
throw new Error(`Couldn't find post with id ${args.id}`);
7485
}
75-
hero = args.heroId;
86+
hero.name = args.name;
7687
return hero;
7788
},
7889
addHero(root: any, args: any) {
@@ -82,8 +93,19 @@ const resolveFunctions = {
8293
id: maxId + 1
8394
};
8495
heroes.push(newHero);
85-
return(newHero);
86-
}
96+
return newHero;
97+
},
98+
deleteHero(root: any, args: any) {
99+
console.log('args', args);
100+
let hero = find(heroes, { id: args.id });
101+
console.log('deleted hero start', hero);
102+
if (!hero) {
103+
throw new Error(`Couldn't find post with id ${args.id}`);
104+
}
105+
heroes = heroes.filter(function (currentHero) { return currentHero.id != args.id; });
106+
console.log('deleted hero', hero);
107+
return hero;
108+
},
87109
}
88110
}
89111
// #enddocregion resolvers

0 commit comments

Comments
 (0)