@@ -16,9 +16,8 @@ type Mutation {
16
16
}
17
17
18
18
type Query {
19
- getMessage : String
19
+ getMessage : String
20
20
}
21
-
22
21
````
23
22
</Tabs .Tab >
24
23
<Tabs .Tab >
@@ -78,18 +77,18 @@ input MessageInput {
78
77
}
79
78
80
79
type Message {
81
- id : ID !
82
- content : String
83
- author : String
80
+ id : ID !
81
+ content : String
82
+ author : String
84
83
}
85
84
86
85
type Query {
87
- getMessage (id : ID ! ): Message
86
+ getMessage (id : ID ! ): Message
88
87
}
89
88
90
89
type Mutation {
91
- createMessage (input : MessageInput ): Message
92
- updateMessage (id : ID ! , input : MessageInput ): Message
90
+ createMessage (input : MessageInput ): Message
91
+ updateMessage (id : ID ! , input : MessageInput ): Message
93
92
}
94
93
95
94
````
@@ -174,72 +173,71 @@ const { createHandler } = require('graphql-http/lib/use/express');
174
173
const { buildSchema } = require ('graphql');
175
174
176
175
// Construct a schema , using GraphQL schema language
177
- const schema = buildSchema(/_ GraphQL _/ `
176
+ const schema = buildSchema(`
178
177
input MessageInput {
179
- content : String
180
- author : String
178
+ content : String
179
+ author : String
181
180
}
182
181
183
182
type Message {
184
- id : ID !
185
- content : String
186
- author : String
183
+ id : ID !
184
+ content : String
185
+ author : String
187
186
}
188
187
189
188
type Query {
190
- getMessage (id : ID ! ): Message
189
+ getMessage (id : ID ! ): Message
191
190
}
192
191
193
192
type Mutation {
194
- createMessage (input : MessageInput ): Message
195
- updateMessage (id : ID ! , input : MessageInput ): Message
193
+ createMessage (input : MessageInput ): Message
194
+ updateMessage (id : ID ! , input : MessageInput ): Message
196
195
}
197
196
`);
198
197
199
198
// If Message had any complex fields , we 'd put them on this object .
200
199
class Message {
201
- constructor (id, { content, author }) {
202
- this .id = id ;
203
- this .content = content ;
204
- this .author = author ;
205
- }
200
+ constructor (id, { content, author }) {
201
+ this .id = id ;
202
+ this .content = content ;
203
+ this .author = author ;
204
+ }
206
205
}
207
206
208
207
// Maps username to content
209
208
const fakeDatabase = {};
210
209
211
210
const root = {
212
- getMessage ({ id }) {
213
- if (!fakeDatabase[id]) {
214
- throw new Error ('no message exists with id ' + id);
215
- }
216
- return new Message (id, fakeDatabase[id]);
217
- },
218
- createMessage ({ input }) {
219
- // Create a random id for our "database" .
220
- const id = require ('crypto').randomBytes (10).toString ('hex');
211
+ getMessage ({ id }) {
212
+ if (!fakeDatabase[id]) {
213
+ throw new Error ('no message exists with id ' + id);
214
+ }
215
+ return new Message (id, fakeDatabase[id]);
216
+ },
217
+ createMessage ({ input }) {
218
+ // Create a random id for our "database" .
219
+ const id = require ('crypto').randomBytes (10).toString ('hex');
221
220
222
221
fakeDatabase [id ] = input ;
223
222
return new Message (id, input);
224
-
225
- },
226
- updateMessage ({ id, input }) {
227
- if (!fakeDatabase[id]) {
228
- throw new Error ('no message exists with id ' + id);
229
- }
230
- // This replaces all old data , but some apps might want partial update .
231
- fakeDatabase [id ] = input ;
232
- return new Message (id, input);
233
- },
223
+ },
224
+ updateMessage ({ id, input }) {
225
+ if (!fakeDatabase[id]) {
226
+ throw new Error ('no message exists with id ' + id);
227
+ }
228
+ // This replaces all old data , but some apps might want partial update .
229
+ fakeDatabase [id ] = input ;
230
+ return new Message (id, input);
231
+ },
234
232
};
235
233
236
234
const app = express ();
237
235
app .all (
238
- '/graphql',
239
- createHandler ({
240
- schema : schema ,
241
- rootValue : root ,
242
- }),
236
+ '/graphql',
237
+ createHandler ({
238
+ schema : schema ,
239
+ rootValue : root ,
240
+ }),
243
241
);
244
242
app .listen (4000, () => {
245
243
console .log ('Running a GraphQL API server at localhost :4000 /graphql ');
0 commit comments