Skip to content

Commit f1d6796

Browse files
authored
refactor(api): remove abstraction of schema service caller (#6781)
1 parent 6f43b3e commit f1d6796

File tree

14 files changed

+73
-365
lines changed

14 files changed

+73
-365
lines changed

packages/services/api/src/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { provideSchemaModuleConfig, SchemaModuleConfig } from './modules/schema/
4242
import {
4343
SCHEMA_SERVICE_CONFIG,
4444
SchemaServiceConfig,
45-
} from './modules/schema/providers/orchestrators/tokens';
45+
} from './modules/schema/providers/orchestrator/tokens';
4646
import { sharedModule } from './modules/shared';
4747
import { CryptoProvider, encryptionSecretProvider } from './modules/shared/providers/crypto';
4848
import { DistributedCache } from './modules/shared/providers/distributed-cache';

packages/services/api/src/modules/schema/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Contracts } from './providers/contracts';
44
import { ContractsManager } from './providers/contracts-manager';
55
import { Inspector } from './providers/inspector';
66
import { models } from './providers/models';
7-
import { orchestrators } from './providers/orchestrators';
7+
import { CompositionOrchestrator } from './providers/orchestrator/composition-orchestrator';
88
import { RegistryChecks } from './providers/registry-checks';
99
import { SchemaCheckManager } from './providers/schema-check-manager';
1010
import { SchemaHelper } from './providers/schema-helper';
@@ -30,7 +30,7 @@ export const schemaModule = createModule({
3030
ContractsManager,
3131
SchemaCheckManager,
3232
BreakingSchemaChangeUsageHelper,
33-
...orchestrators,
33+
CompositionOrchestrator,
3434
...models,
3535
],
3636
});

packages/services/api/src/modules/schema/providers/models/composite.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Injectable, Scope } from 'graphql-modules';
22
import { traceFn } from '@hive/service-common';
33
import { SchemaChangeType } from '@hive/storage';
4-
import { FederationOrchestrator } from '../orchestrators/federation';
5-
import { StitchingOrchestrator } from '../orchestrators/stitching';
64
import { RegistryChecks, type ConditionalBreakingChangeDiffConfig } from '../registry-checks';
75
import { swapServices } from '../schema-helper';
86
import { shouldUseLatestComposableVersion } from '../schema-manager';
@@ -39,8 +37,6 @@ import {
3937
})
4038
export class CompositeModel {
4139
constructor(
42-
private federationOrchestrator: FederationOrchestrator,
43-
private stitchingOrchestrator: StitchingOrchestrator,
4440
private checks: RegistryChecks,
4541
private logger: Logger,
4642
) {}
@@ -189,14 +185,7 @@ export class CompositeModel {
189185
};
190186
}
191187

192-
const orchestrator =
193-
project.type === ProjectType.FEDERATION
194-
? this.federationOrchestrator
195-
: this.stitchingOrchestrator;
196-
this.logger.debug('Using orchestrator of type %s', orchestrator.type);
197-
198188
const compositionCheck = await this.checks.composition({
199-
orchestrator,
200189
targetId: selector.targetId,
201190
project,
202191
organization,
@@ -215,7 +204,6 @@ export class CompositeModel {
215204
});
216205

217206
const previousVersionSdl = await this.checks.retrievePreviousVersionSdl({
218-
orchestrator,
219207
version: comparedVersion,
220208
organization,
221209
project,
@@ -432,13 +420,7 @@ export class CompositeModel {
432420
};
433421
}
434422

435-
const orchestrator =
436-
project.type === ProjectType.FEDERATION
437-
? this.federationOrchestrator
438-
: this.stitchingOrchestrator;
439-
440423
const compositionCheck = await this.checks.composition({
441-
orchestrator,
442424
targetId: target.id,
443425
project,
444426
organization,
@@ -488,7 +470,6 @@ export class CompositeModel {
488470
}
489471

490472
const previousVersionSdl = await this.checks.retrievePreviousVersionSdl({
491-
orchestrator,
492473
version: schemaVersionToCompareAgainst,
493474
organization,
494475
project,
@@ -632,15 +613,10 @@ export class CompositeModel {
632613
};
633614
}
634615

635-
const orchestrator =
636-
project.type === ProjectType.FEDERATION
637-
? this.federationOrchestrator
638-
: this.stitchingOrchestrator;
639616
const schemas = latestVersion.schemas.filter(s => s.service_name !== input.serviceName);
640617
schemas.sort((a, b) => a.service_name.localeCompare(b.service_name));
641618

642619
const compositionCheck = await this.checks.composition({
643-
orchestrator,
644620
targetId: selector.target,
645621
project,
646622
organization,
@@ -659,7 +635,6 @@ export class CompositeModel {
659635
});
660636

661637
const previousVersionSdl = await this.checks.retrievePreviousVersionSdl({
662-
orchestrator,
663638
version: compareToLatestComposable ? latestComposable : latest,
664639
organization,
665640
project,

packages/services/api/src/modules/schema/providers/models/single.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Injectable, Scope } from 'graphql-modules';
22
import { traceFn } from '@hive/service-common';
33
import { SchemaChangeType } from '@hive/storage';
4-
import { SingleOrchestrator } from '../orchestrators/single';
54
import { ConditionalBreakingChangeDiffConfig, RegistryChecks } from '../registry-checks';
65
import type { PublishInput } from '../schema-publisher';
76
import type { Organization, Project, SingleSchema, Target } from './../../../../shared/entities';
@@ -22,7 +21,6 @@ import {
2221
})
2322
export class SingleModel {
2423
constructor(
25-
private orchestrator: SingleOrchestrator,
2624
private checks: RegistryChecks,
2725
private logger: Logger,
2826
) {}
@@ -108,7 +106,6 @@ export class SingleModel {
108106
}
109107

110108
const compositionCheck = await this.checks.composition({
111-
orchestrator: this.orchestrator,
112109
targetId: selector.targetId,
113110
project,
114111
organization,
@@ -118,7 +115,6 @@ export class SingleModel {
118115
});
119116

120117
const previousVersionSdl = await this.checks.retrievePreviousVersionSdl({
121-
orchestrator: this.orchestrator,
122118
version: comparedVersion,
123119
organization,
124120
project,
@@ -239,7 +235,6 @@ export class SingleModel {
239235
}
240236

241237
const compositionCheck = await this.checks.composition({
242-
orchestrator: this.orchestrator,
243238
targetId: target.id,
244239
project,
245240
organization,
@@ -256,7 +251,6 @@ export class SingleModel {
256251
});
257252

258253
const previousVersionSdl = await this.checks.retrievePreviousVersionSdl({
259-
orchestrator: this.orchestrator,
260254
version: comparedVersion,
261255
organization,
262256
project,

packages/services/api/src/modules/schema/providers/orchestrators/federation.ts renamed to packages/services/api/src/modules/schema/providers/orchestrator/composition-orchestrator.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { traceFn } from '@hive/service-common';
55
import { createTRPCProxyClient, httpLink } from '@trpc/client';
66
import {
77
ComposeAndValidateResult,
8-
Orchestrator,
98
Project,
109
ProjectType,
1110
SchemaObject,
@@ -22,12 +21,27 @@ type ExternalCompositionConfig = {
2221
@Injectable({
2322
scope: Scope.Operation,
2423
})
25-
export class FederationOrchestrator implements Orchestrator {
26-
type = ProjectType.FEDERATION;
24+
/**
25+
* Orchestrate composition calls to the schema service.
26+
*/
27+
export class CompositionOrchestrator {
2728
private logger: Logger;
2829
private schemaService;
2930
private incomingRequestAbortSignal: AbortSignal;
3031

32+
static projectTypeToOrchestratorType(
33+
projectType: ProjectType,
34+
): 'federation' | 'stitching' | 'single' {
35+
switch (projectType) {
36+
case ProjectType.FEDERATION:
37+
return 'federation';
38+
case ProjectType.STITCHING:
39+
return 'stitching';
40+
case ProjectType.SINGLE:
41+
return 'single';
42+
}
43+
}
44+
3145
constructor(
3246
logger: Logger,
3347
@Inject(SCHEMA_SERVICE_CONFIG) serviceConfig: SchemaServiceConfig,
@@ -67,8 +81,9 @@ export class FederationOrchestrator implements Orchestrator {
6781
return null;
6882
}
6983

70-
@traceFn('FederationOrchestrator.composeAndValidate', {
71-
initAttributes: (schemas, config) => ({
84+
@traceFn('CompositionOrchestrator.composeAndValidate', {
85+
initAttributes: (ttype, schemas, config) => ({
86+
'hive.composition.type': ttype,
7287
'hive.composition.schema.count': schemas.length,
7388
'hive.composition.external': config.external.enabled,
7489
'hive.composition.native': config.native,
@@ -79,18 +94,35 @@ export class FederationOrchestrator implements Orchestrator {
7994
'hive.composition.contracts.count': result.contracts?.length ?? 0,
8095
}),
8196
})
97+
/**
98+
* Compose and validate schemas via the schema service.
99+
* - Requests time out after 30 seconds and result in a human readable error response
100+
* - In case the incoming request is canceled, the call to the schema service is aborted
101+
*/
82102
async composeAndValidate(
103+
compositionType: 'federation' | 'single' | 'stitching',
83104
schemas: SchemaObject[],
84105
config: {
106+
/** Whether external composition should be used (only Federation) */
85107
external: Project['externalComposition'];
108+
/** Whether native composition should be used (only Federation) */
86109
native: boolean;
110+
/** Specified contracts (only Federation) */
87111
contracts: ContractsInputType | null;
88112
},
89113
) {
90114
this.logger.debug(
91-
'Composing and Validating Federated Schemas (method=%s)',
92-
config.native ? 'native' : config.external.enabled ? 'external' : 'v1',
115+
'Composing and validating schemas (type=%s, method=%s)',
116+
compositionType,
117+
compositionType === 'federation'
118+
? config.native
119+
? 'native'
120+
: config.external.enabled
121+
? 'external'
122+
: 'v1'
123+
: 'none',
93124
);
125+
94126
const timeoutAbortSignal = AbortSignal.timeout(30_000);
95127

96128
const onTimeout = () => {
@@ -106,7 +138,7 @@ export class FederationOrchestrator implements Orchestrator {
106138
try {
107139
const result = await this.schemaService.composeAndValidate.mutate(
108140
{
109-
type: 'federation',
141+
type: compositionType,
110142
schemas: schemas.map(s => ({
111143
raw: s.raw,
112144
source: s.source,

packages/services/api/src/modules/schema/providers/orchestrators/errors.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/services/api/src/modules/schema/providers/orchestrators/index.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)