summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/src/gql/Mutations.ts
blob: d669b251763d78de1cc206b017a860df152b4d25 (plain) (blame)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { toastSuccess } from '$lib/Toasts';
import {
	Client,
	type AnyVariables,
	type OperationResult,
	type OperationResultSource
} from '@urql/svelte';
import { isError, isSuccess, type RequiredName } from './Utils';
import * as gql from './graphql';

export type DeleteMutation = (client: Client, args: { ids: number | number[] }) => Promise<unknown>;

const comicTypes = ['Comic'];
const comicUpsertTypes = comicTypes.concat([
	'Artist',
	'Character',
	'Circle',
	'Namespace',
	'Collection',
	'Tag',
	'World'
]);

function handleResult<D, I extends AnyVariables>(result: OperationResult<D, I>): Promise<D> {
	return new Promise((resolve, reject) => {
		if (result.error) {
			return reject(`${result.error.name}: ${result.error.message}`);
		}

		if (!result.data) {
			return reject('Mutation resolved, but result contains no data.');
		}

		const obj = Object.values(result.data)[0];

		if (isError(obj)) {
			reject(obj.message);
		} else if (isSuccess(obj)) {
			toastSuccess(obj.message);
			resolve(result.data);
		}

		reject('This should not happen.');
	});
}

async function handleMutation<D, V extends AnyVariables>(
	mutation: OperationResultSource<OperationResult<D, V>>
) {
	return await mutation.toPromise().then(handleResult);
}

export async function addComic(client: Client, args: gql.AddComicMutationVariables) {
	return await handleMutation(
		client.mutation(gql.AddComicDocument, args, {
			additionalTypenames: comicTypes.concat('Archive', 'Page')
		})
	);
}

export async function updateComics(client: Client, args: gql.UpdateComicsMutationVariables) {
	return await handleMutation(
		client.mutation(gql.UpdateComicsDocument, args, {
			additionalTypenames: comicTypes
		})
	);
}

export async function upsertComics(client: Client, args: gql.UpsertComicsMutationVariables) {
	return await handleMutation(
		client.mutation(gql.UpsertComicsDocument, args, {
			additionalTypenames: comicUpsertTypes
		})
	);
}

export async function updateArchives(client: Client, args: gql.UpdateArchivesMutationVariables) {
	return await handleMutation(
		client.mutation(gql.UpdateArchivesDocument, args, {
			additionalTypenames: ['Archive']
		})
	);
}

export async function deleteArchives(client: Client, args: gql.DeleteArchivesMutationVariables) {
	return await handleMutation(
		client.mutation(gql.DeleteArchivesDocument, args, {
			additionalTypenames: comicTypes.concat('Archive')
		})
	);
}

export async function deleteComics(client: Client, args: gql.DeleteComicsMutationVariables) {
	return await handleMutation(
		client.mutation(gql.DeleteComicsDocument, args, { additionalTypenames: comicTypes })
	);
}

export async function addArtist(client: Client, args: gql.AddArtistMutationVariables) {
	return await handleMutation(
		client.mutation(gql.AddArtistDocument, args, { additionalTypenames: ['ArtistFilterResult'] })
	);
}

export async function updateArtists(client: Client, args: gql.UpdateArtistsMutationVariables) {
	return await handleMutation(
		client.mutation(gql.UpdateArtistsDocument, args, { additionalTypenames: ['Artist'] })
	);
}

export async function deleteArtists(client: Client, args: gql.DeleteArtistsMutationVariables) {
	return await handleMutation(
		client.mutation(gql.DeleteArtistsDocument, args, {
			additionalTypenames: ['Artist']
		})
	);
}

export async function addCharacter(client: Client, args: gql.AddCharacterMutationVariables) {
	return await handleMutation(
		client.mutation(gql.AddCharacterDocument, args, {
			additionalTypenames: ['CharacterFilterResult']
		})
	);
}

export async function updateCharacters(
	client: Client,
	args: gql.UpdateCharactersMutationVariables
) {
	return await handleMutation(
		client.mutation(gql.UpdateCharactersDocument, args, { additionalTypenames: ['Character'] })
	);
}

export async function deleteCharacters(
	client: Client,
	args: gql.DeleteCharactersMutationVariables
) {
	return await handleMutation(
		client.mutation(gql.DeleteCharactersDocument, args, {
			additionalTypenames: ['Character']
		})
	);
}

export async function addCircle(client: Client, args: gql.AddCircleMutationVariables) {
	return await handleMutation(
		client.mutation(gql.AddCircleDocument, args, { additionalTypenames: ['CircleFilterResult'] })
	);
}

export async function updateCircles(client: Client, args: gql.UpdateCirclesMutationVariables) {
	return await handleMutation(
		client.mutation(gql.UpdateCirclesDocument, args, { additionalTypenames: ['Circle'] })
	);
}

export async function deleteCircles(client: Client, args: gql.DeleteCirclesMutationVariables) {
	return await handleMutation(
		client.mutation(gql.DeleteCirclesDocument, args, {
			additionalTypenames: ['Circle']
		})
	);
}

export async function addNamespace(client: Client, args: gql.AddNamespaceMutationVariables) {
	return await handleMutation(
		client.mutation(gql.AddNamespaceDocument, args, {
			additionalTypenames: ['NamespaceFilterResult', 'ComicTagFilterResult']
		})
	);
}

export async function updateNamespaces(
	client: Client,
	args: gql.UpdateNamespacesMutationVariables
) {
	return await handleMutation(
		client.mutation(gql.UpdateNamespacesDocument, args, {
			additionalTypenames: ['Namespace', 'ComicTag']
		})
	);
}

export async function deleteNamespaces(
	client: Client,
	args: gql.DeleteNamespacesMutationVariables
) {
	return await handleMutation(
		client.mutation(gql.DeleteNamespacesDocument, args, {
			additionalTypenames: ['NamespaceFilterResult', 'ComicTag']
		})
	);
}

export async function addTag(client: Client, args: gql.AddTagMutationVariables) {
	return await handleMutation(
		client.mutation(gql.AddTagDocument, args, {
			additionalTypenames: ['TagFilterResult', 'ComicTagFilterResult']
		})
	);
}

export async function updateTags(client: Client, args: gql.UpdateTagsMutationVariables) {
	return await handleMutation(
		client.mutation(gql.UpdateTagsDocument, args, {
			additionalTypenames: ['Tag', 'ComicTag']
		})
	);
}

export async function deleteTags(client: Client, args: gql.DeleteTagsMutationVariables) {
	return await handleMutation(
		client.mutation(gql.DeleteTagsDocument, args, {
			additionalTypenames: ['TagFilterResult', 'ComicTag']
		})
	);
}

export async function addWorld(client: Client, args: gql.AddWorldMutationVariables) {
	return await handleMutation(
		client.mutation(gql.AddWorldDocument, args, { additionalTypenames: ['WorldFilterResult'] })
	);
}

export async function updateWorlds(client: Client, args: gql.UpdateWorldsMutationVariables) {
	return await handleMutation(
		client.mutation(gql.UpdateWorldsDocument, args, { additionalTypenames: ['World'] })
	);
}

export async function deleteWorlds(client: Client, args: gql.DeleteWorldsMutationVariables) {
	return await handleMutation(
		client.mutation(gql.DeleteWorldsDocument, args, { additionalTypenames: ['World'] })
	);
}

export type ArtistInput = RequiredName<gql.UpdateArtistInput>;
export type CharacterInput = RequiredName<gql.UpdateCharacterInput>;
export type CircleInput = RequiredName<gql.UpdateCircleInput>;
export type NamespaceInput = RequiredName<gql.UpdateNamespaceInput>;
export type TagInput = RequiredName<gql.UpdateTagInput>;
export type WorldInput = RequiredName<gql.UpdateWorldInput>;