From 4044e057a652de749838b5eba8e66139689d834e Mon Sep 17 00:00:00 2001 From: Wroud Date: Fri, 11 Sep 2020 10:45:40 +0300 Subject: [PATCH] refactor(core-sdk): GraphQLService --- .../ServerConfigurationService.ts | 4 +- .../shared/NodesManager/DBObjectService.ts | 4 +- .../NodesManager/NavNodeInfoResource.ts | 2 +- .../NodesManager/NavNodeManagerService.ts | 2 +- .../shared/NodesManager/NavTreeResource.ts | 2 +- .../ToolsPanel/LogViewTab/LogViewerService.ts | 2 +- .../src/AuthInfoService.ts | 12 ++--- .../src/AuthProvidersResource.ts | 2 +- .../core-authentication/src/RolesResource.ts | 2 +- .../core-authentication/src/UsersResource.ts | 18 +++---- .../src/Administration/ConnectionsResource.ts | 22 ++++---- .../src/ConnectionInfoResource.ts | 12 ++--- .../core-connections/src/ContainerResource.ts | 2 +- .../core-connections/src/DBDriverResource.ts | 2 +- .../src/DatabaseAuthModelsResource.ts | 2 +- .../src/DriverPropertiesService.ts | 2 +- .../src/LocalizationService.ts | 2 +- .../core-root/src/PermissionsResource.ts | 2 +- .../core-root/src/ServerConfigResource.ts | 2 +- .../packages/core-root/src/SessionResource.ts | 2 +- .../core-sdk/src/CustomGraphQLClient.ts | 51 +++++++++++++++++++ .../packages/core-sdk/src/GraphQLService.ts | 41 +++------------ webapp/packages/core-sdk/src/index.ts | 13 ++--- .../src/CustomConnectionService.ts | 4 +- .../src/TemplateConnectionsResource.ts | 2 +- .../src/DataExportProcessService.ts | 2 +- .../src/DataTransferProcessorsResource.ts | 2 +- .../src/ExportFromContainerProcess.ts | 8 +-- .../src/ExportFromResultsProcess.ts | 8 +-- .../src/DataViewerTableService.ts | 6 +-- .../plugin-ddl-viewer/src/DdlViewerService.ts | 2 +- .../src/SqlDialectInfoService.ts | 2 +- .../src/SqlEditor/SqlEditorService.ts | 2 +- .../src/SqlEditorTabService.ts | 6 +-- .../SqlResultTabs/SQLQueryExecutionProcess.ts | 10 ++-- .../src/SqlResultTabs/SqlResultService.ts | 15 ++---- 36 files changed, 144 insertions(+), 130 deletions(-) create mode 100644 webapp/packages/core-sdk/src/CustomGraphQLClient.ts diff --git a/webapp/packages/core-administration/src/AdministrationScreen/ConfigurationWizard/Pages/ServerConfiguration/ServerConfigurationService.ts b/webapp/packages/core-administration/src/AdministrationScreen/ConfigurationWizard/Pages/ServerConfiguration/ServerConfigurationService.ts index fdcb554bae..4359be4db8 100644 --- a/webapp/packages/core-administration/src/AdministrationScreen/ConfigurationWizard/Pages/ServerConfiguration/ServerConfigurationService.ts +++ b/webapp/packages/core-administration/src/AdministrationScreen/ConfigurationWizard/Pages/ServerConfiguration/ServerConfigurationService.ts @@ -89,8 +89,8 @@ export class ServerConfigurationService { } try { - await this.graphQLService.gql.setDefaultNavigatorSettings({ settings: this.state.navigatorConfig }); - await this.graphQLService.gql.configureServer({ configuration: this.state.serverConfig }); + await this.graphQLService.sdk.setDefaultNavigatorSettings({ settings: this.state.navigatorConfig }); + await this.graphQLService.sdk.configureServer({ configuration: this.state.serverConfig }); this.usersResource.refreshAllLazy(); } catch (exception) { this.notificationService.logException(exception, 'Can\'t save server configuration'); diff --git a/webapp/packages/core-app/src/shared/NodesManager/DBObjectService.ts b/webapp/packages/core-app/src/shared/NodesManager/DBObjectService.ts index 80aca26b6a..977fdff821 100644 --- a/webapp/packages/core-app/src/shared/NodesManager/DBObjectService.ts +++ b/webapp/packages/core-app/src/shared/NodesManager/DBObjectService.ts @@ -48,7 +48,7 @@ export class DBObjectService extends CachedMapResource { } private async loadFromChildren(parentId: string) { - const { dbObjects } = await this.graphQLService.gql.getChildrenDBObjectInfo({ + const { dbObjects } = await this.graphQLService.sdk.getChildrenDBObjectInfo({ navNodeId: parentId, }); @@ -64,7 +64,7 @@ export class DBObjectService extends CachedMapResource { } private async loadDBObjectInfo(navNodeId: string): Promise { - const { objectInfo: { object } } = await this.graphQLService.gql.getDBObjectInfo({ + const { objectInfo: { object } } = await this.graphQLService.sdk.getDBObjectInfo({ navNodeId, }); diff --git a/webapp/packages/core-app/src/shared/NodesManager/NavNodeInfoResource.ts b/webapp/packages/core-app/src/shared/NodesManager/NavNodeInfoResource.ts index 797e57800f..62977bf0f2 100644 --- a/webapp/packages/core-app/src/shared/NodesManager/NavNodeInfoResource.ts +++ b/webapp/packages/core-app/src/shared/NodesManager/NavNodeInfoResource.ts @@ -47,7 +47,7 @@ export class NavNodeInfoResource extends CachedMapResource { } private async loadNodeInfo(nodePath: string): Promise { - const { navNodeInfo } = await this.graphQLService.gql.navNodeInfo({ + const { navNodeInfo } = await this.graphQLService.sdk.navNodeInfo({ nodePath, }); diff --git a/webapp/packages/core-app/src/shared/NodesManager/NavNodeManagerService.ts b/webapp/packages/core-app/src/shared/NodesManager/NavNodeManagerService.ts index 9edb723678..e649adc58e 100644 --- a/webapp/packages/core-app/src/shared/NodesManager/NavNodeManagerService.ts +++ b/webapp/packages/core-app/src/shared/NodesManager/NavNodeManagerService.ts @@ -134,7 +134,7 @@ export class NavNodeManagerService extends Bootstrap { } async refreshTree(navNodeId: string) { - await this.graphQLService.gql.navRefreshNode({ + await this.graphQLService.sdk.navRefreshNode({ nodePath: navNodeId, }); this.markTreeOutdated(navNodeId); diff --git a/webapp/packages/core-app/src/shared/NodesManager/NavTreeResource.ts b/webapp/packages/core-app/src/shared/NodesManager/NavTreeResource.ts index 3b2ca0952d..22d4be12f2 100644 --- a/webapp/packages/core-app/src/shared/NodesManager/NavTreeResource.ts +++ b/webapp/packages/core-app/src/shared/NodesManager/NavTreeResource.ts @@ -212,7 +212,7 @@ export class NavTreeResource extends CachedMapResource { } private async loadNodeChildren(parentPath: string) { - const { navNodeChildren, navNodeInfo } = await this.graphQLService.gql.navNodeChildren({ + const { navNodeChildren, navNodeInfo } = await this.graphQLService.sdk.navNodeChildren({ parentPath, }); diff --git a/webapp/packages/core-app/src/shared/ToolsPanel/LogViewTab/LogViewerService.ts b/webapp/packages/core-app/src/shared/ToolsPanel/LogViewTab/LogViewerService.ts index 9961a65c4f..26b7bf091d 100644 --- a/webapp/packages/core-app/src/shared/ToolsPanel/LogViewTab/LogViewerService.ts +++ b/webapp/packages/core-app/src/shared/ToolsPanel/LogViewTab/LogViewerService.ts @@ -83,7 +83,7 @@ export class LogViewerService { async loadLog(): Promise { const maxLogEntries = this.coreSettingsService.settings.getValue('app.logViewer.logBatchSize'); - const { log } = await this.graphQLService.gql.readSessionLog({ + const { log } = await this.graphQLService.sdk.readSessionLog({ maxEntries: maxLogEntries, clearEntries: true, }); diff --git a/webapp/packages/core-authentication/src/AuthInfoService.ts b/webapp/packages/core-authentication/src/AuthInfoService.ts index 8ec763e6ac..1c2feb6baa 100644 --- a/webapp/packages/core-authentication/src/AuthInfoService.ts +++ b/webapp/packages/core-authentication/src/AuthInfoService.ts @@ -26,18 +26,18 @@ export class AuthInfoService { private sessionService: SessionService, ) { } - get userInfo() { + get userInfo(): UserAuthInfo | null { return this.user; } - async login(provider: string, credentials: object): Promise { + async login(provider: string, credentials: Record): Promise { if (this.user) { throw new Error('User already logged in'); } const processedCredentials = await this.authProviderService.processCredentials(provider, credentials); - const { user } = await this.graphQLService.gql.authLogin({ + const { user } = await this.graphQLService.sdk.authLogin({ provider, credentials: processedCredentials, }); @@ -47,9 +47,9 @@ export class AuthInfoService { return this.user; } - async logout() { + async logout(): Promise { if (this.user) { - await this.graphQLService.gql.authLogout(); + await this.graphQLService.sdk.authLogout(); this.user = null; await this.updateSession(); } @@ -57,7 +57,7 @@ export class AuthInfoService { async updateAuthInfo(): Promise { try { - const { user } = await this.graphQLService.gql.getSessionUser(); + const { user } = await this.graphQLService.sdk.getSessionUser(); this.user = user || null; } catch (exception) { this.notificationService.logException(exception, 'Can\'t load session user'); diff --git a/webapp/packages/core-authentication/src/AuthProvidersResource.ts b/webapp/packages/core-authentication/src/AuthProvidersResource.ts index 819a3ec21f..3d9b2a8dbc 100644 --- a/webapp/packages/core-authentication/src/AuthProvidersResource.ts +++ b/webapp/packages/core-authentication/src/AuthProvidersResource.ts @@ -28,7 +28,7 @@ export class AuthProvidersResource extends CachedDataResource { - const { providers } = await this.graphQLService.gql.getAuthProviders(); + const { providers } = await this.graphQLService.sdk.getAuthProviders(); this.markUpdated(key); return providers; diff --git a/webapp/packages/core-authentication/src/RolesResource.ts b/webapp/packages/core-authentication/src/RolesResource.ts index e316d31f24..28404754e8 100644 --- a/webapp/packages/core-authentication/src/RolesResource.ts +++ b/webapp/packages/core-authentication/src/RolesResource.ts @@ -44,7 +44,7 @@ export class RolesResource extends CachedMapResource { protected async loader(key: ResourceKey): Promise> { const roleId = key === 'all' ? undefined : key as string; - const { roles } = await this.graphQLService.gql.getRolesList({ roleId }); + const { roles } = await this.graphQLService.sdk.getRolesList({ roleId }); if (key === 'all') { this.data.clear(); diff --git a/webapp/packages/core-authentication/src/UsersResource.ts b/webapp/packages/core-authentication/src/UsersResource.ts index 5024886fed..07effeb2ab 100644 --- a/webapp/packages/core-authentication/src/UsersResource.ts +++ b/webapp/packages/core-authentication/src/UsersResource.ts @@ -63,19 +63,19 @@ export class UsersResource extends CachedMapResource { return []; } - const { grantedConnections } = await this.graphQLService.gql.getUserGrantedConnections({ userId }); + const { grantedConnections } = await this.graphQLService.sdk.getUserGrantedConnections({ userId }); return grantedConnections; } async setConnections(userId: string, connections: string[]) { - await this.graphQLService.gql.setConnections({ userId, connections }); + await this.graphQLService.sdk.setConnections({ userId, connections }); } async create({ userId, roles, credentials, grantedConnections, }: UserCreateOptions): Promise { - const { user } = await this.graphQLService.gql.createUser({ userId }); + const { user } = await this.graphQLService.sdk.createUser({ userId }); try { await this.updateCredentials(userId, credentials); @@ -95,7 +95,7 @@ export class UsersResource extends CachedMapResource { } async grantRole(userId: string, roleId: string, skipUpdate?: boolean) { - await this.graphQLService.gql.grantUserRole({ userId, roleId }); + await this.graphQLService.sdk.grantUserRole({ userId, roleId }); if (!skipUpdate) { await this.refresh(userId); @@ -103,7 +103,7 @@ export class UsersResource extends CachedMapResource { } async revokeRole(userId: string, roleId: string, skipUpdate?: boolean) { - await this.graphQLService.gql.revokeUserRole({ userId, roleId }); + await this.graphQLService.sdk.revokeUserRole({ userId, roleId }); if (!skipUpdate) { await this.refresh(userId); @@ -114,7 +114,7 @@ export class UsersResource extends CachedMapResource { const provider = 'local'; const processedCredentials = await this.authProviderService.processCredentials(provider, credentials); - await this.graphQLService.gql.setUserCredentials({ + await this.graphQLService.sdk.setUserCredentials({ providerId: provider, userId, credentials: processedCredentials, @@ -128,7 +128,7 @@ export class UsersResource extends CachedMapResource { throw new Error('You can\'t delete current logged user'); } if (!this.isNew(key.list[i])) { - await this.graphQLService.gql.deleteUser({ userId: key.list[i] }); + await this.graphQLService.sdk.deleteUser({ userId: key.list[i] }); } this.data.delete(key.list[i]); } @@ -137,7 +137,7 @@ export class UsersResource extends CachedMapResource { throw new Error('You can\'t delete current logged user'); } if (!this.isNew(key)) { - await this.graphQLService.gql.deleteUser({ userId: key }); + await this.graphQLService.sdk.deleteUser({ userId: key }); } this.data.delete(key); } @@ -163,7 +163,7 @@ export class UsersResource extends CachedMapResource { protected async loader(key: ResourceKey): Promise> { const userId = key === 'all' ? undefined : key as string; - const { users } = await this.graphQLService.gql.getUsersList({ userId }); + const { users } = await this.graphQLService.sdk.getUsersList({ userId }); if (key === 'all') { this.data.clear(); diff --git a/webapp/packages/core-connections/src/Administration/ConnectionsResource.ts b/webapp/packages/core-connections/src/Administration/ConnectionsResource.ts index 44b9e16631..08d2eb63d1 100644 --- a/webapp/packages/core-connections/src/Administration/ConnectionsResource.ts +++ b/webapp/packages/core-connections/src/Administration/ConnectionsResource.ts @@ -80,14 +80,14 @@ export class ConnectionsResource extends CachedMapResource { - await this.graphQLService.gql.testConnection({ + await this.graphQLService.sdk.testConnection({ config, }); } async update(id: string, config: ConnectionConfig) { await this.performUpdate(id, () => this.updateConnection(id, config)); - await this.graphQLService.gql.refreshSessionConnections(); + await this.graphQLService.sdk.refreshSessionConnections(); return this.get(id)!; } async delete(key: ResourceKey) { await this.performUpdate(key, () => this.deleteConnectionTask(key)); - await this.graphQLService.gql.refreshSessionConnections(); + await this.graphQLService.sdk.refreshSessionConnections(); } async loadAccessSubjects(connectionId: string): Promise { @@ -120,17 +120,17 @@ export class ConnectionsResource extends CachedMapResource): Promise> { - const { connections } = await this.graphQLService.gql.getConnections(); + const { connections } = await this.graphQLService.sdk.getConnections(); this.data.clear(); for (const connection of connections) { @@ -171,12 +171,12 @@ export class ConnectionsResource extends CachedMapResource { - const { connection } = await this.graphQLService.gql.createConnectionFromTemplate({ templateId }); + const { connection } = await this.graphQLService.sdk.createConnectionFromTemplate({ templateId }); this.set(connection.id, connection); return this.get(connection.id)!; @@ -61,7 +61,7 @@ export class ConnectionInfoResource extends CachedMapResource { - await this.graphQLService.gql.deleteConnection({ id: connectionId }); + await this.graphQLService.sdk.deleteConnection({ id: connectionId }); }); this.delete(connectionId); } @@ -82,7 +82,7 @@ export class ConnectionInfoResource extends CachedMapResource> { - const { connection } = await this.graphQLService.gql.connectionInfo({ id: connectionId }); + const { connection } = await this.graphQLService.sdk.connectionInfo({ id: connectionId }); const oldConnection = this.get(connectionId) || {}; this.set(connectionId, { ...oldConnection, ...connection }); @@ -104,19 +104,19 @@ export class ConnectionInfoResource extends CachedMapResource { - const { connection: { authProperties } } = await this.graphQLService.gql.connectionAuthProperties({ id }); + const { connection: { authProperties } } = await this.graphQLService.sdk.connectionAuthProperties({ id }); return authProperties; } private async initConnection(id: string, credentials?: any): Promise { - const { connection } = await this.graphQLService.gql.initConnection({ id, credentials }); + const { connection } = await this.graphQLService.sdk.initConnection({ id, credentials }); return connection; } private async closeConnection(id: string): Promise { - const { connection } = await this.graphQLService.gql.closeConnection({ id }); + const { connection } = await this.graphQLService.sdk.closeConnection({ id }); return connection; } diff --git a/webapp/packages/core-connections/src/ContainerResource.ts b/webapp/packages/core-connections/src/ContainerResource.ts index 897e7da4fb..18938246b7 100644 --- a/webapp/packages/core-connections/src/ContainerResource.ts +++ b/webapp/packages/core-connections/src/ContainerResource.ts @@ -31,7 +31,7 @@ export class ContainerResource extends CachedDataResource> { - const { navGetStructContainers } = await this.graphQLService.gql.navGetStructContainers({ + const { navGetStructContainers } = await this.graphQLService.sdk.navGetStructContainers({ connectionId, catalogId, }); diff --git a/webapp/packages/core-connections/src/DBDriverResource.ts b/webapp/packages/core-connections/src/DBDriverResource.ts index 97ead65e64..7c8ced449f 100644 --- a/webapp/packages/core-connections/src/DBDriverResource.ts +++ b/webapp/packages/core-connections/src/DBDriverResource.ts @@ -64,7 +64,7 @@ export class DBDriverResource extends CachedMapResource { } protected async loader(key: string): Promise> { - const { driverList } = await this.graphQLService.gql.driverList(); + const { driverList } = await this.graphQLService.sdk.driverList(); this.data.clear(); diff --git a/webapp/packages/core-connections/src/DatabaseAuthModelsResource.ts b/webapp/packages/core-connections/src/DatabaseAuthModelsResource.ts index a808b39ee7..9f90d87af8 100644 --- a/webapp/packages/core-connections/src/DatabaseAuthModelsResource.ts +++ b/webapp/packages/core-connections/src/DatabaseAuthModelsResource.ts @@ -21,7 +21,7 @@ export class DatabaseAuthModelsResource extends CachedMapResource> { - const { models } = await this.graphQLService.gql.getAuthModels(); + const { models } = await this.graphQLService.sdk.getAuthModels(); this.set(resourceKeyList(models.map(model => model.id)), models); this.markUpdated(key); diff --git a/webapp/packages/core-connections/src/DriverPropertiesService.ts b/webapp/packages/core-connections/src/DriverPropertiesService.ts index e827c6a670..1b72f9bc03 100644 --- a/webapp/packages/core-connections/src/DriverPropertiesService.ts +++ b/webapp/packages/core-connections/src/DriverPropertiesService.ts @@ -18,7 +18,7 @@ export class DriverPropertiesService { } async loadDriverProperties(driverId: string): Promise { - const response = await this.graphQLService.gql.driverProperties({ + const response = await this.graphQLService.sdk.driverProperties({ driverId, }); diff --git a/webapp/packages/core-localization/src/LocalizationService.ts b/webapp/packages/core-localization/src/LocalizationService.ts index 2227519dcd..61f403bc4e 100644 --- a/webapp/packages/core-localization/src/LocalizationService.ts +++ b/webapp/packages/core-localization/src/LocalizationService.ts @@ -93,7 +93,7 @@ export class LocalizationService extends Bootstrap { if (key === this.settings.language) { return; } - const response = await this.graphQLService.gql.changeSessionLanguage({ locale: key }); + const response = await this.graphQLService.sdk.changeSessionLanguage({ locale: key }); this.setLocale(key); if (response.changeSessionLanguage) { window.location.reload(); diff --git a/webapp/packages/core-root/src/PermissionsResource.ts b/webapp/packages/core-root/src/PermissionsResource.ts index 2c21a40285..eea8f20bae 100644 --- a/webapp/packages/core-root/src/PermissionsResource.ts +++ b/webapp/packages/core-root/src/PermissionsResource.ts @@ -30,7 +30,7 @@ export class PermissionsResource extends CachedDataResource, null> { } protected async loader(key: null): Promise> { - const { permissions } = await this.graphQLService.gql.sessionPermissions(); + const { permissions } = await this.graphQLService.sdk.sessionPermissions(); this.data.clear(); for (const permission of permissions) { diff --git a/webapp/packages/core-root/src/ServerConfigResource.ts b/webapp/packages/core-root/src/ServerConfigResource.ts index 82631d7f22..721b4fd6ec 100644 --- a/webapp/packages/core-root/src/ServerConfigResource.ts +++ b/webapp/packages/core-root/src/ServerConfigResource.ts @@ -26,7 +26,7 @@ export class ServerConfigResource extends CachedDataResource { - const { serverConfig } = await this.graphQLService.gql.serverConfig(); + const { serverConfig } = await this.graphQLService.sdk.serverConfig(); this.markUpdated(key); return serverConfig; diff --git a/webapp/packages/core-root/src/SessionResource.ts b/webapp/packages/core-root/src/SessionResource.ts index 0a89e82c8d..c8d9cb3989 100644 --- a/webapp/packages/core-root/src/SessionResource.ts +++ b/webapp/packages/core-root/src/SessionResource.ts @@ -35,7 +35,7 @@ export class SessionResource extends CachedDataResource { - const { session } = await this.graphQLService.gql.openSession(); + const { session } = await this.graphQLService.sdk.openSession(); this.markUpdated(key); return session; diff --git a/webapp/packages/core-sdk/src/CustomGraphQLClient.ts b/webapp/packages/core-sdk/src/CustomGraphQLClient.ts new file mode 100644 index 0000000000..a582e2155c --- /dev/null +++ b/webapp/packages/core-sdk/src/CustomGraphQLClient.ts @@ -0,0 +1,51 @@ +/* + * cloudbeaver - Cloud Database Manager + * Copyright (C) 2020 DBeaver Corp and others + * + * Licensed under the Apache License, Version 2.0. + * you may not use this file except in compliance with the License. + */ + +import { GraphQLClient } from 'graphql-request'; +import { Variables, ClientError } from 'graphql-request/dist/src/types'; + +import { GQLError } from './GQLError'; +import { IResponseInterceptor } from './IResponseInterceptor'; + +export class CustomGraphQLClient extends GraphQLClient { + private interceptors: IResponseInterceptor[] = []; + + constructor(endpoint: string) { + super(endpoint); + } + + registerInterceptor(interceptor: IResponseInterceptor): void { + this.interceptors.push(interceptor); + } + + request(query: string, variables?: Variables): Promise { + return this.interceptors.reduce( + (accumulator, interceptor) => interceptor(accumulator), + this.overrideRequest(query, variables) + ); + } + + private async overrideRequest(query: string, variables?: Variables): Promise { + try { + const response = await this.rawRequest(query, variables); + + // TODO: seems here can be undefined + return response.data as T; + } catch (error) { + if (isClientError(error)) { + throw new GQLError(error); + } + throw error; + } + } +} + +function isClientError(obj: any): obj is ClientError { + // in es5 build `instanceof ClientError` always false, so we try to determine by checking response property + return obj instanceof ClientError || obj.response; +} diff --git a/webapp/packages/core-sdk/src/GraphQLService.ts b/webapp/packages/core-sdk/src/GraphQLService.ts index 5279d50c19..80f9c3d959 100644 --- a/webapp/packages/core-sdk/src/GraphQLService.ts +++ b/webapp/packages/core-sdk/src/GraphQLService.ts @@ -6,55 +6,26 @@ * you may not use this file except in compliance with the License. */ -import { GraphQLClient } from 'graphql-request'; -import { Variables, ClientError } from 'graphql-request/dist/src/types'; - import { injectable } from '@cloudbeaver/core-di'; +import { CustomGraphQLClient } from './CustomGraphQLClient'; import { EnvironmentService } from './EnvironmentService'; -import { GQLError } from './GQLError'; import { IResponseInterceptor } from './IResponseInterceptor'; import { getSdk } from './sdk'; @injectable() export class GraphQLService { - gql: ReturnType - private client: GraphQLClient; + sdk: ReturnType - private interceptors: IResponseInterceptor[] = []; + private client: CustomGraphQLClient; constructor(private environmentService: EnvironmentService) { const gqlEndpoint = this.environmentService.gqlEndpoint; - this.client = new GraphQLClient(gqlEndpoint); - this.gql = getSdk({ ...this.client, request: this.interceptedRequest.bind(this) } as any); + this.client = new CustomGraphQLClient(gqlEndpoint); + this.sdk = getSdk(this.client); } registerInterceptor(interceptor: IResponseInterceptor): void { - this.interceptors.push(interceptor); - } - - private interceptedRequest(query: string, variables?: Variables) { - return this.interceptors.reduce( - (accumulator, interceptor) => interceptor(accumulator), - this.overrideRequest(query, variables) - ); - } - - private async overrideRequest(query: string, variables?: Variables) { - try { - const response = await this.client.rawRequest(query, variables); - - return response.data; - } catch (error) { - if (isClientError(error)) { - throw new GQLError(error); - } - throw error; - } + this.client.registerInterceptor(interceptor); } } - -function isClientError(obj: any): obj is ClientError { - // in es5 build `instanceof ClientError` always false, so we try to determine by checking response property - return obj instanceof ClientError || obj.response; -} diff --git a/webapp/packages/core-sdk/src/index.ts b/webapp/packages/core-sdk/src/index.ts index 5dd82df9cd..0d5a5138c0 100644 --- a/webapp/packages/core-sdk/src/index.ts +++ b/webapp/packages/core-sdk/src/index.ts @@ -1,10 +1,11 @@ export * from './CachedDataResource'; -export * from './GraphQLService'; -export * from './EnvironmentService'; -export * from './sdk'; -export * from './GQLError'; -export * from './ServerInternalError'; -export * from './CachedResource'; export * from './CachedMapResource'; +export * from './CachedResource'; +export * from './CustomGraphQLClient'; +export * from './EnvironmentService'; export * from './EServerErrorCode'; +export * from './GQLError'; export * from './GQLErrorCatcher'; +export * from './GraphQLService'; +export * from './sdk'; +export * from './ServerInternalError'; diff --git a/webapp/packages/plugin-connection-custom/src/CustomConnectionService.ts b/webapp/packages/plugin-connection-custom/src/CustomConnectionService.ts index a8ba449118..73328e48d5 100644 --- a/webapp/packages/plugin-connection-custom/src/CustomConnectionService.ts +++ b/webapp/packages/plugin-connection-custom/src/CustomConnectionService.ts @@ -20,7 +20,7 @@ export class CustomConnectionService { } async createConnectionAsync(config: ConnectionConfig): Promise { - const response = await this.graphQLService.gql.createConnection({ + const response = await this.graphQLService.sdk.createConnection({ config, }); @@ -30,7 +30,7 @@ export class CustomConnectionService { } async testConnectionAsync(config: ConnectionConfig): Promise { - await this.graphQLService.gql.testConnection({ + await this.graphQLService.sdk.testConnection({ config, }); } diff --git a/webapp/packages/plugin-connection-template/src/TemplateConnectionsResource.ts b/webapp/packages/plugin-connection-template/src/TemplateConnectionsResource.ts index 1afc48f4a2..a2fca57769 100644 --- a/webapp/packages/plugin-connection-template/src/TemplateConnectionsResource.ts +++ b/webapp/packages/plugin-connection-template/src/TemplateConnectionsResource.ts @@ -40,7 +40,7 @@ export class TemplateConnectionsResource extends CachedDataResource { ): Promise { // start async task try { - const { taskInfo } = await this.graphQLService.gql.exportDataFromContainer({ + const { taskInfo } = await this.graphQLService.sdk.exportDataFromContainer({ connectionId, containerNodePath, parameters, @@ -80,7 +80,7 @@ export class ExportFromContainerProcess extends Deferred { } // run the first check immediately because usually the query execution is fast try { - const { taskInfo } = await this.graphQLService.gql.getAsyncTaskInfo({ + const { taskInfo } = await this.graphQLService.sdk.getAsyncTaskInfo({ taskId: this.taskId, removeOnFinish: false, }); @@ -104,7 +104,7 @@ export class ExportFromContainerProcess extends Deferred { return; } try { - await this.graphQLService.gql.asyncTaskCancel({ taskId }); + await this.graphQLService.sdk.asyncTaskCancel({ taskId }); this.isCancelConfirmed = true; } catch (e) { if (this.getState() === EDeferredState.CANCELLING) { @@ -131,7 +131,7 @@ export class ExportFromContainerProcess extends Deferred { } // task execution successful this.toResolved(taskInfo.taskResult); - await this.graphQLService.gql.getAsyncTaskInfo({ taskId: taskInfo.id, removeOnFinish: true }); + await this.graphQLService.sdk.getAsyncTaskInfo({ taskId: taskInfo.id, removeOnFinish: true }); } private onError(error: Error, status?: string) { diff --git a/webapp/packages/plugin-data-export/src/ExportFromResultsProcess.ts b/webapp/packages/plugin-data-export/src/ExportFromResultsProcess.ts index 3d202d56d8..1861732bac 100644 --- a/webapp/packages/plugin-data-export/src/ExportFromResultsProcess.ts +++ b/webapp/packages/plugin-data-export/src/ExportFromResultsProcess.ts @@ -36,7 +36,7 @@ export class ExportFromResultsProcess extends Deferred { ): Promise { // start async task try { - const { taskInfo } = await this.graphQLService.gql.exportDataFromResults({ + const { taskInfo } = await this.graphQLService.sdk.exportDataFromResults({ connectionId, contextId, resultsId, @@ -82,7 +82,7 @@ export class ExportFromResultsProcess extends Deferred { } // run the first check immediately because usually the query execution is fast try { - const { taskInfo } = await this.graphQLService.gql.getAsyncTaskInfo({ + const { taskInfo } = await this.graphQLService.sdk.getAsyncTaskInfo({ taskId: this.taskId, removeOnFinish: false, }); @@ -106,7 +106,7 @@ export class ExportFromResultsProcess extends Deferred { return; } try { - await this.graphQLService.gql.asyncTaskCancel({ taskId }); + await this.graphQLService.sdk.asyncTaskCancel({ taskId }); this.isCancelConfirmed = true; } catch (e) { if (this.getState() === EDeferredState.CANCELLING) { @@ -133,7 +133,7 @@ export class ExportFromResultsProcess extends Deferred { } // task execution successful this.toResolved(taskInfo.taskResult); - await this.graphQLService.gql.getAsyncTaskInfo({ taskId: taskInfo.id, removeOnFinish: true }); + await this.graphQLService.sdk.getAsyncTaskInfo({ taskId: taskInfo.id, removeOnFinish: true }); } private onError(error: Error, status?: string) { diff --git a/webapp/packages/plugin-data-viewer/src/DataViewerTableService.ts b/webapp/packages/plugin-data-viewer/src/DataViewerTableService.ts index 814bced59b..dd0d4687eb 100644 --- a/webapp/packages/plugin-data-viewer/src/DataViewerTableService.ts +++ b/webapp/packages/plugin-data-viewer/src/DataViewerTableService.ts @@ -55,7 +55,7 @@ export class DataViewerTableService { defaultSchema?: string ): Promise { - const response = await this.graphQLService.gql.sqlContextCreate({ + const response = await this.graphQLService.sdk.sqlContextCreate({ connectionId, defaultCatalog, defaultSchema, @@ -77,7 +77,7 @@ export class DataViewerTableService { throw new Error('It is expected that data.sqlContextParams was set after first fetch'); } - const response = await this.graphQLService.gql.updateResultsDataBatch({ + const response = await this.graphQLService.sdk.updateResultsDataBatch({ connectionId: data.executionContext.connectionId, contextId: data.executionContext.contextId, resultsId: data.resultId, @@ -111,7 +111,7 @@ export class DataViewerTableService { model.executionContext = executionContext; } - const { readDataFromContainer } = await this.graphQLService.gql.readDataFromContainer({ + const { readDataFromContainer } = await this.graphQLService.sdk.readDataFromContainer({ connectionId: model.executionContext.connectionId, contextId: model.executionContext.contextId, containerNodePath: model.containerNodePath, diff --git a/webapp/packages/plugin-ddl-viewer/src/DdlViewerService.ts b/webapp/packages/plugin-ddl-viewer/src/DdlViewerService.ts index 1841e30257..b680870afd 100644 --- a/webapp/packages/plugin-ddl-viewer/src/DdlViewerService.ts +++ b/webapp/packages/plugin-ddl-viewer/src/DdlViewerService.ts @@ -37,7 +37,7 @@ export class DdlViewerService { } private async fetchDdlMetadata(nodeId: string): Promise { - const response = await this.graphQLService.gql.metadataGetNodeDDL({ nodeId }); + const response = await this.graphQLService.sdk.metadataGetNodeDDL({ nodeId }); return response.metadataGetNodeDDL || ''; } diff --git a/webapp/packages/plugin-sql-editor/src/SqlDialectInfoService.ts b/webapp/packages/plugin-sql-editor/src/SqlDialectInfoService.ts index d29cb5fcc1..b4c3e59c43 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlDialectInfoService.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlDialectInfoService.ts @@ -28,7 +28,7 @@ export class SqlDialectInfoService { async loadSqlDialectInfo(connectionId: string): Promise { if (!this.dialectInfo.has(connectionId)) { try { - const result = await this.graphQLService.gql.querySqlDialectInfo({ connectionId }); + const result = await this.graphQLService.sdk.querySqlDialectInfo({ connectionId }); this.dialectInfo.set(connectionId, result.dialect!); } catch (error) { this.notificationService.logException(error, 'Failed to load SqlDialectInfo'); diff --git a/webapp/packages/plugin-sql-editor/src/SqlEditor/SqlEditorService.ts b/webapp/packages/plugin-sql-editor/src/SqlEditor/SqlEditorService.ts index dc34e40a7c..1310b9a89f 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlEditor/SqlEditorService.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlEditor/SqlEditorService.ts @@ -22,7 +22,7 @@ export class SqlEditorService { cursor: number ): Promise { - const result = await this.gql.gql.querySqlCompletionProposals({ + const result = await this.gql.sdk.querySqlCompletionProposals({ connectionId, contextId, query, diff --git a/webapp/packages/plugin-sql-editor/src/SqlEditorTabService.ts b/webapp/packages/plugin-sql-editor/src/SqlEditorTabService.ts index 3e336a37fa..cbf35dea18 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlEditorTabService.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlEditorTabService.ts @@ -235,7 +235,7 @@ export class SqlEditorTabService { return; } try { - await this.gql.gql.sqlContextDestroy({ connectionId, contextId }); + await this.gql.sdk.sqlContextDestroy({ connectionId, contextId }); } catch (exception) { this.notificationService.logException(exception, `Failed to destroy SQL-context ${contextId}`, true); } @@ -253,7 +253,7 @@ export class SqlEditorTabService { defaultSchema?: string ): Promise { - const response = await this.gql.gql.sqlContextCreate({ + const response = await this.gql.sdk.sqlContextCreate({ connectionId, defaultCatalog, defaultSchema, @@ -275,7 +275,7 @@ export class SqlEditorTabService { defaultCatalog?: string, defaultSchema?: string ) { - await this.gql.gql.sqlContextSetDefaults({ + await this.gql.sdk.sqlContextSetDefaults({ connectionId, contextId, defaultCatalog, diff --git a/webapp/packages/plugin-sql-editor/src/SqlResultTabs/SQLQueryExecutionProcess.ts b/webapp/packages/plugin-sql-editor/src/SqlResultTabs/SQLQueryExecutionProcess.ts index 34c218ebe7..4d2aabd9b8 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlResultTabs/SQLQueryExecutionProcess.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlResultTabs/SQLQueryExecutionProcess.ts @@ -109,7 +109,7 @@ export class SQLQueryExecutionProcess extends Deferred { sqlQueryParams: ISqlQueryParams, filter: SqlDataFilter, ): Promise { - const { taskInfo } = await this.graphQLService.gql.asyncSqlExecuteQuery({ + const { taskInfo } = await this.graphQLService.sdk.asyncSqlExecuteQuery({ connectionId: sqlQueryParams.connectionId, contextId: sqlQueryParams.contextId, query: sqlQueryParams.query, @@ -119,12 +119,12 @@ export class SQLQueryExecutionProcess extends Deferred { } private async getQueryStatusAsync(taskId: string): Promise { - const { taskInfo } = await this.graphQLService.gql.getAsyncTaskInfo({ taskId, removeOnFinish: false }); + const { taskInfo } = await this.graphQLService.sdk.getAsyncTaskInfo({ taskId, removeOnFinish: false }); return taskInfo; } private async cancelQueryAsync(taskId: string): Promise { - await this.graphQLService.gql.asyncTaskCancel({ taskId }); + await this.graphQLService.sdk.asyncTaskCancel({ taskId }); } private async applyResult(taskInfo: AsyncTaskInfo) { @@ -140,9 +140,9 @@ export class SQLQueryExecutionProcess extends Deferred { } try { // task execution successful - const { result } = await this.graphQLService.gql.getSqlExecuteTaskResults({ taskId: taskInfo.id }); + const { result } = await this.graphQLService.sdk.getSqlExecuteTaskResults({ taskId: taskInfo.id }); this.toResolved(result); - await this.graphQLService.gql.getAsyncTaskInfo({ taskId: taskInfo.id, removeOnFinish: true }); + await this.graphQLService.sdk.getAsyncTaskInfo({ taskId: taskInfo.id, removeOnFinish: true }); } catch (exception) { this.onError(new Error('Tasks execution returns no result'), exception); } diff --git a/webapp/packages/plugin-sql-editor/src/SqlResultTabs/SqlResultService.ts b/webapp/packages/plugin-sql-editor/src/SqlResultTabs/SqlResultService.ts index 0903825c9c..6c7ebbb26b 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlResultTabs/SqlResultService.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlResultTabs/SqlResultService.ts @@ -7,24 +7,15 @@ */ import { injectable } from '@cloudbeaver/core-di'; -import { NotificationService } from '@cloudbeaver/core-events'; -import { - GraphQLService, - SqlExecuteInfo, - SqlDataFilter, -} from '@cloudbeaver/core-sdk'; +import { GraphQLService, SqlExecuteInfo } from '@cloudbeaver/core-sdk'; import { IRequestDataResult, RowDiff } from '@cloudbeaver/plugin-data-viewer'; import { ISqlQueryParams } from '../ISqlEditorTabState'; -import { SQLQueryExecutionProcess } from './SQLQueryExecutionProcess'; @injectable() export class SqlResultService { - constructor( - private graphQLService: GraphQLService, - private notificationService: NotificationService - ) { } + constructor(private graphQLService: GraphQLService) { } async saveChanges( sqlQueryParams: ISqlQueryParams, @@ -32,7 +23,7 @@ export class SqlResultService { diff: RowDiff[] ): Promise { - const response = await this.graphQLService.gql.updateResultsDataBatch({ + const response = await this.graphQLService.sdk.updateResultsDataBatch({ connectionId: sqlQueryParams.connectionId, contextId: sqlQueryParams.contextId, resultsId: resultId,