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