feat: new relation schema generation (#9882)

Fix https://github.com/twentyhq/core-team-issues/issues/295

Based on the feature-flag `IsNewRelationEnabled` the schema will be
marked as outdated and regenerated, this will cause an error on the
front-end on the first request on the following ones schema will be well
generated ans request will work.
This commit is contained in:
Jérémy M
2025-01-29 10:33:17 +01:00
committed by GitHub
parent f74bb5a60b
commit fbb67d74c8
18 changed files with 361 additions and 24 deletions

View File

@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { makeExecutableSchema } from '@graphql-tools/schema';
import chalk from 'chalk';
import { GraphQLSchema, printSchema } from 'graphql';
import { gql } from 'graphql-tag';
@ -13,9 +14,12 @@ import { workspaceResolverBuilderMethodNames } from 'src/engine/api/graphql/work
import { WorkspaceResolverFactory } from 'src/engine/api/graphql/workspace-resolver-builder/workspace-resolver.factory';
import { WorkspaceGraphQLSchemaFactory } from 'src/engine/api/graphql/workspace-schema-builder/workspace-graphql-schema.factory';
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { WorkspaceMetadataCacheService } from 'src/engine/metadata-modules/workspace-metadata-cache/services/workspace-metadata-cache.service';
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';
@Injectable()
export class WorkspaceSchemaFactory {
constructor(
@ -25,6 +29,7 @@ export class WorkspaceSchemaFactory {
private readonly workspaceResolverFactory: WorkspaceResolverFactory,
private readonly workspaceCacheStorageService: WorkspaceCacheStorageService,
private readonly workspaceMetadataCacheService: WorkspaceMetadataCacheService,
private readonly featureFlagService: FeatureFlagService,
) {}
async createGraphQLSchema(authContext: AuthContext): Promise<GraphQLSchema> {
@ -32,6 +37,22 @@ export class WorkspaceSchemaFactory {
return new GraphQLSchema({});
}
const cachedIsNewRelationEnabled =
await this.workspaceCacheStorageService.getIsNewRelationEnabled(
authContext.workspace.id,
);
const isNewRelationEnabled = await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IsNewRelationEnabled,
authContext.workspace.id,
);
if (isNewRelationEnabled) {
console.log(
chalk.yellow('🚧 New relation schema generation is enabled 🚧'),
);
}
const dataSourcesMetadata =
await this.dataSourceService.getDataSourcesMetadataFromWorkspaceId(
authContext.workspace.id,
@ -50,12 +71,41 @@ export class WorkspaceSchemaFactory {
await this.workspaceMetadataCacheService.recomputeMetadataCache({
workspaceId: authContext.workspace.id,
});
throw new GraphqlQueryRunnerException(
'Metadata cache version not found',
GraphqlQueryRunnerExceptionCode.METADATA_CACHE_VERSION_NOT_FOUND,
);
}
// TODO: remove this after the feature flag is droped
if (
(isNewRelationEnabled && cachedIsNewRelationEnabled === undefined) ||
(isNewRelationEnabled !== cachedIsNewRelationEnabled &&
cachedIsNewRelationEnabled !== undefined)
) {
console.log(
chalk.yellow('Recomputing due to new relation feature flag'),
{
isNewRelationEnabled,
},
);
await this.workspaceCacheStorageService.setIsNewRelationEnabled(
authContext.workspace.id,
isNewRelationEnabled,
);
await this.workspaceMetadataCacheService.recomputeMetadataCache({
workspaceId: authContext.workspace.id,
});
throw new GraphqlQueryRunnerException(
'Metadata cache recomputation required due to relation feature flag change',
GraphqlQueryRunnerExceptionCode.METADATA_CACHE_FEATURE_FLAG_RECOMPUTATION_REQUIRED,
);
}
const objectMetadataMaps =
await this.workspaceCacheStorageService.getObjectMetadataMaps(
authContext.workspace.id,