From: Ruben Beltran del Rio Date: Sun, 14 Mar 2021 21:41:16 +0000 (+0100) Subject: Add stores for GraphQL data: X-Git-Url: https://git.r.bdr.sh/rbdr/forum/commitdiff_plain/18ced3bfaff8ca40edb1a96e631ba1553a3a7a6d?ds=inline Add stores for GraphQL data: --- diff --git a/src/data/queries.js b/src/data/queries.js index de223cb..93e4b23 100644 --- a/src/data/queries.js +++ b/src/data/queries.js @@ -20,10 +20,71 @@ export const GET_FORUM = gql` 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 + } + } + } +`; 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 } });