File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -207,6 +207,8 @@ const express = require('express');
207207const { createHandler } = require (' graphql-http/lib/use/express' );
208208const { buildSchema } = require (' graphql' );
209209
210+ const fakeDatabase = {};
211+
210212// Construct a schema, using GraphQL schema language
211213const schema = buildSchema (`
212214input MessageInput {
@@ -222,6 +224,27 @@ type Message {
222224
223225type 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
227250type Mutation {
@@ -244,6 +267,7 @@ app.all(
244267 ' /graphql' ,
245268 createHandler ({
246269 schema: schema,
270+ rootValue: root,
247271 }),
248272);
249273app .listen (4000 , () => {
You can’t perform that action at this time.
0 commit comments