backend/src/graphql/event/event.ts (view raw)
1export default ({ nexus, strapi }) => ({
2 types: [
3 nexus.extendType({
4 type: "Event",
5 definition(t) {
6 t.field("waitingPassengers", {
7 type: "PassengerRelationResponseCollection",
8 });
9 t.field("tripAlerts", {
10 type: "TripAlertEntityResponseCollection",
11 });
12 },
13 }),
14 nexus.extendType({
15 type: "Query",
16 definition(t) {
17 t.field("eventByUUID", {
18 type: "EventEntityResponse",
19 args: {
20 uuid: nexus.nonNull("String"),
21 },
22 });
23 },
24 }),
25 nexus.extendType({
26 type: "Mutation",
27 definition(t) {
28 t.field("updateEventByUUID", {
29 type: "EventEntityResponse",
30 args: {
31 uuid: nexus.nonNull("String"),
32 data: nexus.nonNull("EventInput"),
33 },
34 });
35 },
36 }),
37 ],
38 resolvers: {
39 Event: {
40 waitingPassengers: async (root, args) => {
41 const waitingPassengers = await strapi
42 .service("api::event.event")
43 .getWaitingPassengers(root);
44 const { toEntityResponseCollection } = strapi
45 .plugin("graphql")
46 .service("format").returnTypes;
47 return toEntityResponseCollection(waitingPassengers, {
48 args,
49 resourceUID: "api::passenger.passenger",
50 });
51 },
52 tripAlerts: async (root, args) => {
53 const tripAlerts = await strapi
54 .service("api::event.event")
55 .getTripAlerts(root);
56 const { toEntityResponseCollection } = strapi
57 .plugin("graphql")
58 .service("format").returnTypes;
59 return toEntityResponseCollection(tripAlerts, {
60 args,
61 resourceUID: "api::trip-alert.trip-alert",
62 });
63 },
64 },
65 Query: {
66 eventByUUID: {
67 description: "Retrieve an event using its UUID",
68 async resolve(_root, args) {
69 const { uuid } = args;
70 const event = await strapi.db
71 .query("api::event.event")
72 .findOne({ where: { uuid } });
73 if (!event) throw new Error("No matching event");
74 const { toEntityResponse } = strapi
75 .plugin("graphql")
76 .service("format").returnTypes;
77 return toEntityResponse(event, {
78 args,
79 resourceUID: "api::event.event",
80 });
81 },
82 },
83 },
84 Mutation: {
85 updateEventByUUID: {
86 description: "Update an event using its UUID",
87 async resolve(_root, args) {
88 const { uuid, data: eventUpdate } = args;
89
90 const updatedEvent = await strapi.db
91 .query("api::event.event")
92 .update({ where: { uuid }, data: eventUpdate });
93 if (!updatedEvent) throw new Error("No matching event");
94
95 const { toEntityResponse } = strapi
96 .plugin("graphql")
97 .service("format").returnTypes;
98 return toEntityResponse(updatedEvent, {
99 args,
100 resourceUID: "api::event.event",
101 });
102 },
103 },
104 createEvent: {
105 async resolve(_root, args, context) {
106 const {
107 koaContext,
108 state: { user },
109 } = context;
110
111 let eventData = args.data;
112 if (user)
113 eventData = { ...eventData, users: [user.id], creator: user.id };
114
115 koaContext.request.body = eventData;
116
117 const createdEvent = await strapi
118 .controller("api::event.event")
119 .create(koaContext);
120
121 return {
122 value: createdEvent,
123 info: { args, resourceUID: "api::event.event" },
124 };
125 },
126 },
127 },
128 },
129 resolversConfig: {
130 "Query.eventByUUID": {
131 auth: false,
132 },
133 "Mutation.updateEventByUUID": {
134 auth: false,
135 policies: ["api::event.check-caroster-plus"],
136 },
137 "Event.passengers": {
138 auth: false,
139 },
140 "Event.waitingPassengers": {
141 auth: false,
142 },
143 "Event.tripAlerts": {
144 auth: true,
145 },
146 "Event.travels": {
147 auth: false,
148 },
149 "Event.linkedEvent": {
150 auth: false,
151 },
152 },
153});