-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
159 lines (154 loc) · 4.39 KB
/
server.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const { ApolloServer, gql } = require("apollo-server");
const jwt = require("jsonwebtoken");
const JWT_SECRET = "makethislongandrandom";
const randomId = () => "" + Math.floor(Math.random() * 100);
// This could also be MongoDB, PostgreSQL, etc
const db = {
users: [
{
organization: "123", // this is a relation by id
id: "1",
name: "Elon Musk",
},
{
organization: "123", // this is a relation by id
id: "2",
name: "jamal",
},
{
organization: "1235", // this is a relation by id
id: "3",
name: "anders",
},
],
organizations: [
{
users: ["1"], // this is a relation by ids
id: "123",
name: "Space X",
},
{
users: ["2"], // this is a relation by ids
id: "1235",
name: "testerne",
},
],
};
// All the code needed for a working GraphQL API
// context, typeDefs (schema), and resolvers
const server = new ApolloServer({
context: ({ req }) => {
let user = null;
try {
const token = req.headers.authorization.replace("Bearer ", "");
user = jwt.verify(token, JWT_SECRET);
} catch (error) {}
return { user };
},
typeDefs: gql`
type Mutation {
signup(organization: String, name: String): User
addOrganization(name: String, users: [String!]): Organization
deleteOrganization(id: String): Organization
updateOrganization(
id: String
name: String
users: [String!]
): Organization
}
type Query {
login(username: String): String
tellMeADadJoke: String
users: [User]
user(id: ID!): User
organizations: [Organization]
organization(id: ID!): Organization
}
type User {
organization: Organization
id: ID
name: String
}
type Organization {
users: [User]
id: ID
name: String
}
`,
resolvers: {
Mutation: {
signup(_, { organization, name }) {
const user = { id: randomId(), organization, name };
const match = db.users.find((user) => user.name === name);
if (match) throw Error("This username already exists");
db.users.push(user);
return user;
},
addOrganization(_, { name, users }) {
const organization = {
id: randomId(),
name,
users,
};
const match = db.organizations.find(
(org) => org.name === organization.name
);
if (match) throw Error("This organisation already exists");
db.organizations.push(organization);
return organization;
},
deleteOrganization(_, { id }) {
const match = db.organizations.find((org) => org.id === id);
if (!match) throw Error("The organisation does not exists");
db.organizations = db.organizations.filter((org) => org.id !== id);
return match;
},
updateOrganization(_, { id, name, users }) {
const match = db.organizations.find((org) => org.id === id);
if (!match) throw Error("The organisation does not exists");
db.organizations = db.organizations.map((o) => {
if (o.id === id) {
o.name = match.name = name;
o.users = match.users = users;
}
return o;
});
return match;
},
},
Query: {
login(_, { username }) {
const user = db.users.find((user) => user.name === username);
if (!user) {
throw Error("username was incorrect");
}
const token = jwt.sign({ id: user.id }, JWT_SECRET);
return token;
},
tellMeADadJoke(_, data, { user }) {
if (!user) throw Error("not authorized");
return "If you see a robbery at an Apple Store does that make you an iWitness?";
},
users: () => db.users,
user: (_, { id }) => db.users.find((user) => user.id === id),
organizations: () => db.organizations,
organization: (_, { id }) =>
db.organizations.find((organization) => organization.id === id),
},
User: {
organization: (parent) => {
return db.organizations.find(({ id }) => {
return parent.organization === id;
});
},
},
Organization: {
users: (parent) => {
return db.users.filter(({ id }) => {
return parent.users.includes(id);
});
},
},
},
});
server.listen().then(({ url }) => console.log(`Server ready at ${url}`));