Skip to content

Update mutations-and-input-types.mdx #4399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 17, 2025
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