-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmutations.js
More file actions
69 lines (62 loc) · 1.38 KB
/
Copy pathmutations.js
File metadata and controls
69 lines (62 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const express = require("express");
const { graphqlHTTP } = require("express-graphql");
const { buildSchema } = require("graphql");
const schema = buildSchema(`
input MessageInput {
content: String,
author: String
}
type Message {
id: ID!,
content: String,
author: String
}
type Query {
getMessage(id:ID!): Message
}
type Mutation {
createMessage(input: MessageInput): Message,
updateMessage(id: ID!,input: MessageInput): Message
}
`);
class Message{
constructor(id, {author, content}){
this.id = id;
this.author = author;
this.content = content;
}
}
const fakeDatabase = {};
const root = {
getMessage: ({id}) => {
if(!fakeDatabase[id]){
throw new Error('no message with id:' + id);
}else{
return new Message(id, fakeDatabase[id]);
}
},
createMessage: ({input}) => {
const id = require('crypto').randomBytes(10).toString('hex')
fakeDatabase[id] = input;
return new Message(id, fakeDatabase[id]);
},
updateMessage: ({id, input}) => {
if(!fakeDatabase[id]){
throw new Error('no message with id:' + id);
}else{
fakeDatabase[id] = input;
return new Message(id, input)
}
}
};
const app = express();
app.use(
"/graphql",
graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
})
);
app.listen(4000);
console.log("Running a GraphQL API server at http://localhost:4000/graphql");