2023-11-28 13:18:25 +00:00
|
|
|
import type {
|
|
|
|
Author,
|
2024-02-21 14:25:28 +00:00
|
|
|
AuthorFollows,
|
2024-02-04 11:25:21 +00:00
|
|
|
CommonResult,
|
|
|
|
FollowingEntity,
|
2023-11-28 13:18:25 +00:00
|
|
|
LoadShoutsOptions,
|
2024-02-04 11:25:21 +00:00
|
|
|
MutationDelete_ShoutArgs,
|
2023-11-28 13:18:25 +00:00
|
|
|
ProfileInput,
|
2023-11-28 15:36:00 +00:00
|
|
|
QueryLoad_Authors_ByArgs,
|
2023-12-18 01:15:49 +00:00
|
|
|
QueryLoad_Shouts_Random_TopArgs,
|
2024-02-04 11:25:21 +00:00
|
|
|
QueryLoad_Shouts_SearchArgs,
|
|
|
|
ReactionBy,
|
|
|
|
ReactionInput,
|
|
|
|
Shout,
|
|
|
|
ShoutInput,
|
|
|
|
Topic,
|
2023-11-28 13:18:25 +00:00
|
|
|
} from '../schema/core.gen'
|
|
|
|
|
2023-12-03 10:22:42 +00:00
|
|
|
import { createGraphQLClient } from '../createGraphQLClient'
|
2023-11-28 13:18:25 +00:00
|
|
|
import createArticle from '../mutation/core/article-create'
|
|
|
|
import deleteShout from '../mutation/core/article-delete'
|
|
|
|
import updateArticle from '../mutation/core/article-update'
|
2023-12-27 23:04:12 +00:00
|
|
|
import rateAuthor from '../mutation/core/author-rate'
|
2024-02-04 11:25:21 +00:00
|
|
|
import updateAuthor from '../mutation/core/author-update'
|
2023-11-28 13:18:25 +00:00
|
|
|
import followMutation from '../mutation/core/follow'
|
|
|
|
import reactionCreate from '../mutation/core/reaction-create'
|
|
|
|
import reactionDestroy from '../mutation/core/reaction-destroy'
|
|
|
|
import reactionUpdate from '../mutation/core/reaction-update'
|
|
|
|
import unfollowMutation from '../mutation/core/unfollow'
|
|
|
|
import shoutLoad from '../query/core/article-load'
|
2024-03-06 09:04:33 +00:00
|
|
|
import getMyShout from '../query/core/article-my'
|
2023-11-28 13:18:25 +00:00
|
|
|
import shoutsLoadBy from '../query/core/articles-load-by'
|
2023-11-28 15:36:00 +00:00
|
|
|
import draftsLoad from '../query/core/articles-load-drafts'
|
|
|
|
import myFeed from '../query/core/articles-load-feed'
|
2023-12-18 01:15:49 +00:00
|
|
|
import loadShoutsTopRandom from '../query/core/articles-load-random-top'
|
2023-12-24 12:56:30 +00:00
|
|
|
import articlesLoadRandomTopic from '../query/core/articles-load-random-topic'
|
2023-11-29 12:37:27 +00:00
|
|
|
import shoutsLoadSearch from '../query/core/articles-load-search'
|
2023-12-18 01:15:49 +00:00
|
|
|
import loadShoutsUnrated from '../query/core/articles-load-unrated'
|
2023-11-28 13:18:25 +00:00
|
|
|
import authorBy from '../query/core/author-by'
|
|
|
|
import authorFollowers from '../query/core/author-followers'
|
2024-02-21 14:25:28 +00:00
|
|
|
import authorFollows from '../query/core/author-follows'
|
2023-12-14 00:04:07 +00:00
|
|
|
import authorId from '../query/core/author-id'
|
2023-12-24 17:29:16 +00:00
|
|
|
import authorsAll from '../query/core/authors-all'
|
2023-11-28 13:18:25 +00:00
|
|
|
import authorsLoadBy from '../query/core/authors-load-by'
|
|
|
|
import reactionsLoadBy from '../query/core/reactions-load-by'
|
|
|
|
import topicBySlug from '../query/core/topic-by-slug'
|
|
|
|
import topicsAll from '../query/core/topics-all'
|
|
|
|
import topicsRandomQuery from '../query/core/topics-random'
|
|
|
|
|
2023-12-03 10:22:42 +00:00
|
|
|
const publicGraphQLClient = createGraphQLClient('core')
|
2023-11-28 13:18:25 +00:00
|
|
|
|
|
|
|
export const apiClient = {
|
2023-12-03 10:22:42 +00:00
|
|
|
private: null,
|
2024-01-31 12:34:15 +00:00
|
|
|
connect: (token: string) => {
|
|
|
|
// NOTE: use it after token appears
|
|
|
|
apiClient.private = createGraphQLClient('core', token)
|
|
|
|
},
|
2023-12-03 10:22:42 +00:00
|
|
|
|
2023-12-18 01:15:49 +00:00
|
|
|
getRandomTopShouts: async (params: QueryLoad_Shouts_Random_TopArgs) => {
|
|
|
|
const response = await publicGraphQLClient.query(loadShoutsTopRandom, params).toPromise()
|
2023-12-24 08:16:41 +00:00
|
|
|
if (!response.data) console.error('[graphql.core] load_shouts_random_top failed', response)
|
|
|
|
|
2023-12-20 07:45:29 +00:00
|
|
|
return response.data.load_shouts_random_top
|
2023-12-18 01:15:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getUnratedShouts: async (limit = 50, offset = 0) => {
|
|
|
|
const response = await apiClient.private.query(loadShoutsUnrated, { limit, offset }).toPromise()
|
2023-12-24 08:16:41 +00:00
|
|
|
if (!response.data) console.error('[graphql.core] load_shouts_unrated', response)
|
2023-12-18 01:15:49 +00:00
|
|
|
|
|
|
|
return response.data.load_shouts_unrated
|
|
|
|
},
|
|
|
|
|
2023-12-27 22:50:42 +00:00
|
|
|
rateAuthor: async ({ rated_slug, value }: { rated_slug: string; value: number }) => {
|
|
|
|
const response = await apiClient.private.mutation(rateAuthor, { rated_slug, value }).toPromise()
|
|
|
|
if (!response.data) console.error('[graphql.client.core] get_topics_random failed', response)
|
|
|
|
|
|
|
|
return response.data.rate_author
|
|
|
|
},
|
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
getRandomTopics: async ({ amount }: { amount: number }) => {
|
|
|
|
const response = await publicGraphQLClient.query(topicsRandomQuery, { amount }).toPromise()
|
2023-12-24 08:16:41 +00:00
|
|
|
if (!response.data) console.error('[graphql.client.core] get_topics_random failed', response)
|
2023-11-28 13:18:25 +00:00
|
|
|
|
|
|
|
return response.data.get_topics_random
|
|
|
|
},
|
|
|
|
|
2024-02-21 14:25:28 +00:00
|
|
|
getRandomTopicShouts: async (limit: number): Promise<CommonResult> => {
|
2023-12-23 07:45:37 +00:00
|
|
|
const resp = await publicGraphQLClient.query(articlesLoadRandomTopic, { limit }).toPromise()
|
2023-12-24 08:16:41 +00:00
|
|
|
if (!resp.data) console.error('[graphql.client.core] load_shouts_random_topic', resp)
|
2023-12-26 14:36:12 +00:00
|
|
|
return resp.data.load_shouts_random_topic
|
2023-12-23 07:45:37 +00:00
|
|
|
},
|
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
follow: async ({ what, slug }: { what: FollowingEntity; slug: string }) => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private.mutation(followMutation, { what, slug }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
return response.data.follow
|
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
unfollow: async ({ what, slug }: { what: FollowingEntity; slug: string }) => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private.mutation(unfollowMutation, { what, slug }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
return response.data.unfollow
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllTopics: async () => {
|
|
|
|
const response = await publicGraphQLClient.query(topicsAll, {}).toPromise()
|
2023-12-24 08:16:41 +00:00
|
|
|
if (!response.data) console.error('[graphql.client.core] get_topics_all', response)
|
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
return response.data.get_topics_all
|
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2023-12-24 12:56:30 +00:00
|
|
|
getAllAuthors: async () => {
|
|
|
|
const response = await publicGraphQLClient.query(authorsAll, {}).toPromise()
|
2023-12-24 21:12:42 +00:00
|
|
|
if (!response.data) console.error('[graphql.client.core] getAllAuthors', response)
|
2023-12-24 08:16:41 +00:00
|
|
|
|
2023-12-24 12:56:30 +00:00
|
|
|
return response.data.get_authors_all
|
2023-11-28 13:18:25 +00:00
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2023-12-13 23:56:44 +00:00
|
|
|
getAuthor: async (params: { slug?: string; author_id?: number }): Promise<Author> => {
|
2023-11-28 13:18:25 +00:00
|
|
|
const response = await publicGraphQLClient.query(authorBy, params).toPromise()
|
|
|
|
return response.data.get_author
|
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2023-12-13 23:56:44 +00:00
|
|
|
getAuthorId: async (params: { user: string }): Promise<Author> => {
|
|
|
|
const response = await publicGraphQLClient.query(authorId, params).toPromise()
|
|
|
|
return response.data.get_author_id
|
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
getAuthorFollowers: async ({ slug }: { slug: string }): Promise<Author[]> => {
|
|
|
|
const response = await publicGraphQLClient.query(authorFollowers, { slug }).toPromise()
|
|
|
|
return response.data.get_author_followers
|
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
|
|
|
getAuthorFollows: async (params: {
|
|
|
|
slug?: string
|
|
|
|
author_id?: number
|
|
|
|
user?: string
|
|
|
|
}): Promise<AuthorFollows> => {
|
|
|
|
const response = await publicGraphQLClient.query(authorFollows, params).toPromise()
|
|
|
|
return response.data.get_author_follows
|
2024-01-31 12:34:15 +00:00
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2024-02-03 14:58:41 +00:00
|
|
|
updateAuthor: async (input: ProfileInput) => {
|
|
|
|
const response = await apiClient.private.mutation(updateAuthor, { profile: input }).toPromise()
|
|
|
|
return response.data.update_author
|
2023-11-28 13:18:25 +00:00
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
getTopic: async ({ slug }: { slug: string }): Promise<Topic> => {
|
|
|
|
const response = await publicGraphQLClient.query(topicBySlug, { slug }).toPromise()
|
|
|
|
return response.data.get_topic
|
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
createArticle: async ({ article }: { article: ShoutInput }): Promise<Shout> => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private.mutation(createArticle, { shout: article }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
return response.data.create_shout.shout
|
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
updateArticle: async ({
|
2024-02-02 21:19:58 +00:00
|
|
|
shout_id,
|
|
|
|
shout_input,
|
2023-11-28 13:18:25 +00:00
|
|
|
publish,
|
|
|
|
}: {
|
2024-02-02 21:19:58 +00:00
|
|
|
shout_id: number
|
|
|
|
shout_input?: ShoutInput
|
2023-11-28 13:18:25 +00:00
|
|
|
publish: boolean
|
2024-02-17 15:03:01 +00:00
|
|
|
}): Promise<CommonResult> => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private
|
2024-02-02 21:19:58 +00:00
|
|
|
.mutation(updateArticle, { shout_id, shout_input, publish })
|
2023-11-28 13:18:25 +00:00
|
|
|
.toPromise()
|
|
|
|
console.debug('[graphql.client.core] updateArticle:', response.data)
|
2024-02-17 15:03:01 +00:00
|
|
|
return response.data.update_shout
|
2023-11-28 13:18:25 +00:00
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2024-01-31 18:15:08 +00:00
|
|
|
deleteShout: async (params: MutationDelete_ShoutArgs): Promise<void> => {
|
|
|
|
const response = await apiClient.private.mutation(deleteShout, params).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
console.debug('[graphql.client.core] deleteShout:', response)
|
|
|
|
},
|
2024-02-21 14:25:28 +00:00
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
getDrafts: async (): Promise<Shout[]> => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private.query(draftsLoad, {}).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
console.debug('[graphql.client.core] getDrafts:', response)
|
2024-03-05 15:52:34 +00:00
|
|
|
return response.data.get_shouts_drafts
|
2023-11-28 13:18:25 +00:00
|
|
|
},
|
|
|
|
createReaction: async (input: ReactionInput) => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private.mutation(reactionCreate, { reaction: input }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
console.debug('[graphql.client.core] createReaction:', response)
|
2024-03-18 07:40:31 +00:00
|
|
|
return response.data.create_reaction
|
2023-11-28 13:18:25 +00:00
|
|
|
},
|
2024-02-16 16:27:38 +00:00
|
|
|
destroyReaction: async (reaction_id: number) => {
|
|
|
|
const response = await apiClient.private.mutation(reactionDestroy, { reaction_id }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
console.debug('[graphql.client.core] destroyReaction:', response)
|
2024-03-07 07:20:50 +00:00
|
|
|
return response.data.delete_reaction
|
2023-11-28 13:18:25 +00:00
|
|
|
},
|
2024-02-16 16:58:24 +00:00
|
|
|
updateReaction: async (reaction: ReactionInput) => {
|
2024-02-16 17:04:05 +00:00
|
|
|
const response = await apiClient.private.mutation(reactionUpdate, { reaction }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
console.debug('[graphql.client.core] updateReaction:', response)
|
|
|
|
return response.data.update_reaction.reaction
|
|
|
|
},
|
2023-12-24 12:56:30 +00:00
|
|
|
loadAuthorsBy: async (args: QueryLoad_Authors_ByArgs) => {
|
2023-11-29 12:37:27 +00:00
|
|
|
const resp = await publicGraphQLClient.query(authorsLoadBy, args).toPromise()
|
2023-12-24 12:56:30 +00:00
|
|
|
console.debug('[graphql.client.core] authorsLoadBy:', resp)
|
2023-11-28 13:18:25 +00:00
|
|
|
return resp.data.load_authors_by
|
|
|
|
},
|
2024-03-05 13:44:51 +00:00
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
getShoutBySlug: async (slug: string) => {
|
2023-11-29 12:37:27 +00:00
|
|
|
const resp = await publicGraphQLClient.query(shoutLoad, { slug }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
return resp.data.get_shout
|
|
|
|
},
|
2024-03-05 13:01:47 +00:00
|
|
|
|
|
|
|
getMyShout: async (shout_id: number) => {
|
2024-03-06 19:16:55 +00:00
|
|
|
await apiClient.private
|
2024-03-06 07:30:07 +00:00
|
|
|
const resp = await apiClient.private.query(getMyShout, { shout_id }).toPromise()
|
2024-03-05 13:01:47 +00:00
|
|
|
if (resp.error) console.error(resp)
|
|
|
|
|
2024-03-06 07:30:07 +00:00
|
|
|
return resp.data.get_my_shout
|
2023-11-28 13:18:25 +00:00
|
|
|
},
|
|
|
|
|
2023-11-29 12:37:27 +00:00
|
|
|
getShouts: async (options: LoadShoutsOptions) => {
|
|
|
|
const resp = await publicGraphQLClient.query(shoutsLoadBy, { options }).toPromise()
|
|
|
|
if (resp.error) console.error(resp)
|
2023-11-28 13:18:25 +00:00
|
|
|
|
2024-01-31 12:34:15 +00:00
|
|
|
return resp.data?.load_shouts_by
|
2023-11-28 13:18:25 +00:00
|
|
|
},
|
|
|
|
|
2023-12-25 04:01:52 +00:00
|
|
|
getShoutsSearch: async ({ text, limit, offset }: QueryLoad_Shouts_SearchArgs) => {
|
|
|
|
const resp = await publicGraphQLClient.query(shoutsLoadSearch, { text, limit, offset }).toPromise()
|
2023-11-29 12:37:27 +00:00
|
|
|
if (resp.error) console.error(resp)
|
2023-11-29 08:23:08 +00:00
|
|
|
|
|
|
|
return resp.data.load_shouts_search
|
|
|
|
},
|
|
|
|
|
2023-11-29 12:37:27 +00:00
|
|
|
getMyFeed: async (options: LoadShoutsOptions) => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const resp = await apiClient.private.query(myFeed, { options }).toPromise()
|
2023-11-29 12:37:27 +00:00
|
|
|
if (resp.error) console.error(resp)
|
2023-11-28 13:18:25 +00:00
|
|
|
|
|
|
|
return resp.data.load_shouts_feed
|
|
|
|
},
|
|
|
|
|
2023-11-30 08:07:31 +00:00
|
|
|
getReactionsBy: async ({ by, limit, offset }: { by: ReactionBy; limit?: number; offset?: number }) => {
|
2023-11-28 13:18:25 +00:00
|
|
|
const resp = await publicGraphQLClient
|
2023-11-30 08:07:31 +00:00
|
|
|
.query(reactionsLoadBy, { by, limit: limit ?? 1000, offset: offset ?? 0 })
|
2023-11-28 13:18:25 +00:00
|
|
|
.toPromise()
|
|
|
|
return resp.data.load_reactions_by
|
|
|
|
},
|
|
|
|
}
|