Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions website/pages/docs/mutations-and-input-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ const express = require('express');
const { createHandler } = require('graphql-http/lib/use/express');
const { buildSchema } = require('graphql');

const fakeDatabase = {};

// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
input MessageInput {
Expand All @@ -222,6 +224,27 @@ type Message {

type Query {
getMessage(id: ID!): Message
getMessages: [Message]
}

const root = {
getMessage: ({ id }) => {
return fakeDatabase[id]
},
getMessages: () => {
return Object.values(fakeDatabase)
},
createMessage: ({ input }) => {
const id = String(Object.keys(fakeDatabase).length + 1)
const message = new Message(id, input)
fakeDatabase[id] = message
return message
},
updateMessage: ({ id, input }) => {
const message = fakeDatabase[id]
Object.assign(message, input)
return message
}
}

type Mutation {
Expand All @@ -244,6 +267,7 @@ app.all(
'/graphql',
createHandler({
schema: schema,
rootValue: root,
}),
);
app.listen(4000, () => {
Expand Down
Loading