From 6b53746c5f80317e850e2c90b32f8282ce4476c6 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Wed, 2 Nov 2022 16:44:00 +0300 Subject: [PATCH] fixed-api --- .../query/{my-chats.ts => im-chats.ts} | 1 - src/graphql/query/im-load-messages.ts | 13 +++++ src/graphql/types.gen.ts | 49 ++++++++++++------- src/pages/inbox.astro | 2 +- src/utils/apiClient.ts | 17 ++++++- 5 files changed, 59 insertions(+), 23 deletions(-) rename src/graphql/query/{my-chats.ts => im-chats.ts} (95%) create mode 100644 src/graphql/query/im-load-messages.ts diff --git a/src/graphql/query/my-chats.ts b/src/graphql/query/im-chats.ts similarity index 95% rename from src/graphql/query/my-chats.ts rename to src/graphql/query/im-chats.ts index 043eab0b..993da387 100644 --- a/src/graphql/query/my-chats.ts +++ b/src/graphql/query/im-chats.ts @@ -4,7 +4,6 @@ export default gql` query GetChatsQuery { myChats { messages { - chatId id author body diff --git a/src/graphql/query/im-load-messages.ts b/src/graphql/query/im-load-messages.ts new file mode 100644 index 00000000..0e2be8f7 --- /dev/null +++ b/src/graphql/query/im-load-messages.ts @@ -0,0 +1,13 @@ +import { gql } from '@urql/core' + +export default gql` + query LoadMessagesQuery($chatId: String!, $offset: Int, $amount: Int) { + loadChat(chatId: $chatId, offset: $offset, amount: $amount) { + id + title + desc + slug + amount + } + } +` diff --git a/src/graphql/types.gen.ts b/src/graphql/types.gen.ts index a8191d36..7f93f85e 100644 --- a/src/graphql/types.gen.ts +++ b/src/graphql/types.gen.ts @@ -31,17 +31,23 @@ export type Author = { } export type Chat = { - createdAt: Scalars['DateTime'] + createdAt: Scalars['Int'] createdBy: User description?: Maybe id: Scalars['Int'] messages: Array> title?: Maybe unread?: Maybe - updatedAt: Scalars['DateTime'] + updatedAt: Scalars['Int'] users: Array> } +export type ChatInput = { + description?: InputMaybe + id: Scalars['String'] + title?: InputMaybe +} + export type ChatMember = { invitedAt?: Maybe invitedBy?: Maybe @@ -99,12 +105,11 @@ export enum FollowingEntity { export type Message = { author: Scalars['String'] body: Scalars['String'] - chatId: Scalars['Int'] - createdAt: Scalars['DateTime'] + chatId: Scalars['String'] + createdAt: Scalars['Int'] id: Scalars['Int'] replyTo?: Maybe - updatedAt: Scalars['DateTime'] - visibleForUsers?: Maybe>> + updatedAt?: Maybe } export enum MessageStatus { @@ -115,7 +120,7 @@ export enum MessageStatus { export type Mutation = { confirmEmail: AuthResult - createChat: Chat + createChat: Result createCollection: Result createCommunity: Result createMessage: Result @@ -128,6 +133,7 @@ export type Mutation = { deleteReaction: Result deleteShout: Result destroyTopic: Result + enterChat: Result follow: Result incrementView: Result inviteAuthor: Result @@ -139,6 +145,7 @@ export type Mutation = { removeAuthor: Result sendLink: Result unfollow: Result + updateChat: Result updateCollection: Result updateCommunity: Result updateMessage: Result @@ -153,7 +160,8 @@ export type MutationConfirmEmailArgs = { } export type MutationCreateChatArgs = { - description?: InputMaybe + members: Array> + title?: InputMaybe } export type MutationCreateCollectionArgs = { @@ -207,6 +215,10 @@ export type MutationDestroyTopicArgs = { slug: Scalars['String'] } +export type MutationEnterChatArgs = { + chatId: Scalars['String'] +} + export type MutationFollowArgs = { slug: Scalars['String'] what: FollowingEntity @@ -257,6 +269,10 @@ export type MutationUnfollowArgs = { what: FollowingEntity } +export type MutationUpdateChatArgs = { + chat: ChatInput +} + export type MutationUpdateCollectionArgs = { collection: CollectionInput } @@ -313,7 +329,6 @@ export type ProfileInput = { export type Query = { authorsAll: Array> collectionsAll: Array> - enterChat: Chat getCollabs: Array> getCommunities: Array> getCommunity: Community @@ -356,10 +371,6 @@ export type Query = { userReactedShouts: Array> } -export type QueryEnterChatArgs = { - chatId: Scalars['String'] -} - export type QueryGetCommunityArgs = { slug?: InputMaybe } @@ -385,9 +396,9 @@ export type QueryIsEmailUsedArgs = { } export type QueryLoadChatArgs = { + amount?: InputMaybe chatId: Scalars['String'] - page: Scalars['Int'] - size: Scalars['Int'] + offset?: InputMaybe } export type QueryMarkdownBodyArgs = { @@ -657,7 +668,7 @@ export type ShoutInput = { topic_slugs?: InputMaybe>> versionOf?: InputMaybe visibleForRoles?: InputMaybe>> - visibleForUsers?: InputMaybe>> + visibleForUsers?: InputMaybe>> } export type Stat = { @@ -669,15 +680,15 @@ export type Stat = { } export type Subscription = { - chatUpdated: Result + newMessage: Message onlineUpdated: Array reactionUpdated: ReactionUpdating shoutUpdated: Shout userUpdated: User } -export type SubscriptionChatUpdatedArgs = { - chatId: Scalars['String'] +export type SubscriptionNewMessageArgs = { + chats?: InputMaybe> } export type SubscriptionReactionUpdatedArgs = { diff --git a/src/pages/inbox.astro b/src/pages/inbox.astro index 8e7abf67..74db702d 100644 --- a/src/pages/inbox.astro +++ b/src/pages/inbox.astro @@ -4,7 +4,7 @@ import { Root } from '../components/Root' import { apiClient } from '../utils/apiClient' import { initRouter } from '../stores/router' -const chatrooms = await apiClient.getInboxes() +const chatrooms = await apiClient.getChats() const { pathname, search } = Astro.url initRouter(pathname, search) diff --git a/src/utils/apiClient.ts b/src/utils/apiClient.ts index 670d7b8d..9a52d77f 100644 --- a/src/utils/apiClient.ts +++ b/src/utils/apiClient.ts @@ -27,7 +27,8 @@ import reactionDestroy from '../graphql/mutation/reaction-destroy' import reactionUpdate from '../graphql/mutation/reaction-update' import authorsBySlugs from '../graphql/query/authors-by-slugs' import incrementView from '../graphql/mutation/increment-view' -import myChats from '../graphql/query/my-chats' +import myChats from '../graphql/query/im-chats' +import loadChat from '../graphql/query/im-load-messages' const FEED_SIZE = 50 @@ -325,8 +326,20 @@ export const apiClient = { incrementView: async ({ articleSlug }) => { await privateGraphQLClient.mutation(incrementView, { shout: articleSlug }) }, - getInboxes: async (payload = {}) => { + getChats: async (payload = {}) => { const resp = await privateGraphQLClient.query(myChats, payload).toPromise() return resp.data.myChats + }, + getChatMessages: async ({ + chatId, + offset = 0, + amount = 50 + }: { + chatId: string + offset?: number + amount?: number + }) => { + const resp = await privateGraphQLClient.query(loadChat, { chatId, offset, amount }).toPromise() + return resp.data.loadChat } }