From c1bc5993a694f6fd047a3881351827058042483b Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sat, 13 Mar 2021 15:28:29 +0100 Subject: Use routify and GraphQL server --- src/stores/forum.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/stores/forum.js (limited to 'src/stores/forum.js') diff --git a/src/stores/forum.js b/src/stores/forum.js new file mode 100644 index 0000000..e7d335e --- /dev/null +++ b/src/stores/forum.js @@ -0,0 +1,39 @@ +import { ApolloError } from '@apollo/client/core'; +import { readable } from 'svelte/store'; +import { client } from '$config/apollo'; +import { GET_FORUM } from '$data/queries'; + +const internals = { + + // The exported data structure + + initialValue: { + loading: true, + data: null, + error: undefined + } +}; + +export const getForum = function forum(id) { + + return readable(internals.initialValue, (set) => { + + client.watchQuery({ query: GET_FORUM, variables: { id } }).subscribe((result) => { + + if (result.errors) { + const error = new ApolloError({ graphQLErrors: result.errors }); + return set({ + loading: false, + data: null, + error + }); + } + + set({ + loading: false, + data: result.data.forum, + error: undefined + }); + }); + }); +}; -- cgit