diff --git a/packages/graphql/api/base.ts b/packages/graphql/api/base.ts index c35cb83..5ce1f14 100644 --- a/packages/graphql/api/base.ts +++ b/packages/graphql/api/base.ts @@ -2,6 +2,7 @@ import { getClientWithToken } from '../apollo/client'; import { ERRORS as SHARED_ERRORS } from '../constants/errors'; import * as GQL from '../types'; +import { type ApolloClient, type NormalizedCacheObject } from '@apollo/client'; import { isCustomerBanned } from '@repo/utils/customer'; export const ERRORS = { @@ -16,16 +17,20 @@ type UserProfile = { export class BaseService { protected _user: UserProfile; + protected graphQL: ApolloClient | null; + constructor(user: UserProfile) { if (!user?.telegramId) { throw new Error(ERRORS.MISSING_TELEGRAM_ID); } this._user = user; + + this.graphQL = null; } protected async _getUser() { - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetCustomerDocument, @@ -44,7 +49,7 @@ export class BaseService { } protected async checkIsBanned() { - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetCustomerDocument, @@ -61,4 +66,10 @@ export class BaseService { return { customer }; } + + protected async getGraphQLClient() { + if (!this.graphQL) this.graphQL = await getClientWithToken(); + + return this.graphQL; + } } diff --git a/packages/graphql/api/customers.ts b/packages/graphql/api/customers.ts index 6ad7f77..31c56a0 100644 --- a/packages/graphql/api/customers.ts +++ b/packages/graphql/api/customers.ts @@ -1,4 +1,3 @@ -import { getClientWithToken } from '../apollo/client'; import { ERRORS } from '../constants/errors'; import * as GQL from '../types'; import { BaseService } from './base'; @@ -17,7 +16,7 @@ export class CustomersService extends BaseService { throw new Error(ERRORS.NO_PERMISSION); } - const { mutate, query } = await getClientWithToken(); + const { mutate, query } = await this.getGraphQLClient(); const getInvitedByResult = await query({ query: GQL.GetInvitedByDocument, variables, @@ -46,7 +45,7 @@ export class CustomersService extends BaseService { } async getCustomer(variables: VariablesOf) { - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetCustomerDocument, @@ -61,7 +60,7 @@ export class CustomersService extends BaseService { async getCustomers(variables: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetCustomersDocument, @@ -77,7 +76,7 @@ export class CustomersService extends BaseService { async getInvited(variables?: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetInvitedDocument, @@ -92,7 +91,7 @@ export class CustomersService extends BaseService { async getInvitedBy(variables?: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetInvitedByDocument, @@ -116,7 +115,7 @@ export class CustomersService extends BaseService { throw new Error(ERRORS.NO_PERMISSION); } - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.UpdateCustomerDocument, diff --git a/packages/graphql/api/orders.ts b/packages/graphql/api/orders.ts index e062487..4278f36 100644 --- a/packages/graphql/api/orders.ts +++ b/packages/graphql/api/orders.ts @@ -1,6 +1,5 @@ /* eslint-disable sonarjs/cognitive-complexity */ /* eslint-disable @typescript-eslint/naming-convention */ -import { getClientWithToken } from '../apollo/client'; import { ERRORS as SHARED_ERRORS } from '../constants/errors'; import * as GQL from '../types'; import { BaseService } from './base'; @@ -123,7 +122,7 @@ export class OrdersService extends BaseService { const isSlotMaster = slot.master.documentId === customer.documentId; - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.CreateOrderDocument, @@ -145,7 +144,7 @@ export class OrdersService extends BaseService { async getOrder(variables: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetOrderDocument, @@ -157,7 +156,7 @@ export class OrdersService extends BaseService { async getOrders(variables: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetOrdersDocument, @@ -177,7 +176,7 @@ export class OrdersService extends BaseService { const { customer } = await this._getUser(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const { data: { order }, @@ -204,7 +203,7 @@ export class OrdersService extends BaseService { throw new Error(SHARED_ERRORS.NO_PERMISSION); } - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const lastOrderNumber = await this.getLastOrderNumber(variables); diff --git a/packages/graphql/api/services.ts b/packages/graphql/api/services.ts index 24b7a3d..454e07f 100644 --- a/packages/graphql/api/services.ts +++ b/packages/graphql/api/services.ts @@ -1,4 +1,3 @@ -import { getClientWithToken } from '../apollo/client'; import { ERRORS } from '../constants/errors'; import * as GQL from '../types'; import { BaseService } from './base'; @@ -10,7 +9,7 @@ export class ServicesService extends BaseService { const { customer } = await this._getUser(); - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.CreateServiceDocument, @@ -32,7 +31,7 @@ export class ServicesService extends BaseService { async getService(variables: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetServiceDocument, @@ -45,7 +44,7 @@ export class ServicesService extends BaseService { async getServices(variables: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetServicesDocument, @@ -60,7 +59,7 @@ export class ServicesService extends BaseService { await this.checkPermission(variables); - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.UpdateServiceDocument, diff --git a/packages/graphql/api/slots.ts b/packages/graphql/api/slots.ts index 506d1f8..3892638 100644 --- a/packages/graphql/api/slots.ts +++ b/packages/graphql/api/slots.ts @@ -1,5 +1,4 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { getClientWithToken } from '../apollo/client'; import { ERRORS as SHARED_ERRORS } from '../constants/errors'; import * as GQL from '../types'; import { BaseService } from './base'; @@ -30,7 +29,7 @@ export class SlotsService extends BaseService { const { customer } = await this._getUser(); - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.CreateSlotDocument, @@ -60,7 +59,7 @@ export class SlotsService extends BaseService { throw new Error(ERRORS.SLOT_HAS_ORDERS); } - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.DeleteSlotDocument, @@ -102,7 +101,7 @@ export class SlotsService extends BaseService { 0, ); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const getSlotsResult = await query({ query: GQL.GetSlotsOrdersDocument, @@ -154,7 +153,7 @@ export class SlotsService extends BaseService { async getSlot(variables: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetSlotDocument, @@ -167,7 +166,7 @@ export class SlotsService extends BaseService { async getSlots(variables: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetSlotsDocument, @@ -183,7 +182,7 @@ export class SlotsService extends BaseService { await this.checkPermission(variables); await this.checkBeforeUpdateDatetime(variables); - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.UpdateSlotDocument, diff --git a/packages/graphql/api/subscriptions.ts b/packages/graphql/api/subscriptions.ts index 8d8a7f3..ecec280 100644 --- a/packages/graphql/api/subscriptions.ts +++ b/packages/graphql/api/subscriptions.ts @@ -1,4 +1,3 @@ -import { getClientWithToken } from '../apollo/client'; import * as GQL from '../types'; import { BaseService } from './base'; import { OrdersService } from './orders'; @@ -86,7 +85,7 @@ export class SubscriptionsService extends BaseService { async createSubscription(variables: VariablesOf) { await this.checkIsBanned(); - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.CreateSubscriptionDocument, @@ -104,7 +103,7 @@ export class SubscriptionsService extends BaseService { ) { await this.checkIsBanned(); - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.CreateSubscriptionHistoryDocument, @@ -209,7 +208,7 @@ export class SubscriptionsService extends BaseService { } async getSubscriptionHistory(variables: VariablesOf) { - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetSubscriptionHistoryDocument, @@ -224,7 +223,7 @@ export class SubscriptionsService extends BaseService { async getSubscriptionPrices(variables?: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetSubscriptionPricesDocument, @@ -237,7 +236,7 @@ export class SubscriptionsService extends BaseService { async getSubscriptions(variables?: VariablesOf) { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetSubscriptionsDocument, @@ -250,7 +249,7 @@ export class SubscriptionsService extends BaseService { async getSubscriptionSettings() { await this.checkIsBanned(); - const { query } = await getClientWithToken(); + const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetSubscriptionSettingsDocument, @@ -262,7 +261,7 @@ export class SubscriptionsService extends BaseService { async updateSubscription(variables: VariablesOf) { await this.checkIsBanned(); - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.UpdateSubscriptionDocument, @@ -280,7 +279,7 @@ export class SubscriptionsService extends BaseService { ) { await this.checkIsBanned(); - const { mutate } = await getClientWithToken(); + const { mutate } = await this.getGraphQLClient(); const mutationResult = await mutate({ mutation: GQL.UpdateSubscriptionHistoryDocument,