-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
113 lines (105 loc) · 3.53 KB
/
index.ts
File metadata and controls
113 lines (105 loc) · 3.53 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
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
import 'dotenv/config';
import express from 'express';
import { buildSchema, graphql as gqlExecute } from 'graphql';
import { randomUUID } from 'crypto';
import { PrismaClient } from './generated/prisma/client.js';
import { PrismaPg } from '@prisma/adapter-pg';
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter });
const schema = buildSchema(`
type Apartment {
id: ID!
name: String!
price: Float!
country: String!
created_at: String!
}
type AdminUser {
id: ID!
email: String!
password_hash: String!
role: String!
created_at: String!
}
type Query {
apartments: [Apartment!]!
adminUsers: [AdminUser!]!
}
type Mutation {
createApartment(name: String!, price: Float!, country: String!): Apartment!
updateApartment(id: ID!, name: String, price: Float, country: String): Apartment!
deleteApartment(id: ID!): Boolean!
createAdminUser(id: ID!, email: String!, password_hash: String!, role: String!): AdminUser!
updateAdminUser(id: ID!, email: String, password_hash: String, role: String): AdminUser!
deleteAdminUser(id: ID!): Boolean!
}
`);
// Prisma returns created_at as a Date — serialize to ISO string for GraphQL.
const serialize = (row: any) => ({ ...row, created_at: row.created_at.toISOString() });
const rootValue = {
apartments: async () => {
const rows = await prisma.apartment.findMany({ orderBy: { created_at: 'desc' } });
return rows.map(serialize);
},
createApartment: async ({ name, price, country }: any) => {
const row = await prisma.apartment.create({ data: { id: randomUUID(), name, price, country } });
return serialize(row);
},
updateApartment: async ({ id, name, price, country }: any) => {
const row = await prisma.apartment.update({
where: { id },
data: {
...(name !== undefined && { name }),
...(price !== undefined && { price }),
...(country !== undefined && { country }),
},
});
return serialize(row);
},
deleteApartment: async ({ id }: any) => {
try {
await prisma.apartment.delete({ where: { id } });
return true;
} catch {
return false;
}
},
adminUsers: async () => {
const rows = await prisma.adminUser.findMany({ orderBy: { created_at: 'desc' } });
return rows.map(serialize);
},
createAdminUser: async ({ id, email, password_hash, role }: any) => {
const row = await prisma.adminUser.create({ data: { id, email, password_hash, role } });
return serialize(row);
},
updateAdminUser: async ({ id, email, password_hash, role }: any) => {
const row = await prisma.adminUser.update({
where: { id },
data: {
...(email !== undefined && { email }),
...(password_hash !== undefined && { password_hash }),
...(role !== undefined && { role }),
},
});
return serialize(row);
},
deleteAdminUser: async ({ id }: any) => {
try {
await prisma.adminUser.delete({ where: { id } });
return true;
} catch {
return false;
}
},
};
const app = express();
app.use(express.json());
app.post('/graphql', async (req, res) => {
const { query, variables } = req.body;
const result = await gqlExecute({ schema, source: query, rootValue, variableValues: variables });
res.json({
data: result.data,
errors: result.errors?.map((e) => ({ message: e.message, locations: e.locations, path: e.path })),
});
});
app.listen(3001, () => console.log('API running on http://localhost:3001'));