Skip to content

Commit 2120ff3

Browse files
Update mutations-and-input-types.mdx (#4399)
Co-authored-by: Jovi De Croock <decroockjovi@gmail.com>
1 parent 98eff7f commit 2120ff3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

website/pages/docs/mutations-and-input-types.mdx

+24
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ const express = require('express');
207207
const { createHandler } = require('graphql-http/lib/use/express');
208208
const { buildSchema } = require('graphql');
209209

210+
const fakeDatabase = {};
211+
210212
// Construct a schema, using GraphQL schema language
211213
const schema = buildSchema(`
212214
input MessageInput {
@@ -222,6 +224,27 @@ type Message {
222224
223225
type Query {
224226
getMessage(id: ID!): Message
227+
getMessages: [Message]
228+
}
229+
230+
const root = {
231+
getMessage: ({ id }) => {
232+
return fakeDatabase[id]
233+
},
234+
getMessages: () => {
235+
return Object.values(fakeDatabase)
236+
},
237+
createMessage: ({ input }) => {
238+
const id = String(Object.keys(fakeDatabase).length + 1)
239+
const message = new Message(id, input)
240+
fakeDatabase[id] = message
241+
return message
242+
},
243+
updateMessage: ({ id, input }) => {
244+
const message = fakeDatabase[id]
245+
Object.assign(message, input)
246+
return message
247+
}
225248
}
226249
227250
type Mutation {
@@ -244,6 +267,7 @@ app.all(
244267
'/graphql',
245268
createHandler({
246269
schema: schema,
270+
rootValue: root,
247271
}),
248272
);
249273
app.listen(4000, () => {

0 commit comments

Comments
 (0)