position
topics {
id
- title,
- updated_at,
+ title
+ updated_at
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
+ }
+ }
+ }
+`;
--- /dev/null
+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));
+ });
+};
-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 } });
-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: [] });
--- /dev/null
+import { store } from './apollo';
+import { GET_POST } from '$data/queries';
+
+export const getPost = (id) => store({ key: 'post', query: GET_POST, variables: { id } });
--- /dev/null
+import { store } from './apollo';
+import { GET_TAG } from '$data/queries';
+
+export const getTag = (id) => store({ key: 'tag', query: GET_TAG, variables: { id } });
--- /dev/null
+import { store } from './apollo';
+import { GET_TOPIC } from '$data/queries';
+
+export const getTopic = (id) => store({ key: 'topic', query: GET_TOPIC, variables: { id } });