aboutsummaryrefslogtreecommitdiff
path: root/src/stores/forums.js
blob: ade01a7110e76ed0fa2c14d30e19ca7ba22d16e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { ApolloError } from '@apollo/client/core';
import { readable } from 'svelte/store';
import { client } from '$config/apollo';
import { GET_FORUMS } from '$data/queries';

const internals = {

  // The exported data structure

  initialValue: {
    loading: true,
    data: [],
    error: undefined
  }
};

export const forums = readable(internals.initialValue, (set) => {

  client.watchQuery({ query: GET_FORUMS }).subscribe((result) => {

    if (result.errors) {
      const error = new ApolloError({ graphQLErrors: result.errors });
      return set({
        loading: false,
        data: [],
        error
      });
    }

    set({
      loading: false,
      data: result.data.forums,
      error: undefined
    });
  });
});