]> git.r.bdr.sh - rbdr/forum/blame - src/stores/forum.js
Use routify and GraphQL server
[rbdr/forum] / src / stores / forum.js
CommitLineData
c1bc5993
RBR
1import { ApolloError } from '@apollo/client/core';
2import { readable } from 'svelte/store';
3import { client } from '$config/apollo';
4import { GET_FORUM } from '$data/queries';
5
6const internals = {
7
8 // The exported data structure
9
10 initialValue: {
11 loading: true,
12 data: null,
13 error: undefined
14 }
15};
16
17export 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};