Refactor GraphQL client usage in services to improve consistency

- Replaced direct calls to `getClientWithToken` with a new method `getGraphQLClient` across multiple services, ensuring a unified approach to obtaining the GraphQL client.
- Updated the `BaseService` to manage the GraphQL client instance, enhancing performance by reusing the client when available.
This commit is contained in:
vchikalkin 2025-09-30 19:02:11 +03:00
parent 1c669f04dd
commit 8cb283d4ba
6 changed files with 42 additions and 36 deletions

View File

@ -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<NormalizedCacheObject> | 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;
}
}

View File

@ -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<typeof GQL.GetCustomerDocument>) {
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<typeof GQL.GetCustomersDocument>) {
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<typeof GQL.GetInvitedDocument>) {
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<typeof GQL.GetInvitedByDocument>) {
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,

View File

@ -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<typeof GQL.GetOrderDocument>) {
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<typeof GQL.GetOrdersDocument>) {
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);

View File

@ -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<typeof GQL.GetServiceDocument>) {
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<typeof GQL.GetServicesDocument>) {
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,

View File

@ -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<typeof GQL.GetSlotDocument>) {
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<typeof GQL.GetSlotsDocument>) {
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,

View File

@ -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<typeof GQL.CreateSubscriptionDocument>) {
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<typeof GQL.GetSubscriptionHistoryDocument>) {
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<typeof GQL.GetSubscriptionPricesDocument>) {
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<typeof GQL.GetSubscriptionsDocument>) {
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<typeof GQL.UpdateSubscriptionDocument>) {
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,