import { Client, queryStore, type OperationResult } from '@urql/svelte'; import { isError } from './Utils'; import * as gql from './graphql'; 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('Query resolved, but result contains no data.'); } const data = Object.values(result.data)[0]; if (isError(data)) { reject(data.message); } else { resolve(data); } reject('This should not happen.'); }); } export function comicTagList(client: Client, args?: gql.ComicTagListQueryVariables) { return queryStore({ client: client, query: gql.ComicTagListDocument, context: { additionalTypenames: ['Namespace', 'Tag'] }, variables: args }); } export function artistList(client: Client) { return queryStore({ client: client, query: gql.ArtistListDocument, context: { additionalTypenames: ['Artist'] } }); } export function characterList(client: Client) { return queryStore({ client: client, query: gql.CharacterListDocument, context: { additionalTypenames: ['Character'] } }); } export function circleList(client: Client) { return queryStore({ client: client, query: gql.CircleListDocument, context: { additionalTypenames: ['Circle'] } }); } export function worldList(client: Client) { return queryStore({ client: client, query: gql.WorldListDocument, context: { additionalTypenames: ['World'] } }); } export function namespaceList(client: Client) { return queryStore({ client: client, query: gql.NamespaceListDocument, context: { additionalTypenames: ['Namespace'] } }); } export function comicScrapersQuery(client: Client, args: gql.ComicScrapersQueryVariables) { return queryStore({ client: client, query: gql.ComicScrapersDocument, variables: args, context: { additionalTypenames: ['Comic'] } }); } export async function scrapeComic(client: Client, args: gql.ScrapeComicQueryVariables) { return await client .query(gql.ScrapeComicDocument, args, { additionalTypenames: ['Comic'] }) .toPromise(); } export function archiveQuery(client: Client, args: gql.ArchiveQueryVariables) { return queryStore({ client: client, query: gql.ArchiveDocument, variables: args, context: { additionalTypenames: ['Archive'] } }); } export function archivesQuery(client: Client, args: gql.ArchivesQueryVariables) { return queryStore({ client: client, query: gql.ArchivesDocument, variables: args, context: { additionalTypenames: ['Archive'] } }); } export function artistsQuery(client: Client, args?: gql.ArtistsQueryVariables) { return queryStore({ client: client, query: gql.ArtistsDocument, variables: args, context: { additionalTypenames: ['Artist'] } }); } export function charactersQuery(client: Client, args?: gql.CharactersQueryVariables) { return queryStore({ client: client, query: gql.CharactersDocument, variables: args, context: { additionalTypenames: ['Character'] } }); } export function circlesQuery(client: Client, args?: gql.CirclesQueryVariables) { return queryStore({ client: client, query: gql.CirclesDocument, variables: args, context: { additionalTypenames: ['Circle'] } }); } export function comicQuery(client: Client, args: gql.ComicQueryVariables) { return queryStore({ client: client, query: gql.ComicDocument, variables: args, context: { additionalTypenames: ['Comic'] } }); } export function comicsQuery(client: Client, args?: gql.ComicsQueryVariables) { return queryStore({ client: client, query: gql.ComicsDocument, variables: args, context: { additionalTypenames: ['Comic'] } }); } export function namespacesQuery(client: Client, args?: gql.NamespacesQueryVariables) { return queryStore({ client: client, query: gql.NamespacesDocument, variables: args, context: { additionalTypenames: ['Namespace'] } }); } export function tagsQuery(client: Client, args?: gql.TagsQueryVariables) { return queryStore({ client: client, query: gql.TagsDocument, variables: args, context: { additionalTypenames: ['Tag'] } }); } export function worldsQuery(client: Client, args?: gql.WorldsQueryVariables) { return queryStore({ client: client, query: gql.WorldsDocument, variables: args, context: { additionalTypenames: ['World'] } }); } export function frontpageQuery(client: Client) { return queryStore({ client: client, query: gql.FrontpageDocument, requestPolicy: 'network-only' }); } export function fetchArtist(client: Client, id: number) { return client .query(gql.ArtistDocument, { id }, { requestPolicy: 'cache-and-network' }) .toPromise() .then(handleResult); } export function fetchCharacter(client: Client, id: number) { return client .query(gql.CharacterDocument, { id }, { requestPolicy: 'cache-and-network' }) .toPromise() .then(handleResult); } export function fetchCircle(client: Client, id: number) { return client .query(gql.CircleDocument, { id }, { requestPolicy: 'cache-and-network' }) .toPromise() .then(handleResult); } export function fetchNamespace(client: Client, id: number) { return client .query(gql.NamespaceDocument, { id }, { requestPolicy: 'cache-and-network' }) .toPromise() .then(handleResult); } export function fetchTag(client: Client, id: number) { return client .query(gql.TagDocument, { id }, { requestPolicy: 'cache-and-network' }) .toPromise() .then(handleResult); } export function fetchWorld(client: Client, id: number) { return client .query(gql.WorldDocument, { id }, { requestPolicy: 'cache-and-network' }) .toPromise() .then(handleResult); }