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; const comicTypes = ['Comic']; const comicUpsertTypes = comicTypes.concat([ 'Artist', 'Character', 'Circle', 'Namespace', 'Collection', 'Tag', 'World' ]); function handleResult(result: OperationResult): Promise { 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( mutation: OperationResultSource> ) { 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; export type CharacterInput = RequiredName; export type CircleInput = RequiredName; export type NamespaceInput = RequiredName; export type TagInput = RequiredName; export type WorldInput = RequiredName;