2023-11-28 13:18:25 +00:00
|
|
|
import type {
|
|
|
|
FollowingEntity,
|
|
|
|
ShoutInput,
|
|
|
|
Topic,
|
|
|
|
Author,
|
|
|
|
LoadShoutsOptions,
|
|
|
|
ProfileInput,
|
|
|
|
ReactionInput,
|
|
|
|
ReactionBy,
|
|
|
|
Shout,
|
|
|
|
Result,
|
2023-11-28 15:36:00 +00:00
|
|
|
QueryLoad_Authors_ByArgs,
|
2023-11-29 08:23:08 +00:00
|
|
|
QueryLoad_Shouts_SearchArgs,
|
2023-12-18 01:15:49 +00:00
|
|
|
QueryLoad_Shouts_Random_TopArgs,
|
2024-01-31 12:34:15 +00:00
|
|
|
Community,
|
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'
|
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 updateProfile from '../mutation/core/update-profile'
|
|
|
|
import shoutLoad from '../query/core/article-load'
|
|
|
|
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'
|
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 mySubscriptions from '../query/core/my-followed'
|
|
|
|
import reactionsLoadBy from '../query/core/reactions-load-by'
|
|
|
|
import topicBySlug from '../query/core/topic-by-slug'
|
|
|
|
import topicsAll from '../query/core/topics-all'
|
2024-01-31 12:34:15 +00:00
|
|
|
import authorFollowedAuthors from '../query/core/authors-followed-by'
|
|
|
|
import authorFollowedTopics from '../query/core/topics-followed-by'
|
|
|
|
import authorFollowedCommunities from '../query/core/communities-followed-by'
|
2023-11-28 13:18:25 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
|
2023-12-23 07:45:37 +00:00
|
|
|
getRandomTopicShouts: async (limit: number): Promise<{ topic: Topic; shouts: Shout[] }> => {
|
|
|
|
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
|
|
|
|
},
|
|
|
|
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
|
|
|
|
},
|
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
|
|
|
},
|
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
|
|
|
|
},
|
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
|
|
|
|
},
|
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-01-31 12:34:15 +00:00
|
|
|
getAuthorFollowingAuthors: async ({ slug }: { slug: string }): Promise<Author[]> => {
|
|
|
|
const response = await publicGraphQLClient.query(authorFollowedAuthors, { slug }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
return response.data.get_author_followed
|
|
|
|
},
|
|
|
|
getAuthorFollowingTopics: async ({ slug }: { slug: string }): Promise<Topic[]> => {
|
2024-01-31 12:34:15 +00:00
|
|
|
const response = await publicGraphQLClient.query(authorFollowedTopics, { slug }).toPromise()
|
2023-12-27 21:23:16 +00:00
|
|
|
return response.data.get_topics_by_author
|
2023-11-28 13:18:25 +00:00
|
|
|
},
|
2024-01-31 12:34:15 +00:00
|
|
|
getAuthorFollowingCommunities: async ({ slug }: { slug: string }): Promise<Community[]> => {
|
|
|
|
const response = await publicGraphQLClient.query(authorFollowedCommunities, { slug }).toPromise()
|
|
|
|
return response.data.get_communities_by_author
|
|
|
|
},
|
2023-11-28 13:18:25 +00:00
|
|
|
updateProfile: async (input: ProfileInput) => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private.mutation(updateProfile, { profile: input }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
return response.data.update_profile
|
|
|
|
},
|
|
|
|
getTopic: async ({ slug }: { slug: string }): Promise<Topic> => {
|
|
|
|
const response = await publicGraphQLClient.query(topicBySlug, { slug }).toPromise()
|
|
|
|
return response.data.get_topic
|
|
|
|
},
|
|
|
|
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
|
|
|
|
},
|
|
|
|
updateArticle: async ({
|
|
|
|
shoutId,
|
|
|
|
shoutInput,
|
|
|
|
publish,
|
|
|
|
}: {
|
|
|
|
shoutId: number
|
|
|
|
shoutInput?: ShoutInput
|
|
|
|
publish: boolean
|
|
|
|
}): Promise<Shout> => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private
|
2023-11-28 13:18:25 +00:00
|
|
|
.mutation(updateArticle, { shoutId, shoutInput, publish })
|
|
|
|
.toPromise()
|
|
|
|
console.debug('[graphql.client.core] updateArticle:', response.data)
|
|
|
|
return response.data.update_shout.shout
|
|
|
|
},
|
2024-01-31 17:41:49 +00:00
|
|
|
deleteShout: async (params): Promise<void> => {
|
|
|
|
console.debug(params)
|
|
|
|
const response = await apiClient.private
|
|
|
|
.mutation(deleteShout, { shout_id: params?.shoutId })
|
|
|
|
.toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
console.debug('[graphql.client.core] deleteShout:', response)
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
return response.data.load_shouts_drafts
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
return response.data.create_reaction.reaction
|
|
|
|
},
|
|
|
|
destroyReaction: async (id: number) => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private.mutation(reactionDestroy, { id: id }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
console.debug('[graphql.client.core] destroyReaction:', response)
|
|
|
|
return response.data.delete_reaction.reaction
|
|
|
|
},
|
|
|
|
updateReaction: async (id: number, input: ReactionInput) => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private
|
2023-11-28 13:18:25 +00:00
|
|
|
.mutation(reactionUpdate, { id: id, reaction: input })
|
|
|
|
.toPromise()
|
|
|
|
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
|
|
|
|
},
|
|
|
|
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
|
|
|
|
},
|
|
|
|
getShoutById: async (shout_id: number) => {
|
2023-11-29 12:37:27 +00:00
|
|
|
const resp = await publicGraphQLClient.query(shoutLoad, { shout_id }).toPromise()
|
|
|
|
if (resp.error) console.error(resp)
|
2023-11-28 13:18:25 +00:00
|
|
|
|
|
|
|
return resp.data.get_shout
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
getMySubscriptions: async (): Promise<Result> => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const resp = await apiClient.private.query(mySubscriptions, {}).toPromise()
|
2023-11-29 12:37:27 +00:00
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
return resp.data.get_my_followed
|
|
|
|
},
|
|
|
|
}
|