Skip to content

Commit e1857cd

Browse files
authored
migrate to code-first graphql using type-graphql and typegraphql-prisma (#90)
* WIP * WIP * complete migration from SDL-first graphql to code-first graphql using type-graphql * WIP * fix booking update resolver, and validate booking function * add typegraphql auth checker, and migrate shield rules to auth checker rules * fix tests * fix eslint * optimise build steps to build dist/ while building the docker image * downgrade typegraphql-prisma to 0.16.4 to resolve graphql-scalars build dependencies * fix dockerfile * remove push condition on dev github action
1 parent c032c8b commit e1857cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+13773
-22738
lines changed

.eslintignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules
22
dist
3-
/src/generated
3+
/src/generated
4+
.eslintrc.js
5+
jest.config.js

.github/workflows/prod.yml

-6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ jobs:
2626
with:
2727
node-version: 14
2828

29-
- name: Install
30-
run: npm install
31-
32-
- name: Build
33-
run: npm run build
34-
3529
- name: Set up Docker Buildx
3630
uses: docker/setup-buildx-action@v1
3731

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ dist/
1818

1919
src/generated
2020

21+
schema.gql
2122
*.sql

Dockerfile

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ FROM node:14-alpine
22
WORKDIR usr/src/app
33

44
COPY package.json .
5-
65
RUN npm install
76

8-
COPY dist dist
9-
10-
RUN mkdir prisma
11-
COPY prisma/schema.prisma prisma/
12-
COPY prisma/migrations prisma/migrations/
7+
COPY src src
8+
COPY prisma prisma
9+
COPY tsconfig.json .
1310
RUN npx prisma generate
1411

12+
RUN npm run build
13+
1514
EXPOSE 4000
1615
CMD ["npm", "run", "start"]

__tests__/bookings.ts

+22-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as env from "dotenv";
2-
env.config();
3-
1+
import "reflect-metadata";
42
import gql from "graphql-tag";
53
import moment from "moment";
64
import { createTestServerWithUserLoggedIn } from "./utils/server";
@@ -44,28 +42,16 @@ const testRoomInfo: TestRoomInfo = {
4442

4543
const testBookingInfo: TestBookingInfo = {
4644
user: "test123",
47-
room: "123",
48-
start: moment()
49-
.startOf("hour")
50-
.add(1, "hour")
51-
.toDate(),
52-
end: moment()
53-
.startOf("hour")
54-
.add(3, "hour")
55-
.toDate(),
45+
room: testRoomInfo.number,
46+
start: moment().startOf("hour").add(1, "hour").toDate(),
47+
end: moment().startOf("hour").add(3, "hour").toDate(),
5648
remark: "Hello",
5749
};
5850

5951
const testInvalidBookingVariables = {
60-
room_number: "123",
61-
start: moment()
62-
.startOf("hour")
63-
.add(2, "hour")
64-
.toISOString(),
65-
end: moment()
66-
.startOf("hour")
67-
.add(3, "hour")
68-
.toISOString(),
52+
room_number: testRoomInfo.number,
53+
start: moment().startOf("hour").add(2, "hour").toISOString(),
54+
end: moment().startOf("hour").add(3, "hour").toISOString(),
6955
remark: "Hi",
7056
};
7157

@@ -109,7 +95,9 @@ describe("Booking queries", () => {
10995
}
11096
}
11197
`;
98+
11299
const response: GraphQLResponse = await client.query({ query });
100+
113101
expect(response.data).toEqual({
114102
bookings: [
115103
{
@@ -119,8 +107,8 @@ describe("Booking queries", () => {
119107
room: {
120108
number: testRoomInfo.number,
121109
},
122-
start: testBookingInfo.start,
123-
end: testBookingInfo.end,
110+
start: testBookingInfo.start.toISOString(),
111+
end: testBookingInfo.end.toISOString(),
124112
remark: testBookingInfo.remark,
125113
},
126114
],
@@ -142,7 +130,7 @@ describe("Booking validation", () => {
142130

143131
// Attempt to create an invalid booking
144132
const mutation = gql`
145-
mutation(
133+
mutation (
146134
$room_number: String!
147135
$start: String!
148136
$end: String!
@@ -163,7 +151,7 @@ describe("Booking validation", () => {
163151
variables: testInvalidBookingVariables,
164152
});
165153

166-
expect(response.data).toEqual({ createBooking: null });
154+
expect(response.errors[0].message).toBeDefined();
167155
});
168156
});
169157

@@ -176,11 +164,11 @@ describe("Booking mutations", () => {
176164
const room = await createRoom(testRoomInfo);
177165
const booking = await createBooking(testBookingInfo);
178166
const mutation = gql`
179-
mutation(
167+
mutation (
180168
$id: ID!
181-
$room: String!
182-
$start: String!
183-
$end: String!
169+
$room: String
170+
$start: String
171+
$end: String
184172
$remark: String
185173
) {
186174
updateBooking(
@@ -197,9 +185,6 @@ describe("Booking mutations", () => {
197185
`;
198186
const testUpdatedBookingInfo = {
199187
id: booking.id,
200-
room: testBookingInfo.room,
201-
start: booking.start.toISOString(),
202-
end: booking.end.toISOString(),
203188
remark: "HelloWorld",
204189
};
205190
const result: GraphQLResponse = await client.mutate({
@@ -224,7 +209,7 @@ describe("Booking mutations", () => {
224209
const client1 = createTestClient(testServer1);
225210

226211
const mutation = gql`
227-
mutation(
212+
mutation (
228213
$id: ID!
229214
$room: String!
230215
$start: String!
@@ -254,7 +239,7 @@ describe("Booking mutations", () => {
254239
mutation,
255240
variables: testUpdatedBookingInfo,
256241
});
257-
expect(result.errors[0].message).toEqual("Not Authorised!");
242+
expect(result.errors[0].message).toBeDefined();
258243
});
259244

260245
test("allow users to delete their own bookings", async () => {
@@ -265,7 +250,7 @@ describe("Booking mutations", () => {
265250
const room = await createRoom(testRoomInfo);
266251
const booking = await createBooking(testBookingInfo);
267252
const mutation = gql`
268-
mutation($id: ID!) {
253+
mutation ($id: ID!) {
269254
deleteBooking(id: $id) {
270255
id
271256
}
@@ -290,7 +275,7 @@ describe("Booking mutations", () => {
290275
const testServer1 = await createTestServerWithUserLoggedIn(user1);
291276
const client1 = createTestClient(testServer1);
292277
const mutation = gql`
293-
mutation($id: ID!) {
278+
mutation ($id: ID!) {
294279
deleteBooking(id: $id) {
295280
id
296281
}
@@ -301,6 +286,6 @@ describe("Booking mutations", () => {
301286
mutation,
302287
variables: { id: booking.id },
303288
});
304-
expect(result.errors[0].message).toEqual("Not Authorised!");
289+
expect(result.errors[0].message).toBeDefined();
305290
});
306291
});

__tests__/events/comments.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as env from "dotenv";
2-
env.config();
3-
1+
import "reflect-metadata";
42
import gql from "graphql-tag";
53
import { GraphQLResponse } from "apollo-server-types";
64
import { createTestServerWithUserLoggedIn } from "../utils/server";
@@ -68,7 +66,7 @@ describe("event comment creation", () => {
6866
const testEvent: Event = await createEvent(testEventInfo);
6967

7068
const mutation = gql`
71-
mutation($eventId: ID!, $content: String!) {
69+
mutation ($eventId: ID!, $content: String!) {
7270
createComment(eventId: $eventId, content: $content) {
7371
content
7472
user {
@@ -114,7 +112,7 @@ describe("event comment deletion", () => {
114112
const testComment: Comment = await createEventComment(eventCommentInfo);
115113

116114
const mutation = gql`
117-
mutation($id: ID!) {
115+
mutation ($id: ID!) {
118116
deleteComment(id: $id) {
119117
id
120118
}
@@ -157,7 +155,7 @@ describe("invalid event comment deletion", () => {
157155
const testComment: Comment = await createEventComment(eventCommentInfo);
158156

159157
const mutation = gql`
160-
mutation($id: ID!) {
158+
mutation ($id: ID!) {
161159
deleteComment(id: $id) {
162160
id
163161
}
@@ -169,6 +167,6 @@ describe("invalid event comment deletion", () => {
169167
variables: { id: testComment.id },
170168
});
171169

172-
expect(response.errors[0].message).toEqual("Not Authorised!");
170+
expect(response.errors[0].message).toBeDefined();
173171
});
174172
});

__tests__/events/events.ts

+11-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as env from "dotenv";
2-
env.config();
1+
import "reflect-metadata";
32
import gql from "graphql-tag";
43
import moment from "moment";
54
import { GraphQLResponse } from "apollo-server-types";
@@ -34,14 +33,8 @@ const testUserInfo1: TestUserInfo = {
3433
const testEventInfo: TestEventInfo = {
3534
title: "test event",
3635
organiser: testUserInfo.username,
37-
start: moment()
38-
.startOf("hour")
39-
.add(1, "hour")
40-
.toDate(),
41-
end: moment()
42-
.startOf("hour")
43-
.add(2, "hour")
44-
.toDate(),
36+
start: moment().startOf("hour").add(1, "hour").toDate(),
37+
end: moment().startOf("hour").add(2, "hour").toDate(),
4538
venue: "test venue",
4639
image_url: "http://url",
4740
description: "test description",
@@ -88,8 +81,8 @@ describe("event queries", () => {
8881
organiser: {
8982
username: testUserInfo.username,
9083
},
91-
start: testEventInfo.start,
92-
end: testEventInfo.end,
84+
start: testEventInfo.start.toISOString(),
85+
end: testEventInfo.end.toISOString(),
9386
venue: testEventInfo.venue,
9487
image_url: testEventInfo.image_url,
9588
description: testEventInfo.description,
@@ -108,7 +101,7 @@ describe("event creation", () => {
108101
const client = createTestClient(testServer);
109102

110103
const mutation = gql`
111-
mutation(
104+
mutation (
112105
$title: String!
113106
$start: String!
114107
$end: String!
@@ -155,8 +148,8 @@ describe("event creation", () => {
155148
organiser: {
156149
username: testUserInfo.username,
157150
},
158-
start: testEventInfo.start,
159-
end: testEventInfo.end,
151+
start: testEventInfo.start.toISOString(),
152+
end: testEventInfo.end.toISOString(),
160153
venue: testEventInfo.venue,
161154
image_url: "",
162155
description: testEventInfo.description,
@@ -176,7 +169,7 @@ describe("event deletion", () => {
176169
const event: Event = await createEvent(testEventInfo);
177170

178171
const mutation = gql`
179-
mutation($id: ID!) {
172+
mutation ($id: ID!) {
180173
deleteEvent(id: $id) {
181174
id
182175
}
@@ -208,7 +201,7 @@ describe("invalid event deletion", () => {
208201
const event: Event = await createEvent(testEventInfo);
209202

210203
const mutation = gql`
211-
mutation($id: ID!) {
204+
mutation ($id: ID!) {
212205
deleteEvent(id: $id) {
213206
id
214207
}
@@ -218,6 +211,6 @@ describe("invalid event deletion", () => {
218211
mutation,
219212
variables: { id: event.id },
220213
});
221-
expect(response.errors[0].message).toEqual("Not Authorised!");
214+
expect(response.errors[0].message).toBeDefined();
222215
});
223216
});

__tests__/events/subscribers.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as env from "dotenv";
2-
env.config();
3-
1+
import "reflect-metadata";
42
import gql from "graphql-tag";
53
import { GraphQLResponse } from "apollo-server-types";
64
import { createTestServerWithUserLoggedIn } from "../utils/server";
@@ -54,7 +52,7 @@ describe("event subscriber addition", () => {
5452
const { id: event_id }: Event = await createEvent(testEventInfo);
5553

5654
const mutation = gql`
57-
mutation($id: ID!) {
55+
mutation ($id: ID!) {
5856
addEventSubscriber(id: $id) {
5957
subscribers {
6058
id
@@ -91,7 +89,7 @@ describe("event subscriber removal for subscribed event", () => {
9189
await addEventSubscriber(addEventSubscriberInfo);
9290

9391
const mutation = gql`
94-
mutation($id: ID!) {
92+
mutation ($id: ID!) {
9593
removeEventSubscriber(id: $id) {
9694
subscribers {
9795
id
@@ -122,7 +120,7 @@ describe("event subscriber removal for non-subscribed event", () => {
122120
const { id: event_id }: Event = await createEvent(testEventInfo);
123121

124122
const mutation = gql`
125-
mutation($id: ID!) {
123+
mutation ($id: ID!) {
126124
removeEventSubscriber(id: $id) {
127125
id
128126
}

0 commit comments

Comments
 (0)