]> git.r.bdr.sh - rbdr/forum/blob - src/stores/forums.js
ade01a7110e76ed0fa2c14d30e19ca7ba22d16e1
[rbdr/forum] / src / stores / forums.js
1 import { ApolloError } from '@apollo/client/core';
2 import { readable } from 'svelte/store';
3 import { client } from '$config/apollo';
4 import { GET_FORUMS } from '$data/queries';
5
6 const internals = {
7
8 // The exported data structure
9
10 initialValue: {
11 loading: true,
12 data: [],
13 error: undefined
14 }
15 };
16
17 export const forums = readable(internals.initialValue, (set) => {
18
19 client.watchQuery({ query: GET_FORUMS }).subscribe((result) => {
20
21 if (result.errors) {
22 const error = new ApolloError({ graphQLErrors: result.errors });
23 return set({
24 loading: false,
25 data: [],
26 error
27 });
28 }
29
30 set({
31 loading: false,
32 data: result.data.forums,
33 error: undefined
34 });
35 });
36 });