aboutsummaryrefslogtreecommitdiff
path: root/src/stores
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2021-03-14 22:41:16 +0100
committerRuben Beltran del Rio <ruben@unlimited.pizza>2021-03-14 22:41:16 +0100
commit18ced3bfaff8ca40edb1a96e631ba1553a3a7a6d (patch)
tree450ee3c84e7400e99ba30261e705b2db619d9bcf /src/stores
parentc1bc5993a694f6fd047a3881351827058042483b (diff)
Add stores for GraphQL data:
Diffstat (limited to 'src/stores')
-rw-r--r--src/stores/apollo.js40
-rw-r--r--src/stores/forum.js39
-rw-r--r--src/stores/forums.js36
-rw-r--r--src/stores/post.js4
-rw-r--r--src/stores/tag.js4
-rw-r--r--src/stores/topic.js4
6 files changed, 56 insertions, 71 deletions
diff --git a/src/stores/apollo.js b/src/stores/apollo.js
new file mode 100644
index 0000000..e2deecd
--- /dev/null
+++ b/src/stores/apollo.js
@@ -0,0 +1,40 @@
+import { ApolloError } from '@apollo/client/core';
+import { readable } from 'svelte/store';
+import { client } from '$config/apollo';
+
+/*
+ * This is a generic store for use with apollo
+ */
+
+export const store = function store({ key, query, initialValue = null, variables = {} }) {
+
+ return readable({
+ loading: true,
+ data: initialValue,
+ error: undefined
+ }, (set) => {
+
+ const handleError = function (error) {
+
+ return set({
+ loading: false,
+ data: initialValue,
+ error
+ });
+ };
+
+ client.watchQuery({ query, variables }).subscribe((result) => {
+
+ if (result.errors) {
+ const error = new ApolloError({ graphQLErrors: result.errors });
+ return handleError(error);
+ }
+
+ set({
+ loading: false,
+ data: result.data[key],
+ error: undefined
+ });
+ }, (error) => handleError(error));
+ });
+};
diff --git a/src/stores/forum.js b/src/stores/forum.js
index e7d335e..110da57 100644
--- a/src/stores/forum.js
+++ b/src/stores/forum.js
@@ -1,39 +1,4 @@
-import { ApolloError } from '@apollo/client/core';
-import { readable } from 'svelte/store';
-import { client } from '$config/apollo';
+import { store } from './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
- });
- });
- });
-};
+export const getForum = (id) => store({ key: 'forum', query: GET_FORUM, variables: { id } });
diff --git a/src/stores/forums.js b/src/stores/forums.js
index ade01a7..5cce7ba 100644
--- a/src/stores/forums.js
+++ b/src/stores/forums.js
@@ -1,36 +1,4 @@
-import { ApolloError } from '@apollo/client/core';
-import { readable } from 'svelte/store';
-import { client } from '$config/apollo';
+import { store } from './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
- });
- });
-});
+export const forums = store({ key: 'forums', query: GET_FORUMS, initialValue: [] });
diff --git a/src/stores/post.js b/src/stores/post.js
new file mode 100644
index 0000000..ed19cd6
--- /dev/null
+++ b/src/stores/post.js
@@ -0,0 +1,4 @@
+import { store } from './apollo';
+import { GET_POST } from '$data/queries';
+
+export const getPost = (id) => store({ key: 'post', query: GET_POST, variables: { id } });
diff --git a/src/stores/tag.js b/src/stores/tag.js
new file mode 100644
index 0000000..742db51
--- /dev/null
+++ b/src/stores/tag.js
@@ -0,0 +1,4 @@
+import { store } from './apollo';
+import { GET_TAG } from '$data/queries';
+
+export const getTag = (id) => store({ key: 'tag', query: GET_TAG, variables: { id } });
diff --git a/src/stores/topic.js b/src/stores/topic.js
new file mode 100644
index 0000000..3225a83
--- /dev/null
+++ b/src/stores/topic.js
@@ -0,0 +1,4 @@
+import { store } from './apollo';
+import { GET_TOPIC } from '$data/queries';
+
+export const getTopic = (id) => store({ key: 'topic', query: GET_TOPIC, variables: { id } });