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-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'
|
|
|
|
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-11-29 12:37:27 +00:00
|
|
|
import shoutsLoadSearch from '../query/core/articles-load-search'
|
2023-11-28 13:18:25 +00:00
|
|
|
import authorBy from '../query/core/author-by'
|
2023-12-13 23:56:44 +00:00
|
|
|
import authorId from '../query/core/author-id'
|
2023-11-28 13:18:25 +00:00
|
|
|
import authorFollowers from '../query/core/author-followers'
|
|
|
|
import authorsAll from '../query/core/authors-all'
|
2023-11-28 15:36:00 +00:00
|
|
|
import authorFollowed from '../query/core/authors-followed-by'
|
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'
|
2023-11-28 15:36:00 +00:00
|
|
|
import userFollowedTopics from '../query/core/topics-by-author'
|
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,
|
|
|
|
connect: () => (apiClient.private = createGraphQLClient('core')), // NOTE: use it after token appears
|
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
getRandomTopics: async ({ amount }: { amount: number }) => {
|
|
|
|
const response = await publicGraphQLClient.query(topicsRandomQuery, { amount }).toPromise()
|
|
|
|
|
|
|
|
if (!response.data) {
|
|
|
|
console.error('[graphql.client.core] getRandomTopics', response.error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.data.get_topics_random
|
|
|
|
},
|
|
|
|
|
|
|
|
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()
|
|
|
|
if (response.error) {
|
|
|
|
console.debug('[graphql.client.core] get_topicss_all', response.error)
|
|
|
|
}
|
|
|
|
return response.data.get_topics_all
|
|
|
|
},
|
2023-12-09 22:15:02 +00:00
|
|
|
getAllAuthors: async (limit: number = 50, offset: number = 0) => {
|
|
|
|
const response = await publicGraphQLClient.query(authorsAll, { limit, offset }).toPromise()
|
2023-11-28 13:18:25 +00:00
|
|
|
if (response.error) {
|
2023-12-09 22:15:02 +00:00
|
|
|
console.debug('[graphql.client.core] load_authors_all', response.error)
|
2023-11-28 13:18:25 +00:00
|
|
|
}
|
2023-12-09 22:15:02 +00:00
|
|
|
return response.data.load_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
|
|
|
|
},
|
|
|
|
getAuthorFollowingUsers: async ({ slug }: { slug: string }): Promise<Author[]> => {
|
|
|
|
const response = await publicGraphQLClient.query(authorFollowed, { slug }).toPromise()
|
|
|
|
return response.data.get_author_followed
|
|
|
|
},
|
|
|
|
getAuthorFollowingTopics: async ({ slug }: { slug: string }): Promise<Topic[]> => {
|
|
|
|
const response = await publicGraphQLClient.query(userFollowedTopics, { slug }).toPromise()
|
|
|
|
return response.data.userFollowedTopics
|
|
|
|
},
|
|
|
|
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
|
|
|
|
},
|
|
|
|
deleteShout: async ({ shoutId }: { shoutId: number }): Promise<void> => {
|
2023-12-03 10:22:42 +00:00
|
|
|
const response = await apiClient.private.mutation(deleteShout, { shout_id: 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-11-29 12:37:27 +00:00
|
|
|
getAuthorsBy: async (args: QueryLoad_Authors_ByArgs) => {
|
|
|
|
const resp = await publicGraphQLClient.query(authorsLoadBy, args).toPromise()
|
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
|
|
|
|
|
|
|
return resp.data.load_shouts_by
|
|
|
|
},
|
|
|
|
|
2023-11-29 08:23:08 +00:00
|
|
|
getShoutsSearch: async (args: QueryLoad_Shouts_SearchArgs) => {
|
|
|
|
const resp = await publicGraphQLClient.query(shoutsLoadSearch, args).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
|
|
|
|
},
|
|
|
|
}
|