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

Commit 5271eeb

Browse files
committed
docs(cookbook/graphql): Add addHero mutation to GraphQL in-memory server
1 parent 2b74c78 commit 5271eeb

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ type Mutation {
2121
updateHero (
2222
heroId: Int!
2323
): Hero
24+
25+
addHero (
26+
heroName: String!
27+
): Hero
2428
}
2529
2630
# we need to tell the server which types represent the root query
@@ -55,14 +59,23 @@ const resolveFunctions = {
5559
}
5660
},
5761
Mutation: {
58-
// updateHero(root: any, params: any) {
59-
// const post = find(posts, { id: params.postId });
60-
// if (!post) {
61-
// throw new Error(`Couldn't find post with id ${params.postId}`);
62-
// }
63-
// post.votes += 1;
64-
// return post;
65-
// }
62+
updateHero(root: any, params: any) {
63+
let hero = find(heroes, { id: params.heroId });
64+
if (!hero) {
65+
throw new Error(`Couldn't find post with id ${params.heroId}`);
66+
}
67+
hero = params.heroId;
68+
return hero;
69+
},
70+
addHero(root: any, params: any) {
71+
const maxId = Math.max(...heroes.map((hero)=>{return hero.id}));
72+
const newHero = {
73+
name: params.heroName,
74+
id: maxId + 1
75+
};
76+
heroes.push(newHero);
77+
return(newHero);
78+
}
6679
}
6780
}
6881

0 commit comments

Comments
 (0)