]> git.r.bdr.sh - rbdr/forum/commitdiff
Add stores for GraphQL data:
authorRuben Beltran del Rio <redacted>
Sun, 14 Mar 2021 21:41:16 +0000 (22:41 +0100)
committerRuben Beltran del Rio <redacted>
Sun, 14 Mar 2021 21:41:16 +0000 (22:41 +0100)
src/data/queries.js
src/stores/apollo.js [new file with mode: 0644]
src/stores/forum.js
src/stores/forums.js
src/stores/post.js [new file with mode: 0644]
src/stores/tag.js [new file with mode: 0644]
src/stores/topic.js [new file with mode: 0644]

index de223cb110412e3999c18b3b3ae50d9fb6662384..93e4b233575e440b2d1ed40cb10f1449d00d2e46 100644 (file)
@@ -20,10 +20,71 @@ export const GET_FORUM = gql`
       position
       topics {
         id
       position
       topics {
         id
-        title,
-        updated_at,
+        title
+        updated_at
         ttl
       }
     }
   }
 `;
         ttl
       }
     }
   }
 `;
+
+export const GET_TAG = gql`
+  query GetTag($id: ID!) {
+    tag(id: $id) {
+      id
+      topics {
+        id
+        title
+        updated_at
+        ttl
+      }
+    }
+  }
+`;
+
+export const GET_TOPIC = gql`
+  query GetTopic($id: ID!) {
+    topic(id: $id) {
+      id
+      title
+      updated_at
+      ttl
+      forum {
+        id
+        glyph
+        label
+      }
+      tags {
+        id
+        weight
+      }
+      posts {
+        id
+        text
+        created_at
+        author {
+          id
+          handle
+        }
+      }
+    }
+  }
+`;
+
+export const GET_POST = gql`
+  query GetPost($id: ID!) {
+    post(id: $id) {
+      id
+      text
+      created_at
+      author {
+        id
+        handle
+      }
+      topic {
+        id
+        title
+      }
+    }
+  }
+`;
diff --git a/src/stores/apollo.js b/src/stores/apollo.js
new file mode 100644 (file)
index 0000000..e2deecd
--- /dev/null
@@ -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));
+  });
+};
index e7d335e14b0c7b729e3f0fc95a1ee5840821650f..110da5792d749de66192166268bd047f64d6855a 100644 (file)
@@ -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';
 
 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 } });
index ade01a7110e76ed0fa2c14d30e19ca7ba22d16e1..5cce7ba60f836584a8ecc1316c08626767a14229 100644 (file)
@@ -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';
 
 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 (file)
index 0000000..ed19cd6
--- /dev/null
@@ -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 (file)
index 0000000..742db51
--- /dev/null
@@ -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 (file)
index 0000000..3225a83
--- /dev/null
@@ -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 } });