X-Git-Url: https://git.r.bdr.sh/rbdr/forum/blobdiff_plain/a7cf03c192470cbab13edeb1aec99e0c66dede10..0472e8073b9df492596e550fc79c42ca93ba14c0:/src/lib/stores/apollo.ts?ds=inline diff --git a/src/lib/stores/apollo.ts b/src/lib/stores/apollo.ts index 12463c3..4ef1986 100644 --- a/src/lib/stores/apollo.ts +++ b/src/lib/stores/apollo.ts @@ -10,55 +10,53 @@ import type { Readable } from 'svelte/store'; */ type ApolloStoreConfiguration = { - key: string, - query: DocumentNode, - initialValue?: Type | void - variables?: object + key: string; + query: DocumentNode; + initialValue?: Type | void; + variables?: object; }; type ApolloStoreState = { - loading: boolean, - data: Type | void, - error: Error | void + loading: boolean; + data: Type | void; + error: Error | void; }; -export const store = function store({ key, query, initialValue = null, variables = {} }: ApolloStoreConfiguration): Readable> { - - const initialState: ApolloStoreState = { - loading: true, - data: initialValue, - error: undefined - }; - - return readable( - initialState, - (set) => { - - const handleError = function (error: Error) { - - return set({ - loading: false, - data: initialValue, - error - }); - }; - - client.watchQuery({ query, variables }).subscribe( - (result: ApolloQueryResult) => { - - if (result.errors) { - const error = new ApolloError({ graphQLErrors: result.errors }); - return handleError(error); - } - - set({ - loading: false, - data: result.data[key], - error: undefined - }); - }, - (error: Error) => handleError(error) - ); - } - ); +export const store = function store({ + key, + query, + initialValue = null, + variables = {} +}: ApolloStoreConfiguration): Readable> { + const initialState: ApolloStoreState = { + loading: true, + data: initialValue, + error: undefined + }; + + return readable(initialState, (set) => { + const handleError = function (error: Error) { + return set({ + loading: false, + data: initialValue, + error + }); + }; + + client.watchQuery({ query, variables }).subscribe( + (result: ApolloQueryResult) => { + if (result.errors) { + const error = new ApolloError({ graphQLErrors: result.errors }); + return handleError(error); + } + + set({ + loading: false, + data: result.data[key], + error: undefined + }); + }, + (error: Error) => handleError(error) + ); + }); };