1
1
// #docregion
2
+ // #docregion import-lodash
2
3
import { find , filter } from 'lodash' ;
4
+ // #enddocregion import-lodash
5
+ // #docregion import-graphql-tools
3
6
import { makeExecutableSchema } from 'graphql-tools' ;
4
-
7
+ // #enddocregion import-graphql-tools
8
+ // #docregion import-graphql
9
+ import { execute } from 'graphql' ;
10
+ // #enddocregion import-graphql
5
11
// #docregion graphql-schema
6
12
const typeDefinitions = `
7
13
type Hero {
@@ -35,7 +41,7 @@ schema {
35
41
}
36
42
` ;
37
43
// #enddocregion graphql-schema
38
-
44
+ // #docregion heroes-array
39
45
const heroes = [
40
46
{ id : 11 , name : 'Mr. Nice' } ,
41
47
{ id : 12 , name : 'Narco' } ,
@@ -48,45 +54,46 @@ const heroes = [
48
54
{ id : 19 , name : 'Magma' } ,
49
55
{ id : 20 , name : 'Tornado' }
50
56
] ;
57
+ // #enddocregion heroes-array
51
58
59
+ // #docregion resolvers
52
60
const resolveFunctions = {
53
61
Query : {
54
62
heroes ( ) {
55
63
return heroes ;
56
64
} ,
57
- hero ( obj : any , params : any , context : any ) {
58
- return find ( heroes , { id : params . heroId } ) ;
65
+ hero ( obj : any , args : any , context : any ) {
66
+ return find ( heroes , { id : args . heroId } ) ;
59
67
}
60
68
} ,
61
69
Mutation : {
62
- updateHero ( root : any , params : any ) {
63
- let hero = find ( heroes , { id : params . heroId } ) ;
70
+ updateHero ( root : any , args : any ) {
71
+ let hero = find ( heroes , { id : args . heroId } ) ;
64
72
if ( ! hero ) {
65
- throw new Error ( `Couldn't find post with id ${ params . heroId } ` ) ;
73
+ throw new Error ( `Couldn't find post with id ${ args . heroId } ` ) ;
66
74
}
67
- hero = params . heroId ;
75
+ hero = args . heroId ;
68
76
return hero ;
69
77
} ,
70
- addHero ( root : any , params : any ) {
78
+ addHero ( root : any , args : any ) {
71
79
const maxId = Math . max ( ...heroes . map ( ( hero ) => { return hero . id } ) ) ;
72
80
const newHero = {
73
- name : params . heroName ,
81
+ name : args . heroName ,
74
82
id : maxId + 1
75
83
} ;
76
84
heroes . push ( newHero ) ;
77
85
return ( newHero ) ;
78
86
}
79
87
}
80
88
}
81
-
89
+ // #enddocregion resolvers
90
+ // #docregion make-executable-schema
82
91
const schema = makeExecutableSchema ( {
83
92
typeDefs : typeDefinitions ,
84
93
resolvers : resolveFunctions ,
85
94
} ) ;
86
-
87
- // in-browser-network-interface.js
88
- import { execute } from 'graphql' ;
89
-
95
+ // #enddocregion make-executable-schema
96
+ // #docregion execute-and-export
90
97
class InBrowserNetworkInterface {
91
98
schema : any = { } ;
92
99
constructor ( params : any ) {
@@ -105,4 +112,5 @@ class InBrowserNetworkInterface {
105
112
}
106
113
107
114
export const networkInterface = new InBrowserNetworkInterface ( { schema } ) ;
115
+ // #enddocregion execute-and-export
108
116
// #enddocregion
0 commit comments