1 import { ApolloError } from '@apollo/client/core';
2 import { readable } from 'svelte/store';
3 import { client } from '$lib/config/apollo';
4 import type { DocumentNode, ApolloQueryResult } from '@apollo/client/core';
6 import type { Readable } from 'svelte/store';
9 * This is a generic store for use with apollo
12 type ApolloStoreConfiguration<Type> = {
15 initialValue?: Type | void
19 type ApolloStoreState<Type> = {
25 export const store = function store<Type>({ key, query, initialValue = null, variables = {} }: ApolloStoreConfiguration<Type>): Readable<ApolloStoreState<Type>> {
27 const initialState: ApolloStoreState<Type> = {
37 const handleError = function (error: Error) {
46 client.watchQuery({ query, variables }).subscribe(
47 (result: ApolloQueryResult<Type>) => {
50 const error = new ApolloError({ graphQLErrors: result.errors });
51 return handleError(error);
56 data: result.data[key],
60 (error: Error) => handleError(error)