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>({
30 }: ApolloStoreConfiguration<Type>): Readable<ApolloStoreState<Type>> {
31 const initialState: ApolloStoreState<Type> = {
37 return readable(initialState, (set) => {
38 const handleError = function (error: Error) {
46 client.watchQuery({ query, variables }).subscribe(
47 (result: ApolloQueryResult<Type>) => {
49 const error = new ApolloError({ graphQLErrors: result.errors });
50 return handleError(error);
55 data: result.data[key],
59 (error: Error) => handleError(error)