feat: add memory cache to boost performance (#2620)
* feat: add memory cache to boost performance * fix: tests * fix: logging * fix: missing commented stuff
This commit is contained in:
@ -5,6 +5,7 @@ import { makeExecutableSchema } from '@graphql-tools/schema';
|
||||
import { gql } from 'graphql-tag';
|
||||
|
||||
import { DataSourceService } from 'src/metadata/data-source/data-source.service';
|
||||
import { WorkspaceSchemaStorageService } from 'src/workspace/workspace-schema-storage/workspace-schema-storage.service';
|
||||
import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service';
|
||||
|
||||
import { WorkspaceGraphQLSchemaFactory } from './workspace-schema-builder/workspace-graphql-schema.factory';
|
||||
@ -18,6 +19,7 @@ export class WorkspaceFactory {
|
||||
private readonly objectMetadataService: ObjectMetadataService,
|
||||
private readonly workspaceGraphQLSchemaFactory: WorkspaceGraphQLSchemaFactory,
|
||||
private readonly workspaceResolverFactory: WorkspaceResolverFactory,
|
||||
private readonly workspaceSchemaStorageService: WorkspaceSchemaStorageService,
|
||||
) {}
|
||||
|
||||
async createGraphQLSchema(
|
||||
@ -37,15 +39,43 @@ export class WorkspaceFactory {
|
||||
return new GraphQLSchema({});
|
||||
}
|
||||
|
||||
const objectMetadataCollection =
|
||||
await this.objectMetadataService.getObjectMetadataFromWorkspaceId(
|
||||
workspaceId,
|
||||
);
|
||||
// Get object metadata from cache
|
||||
let objectMetadataCollection =
|
||||
await this.workspaceSchemaStorageService.getObjectMetadata(workspaceId);
|
||||
|
||||
const autoGeneratedSchema = await this.workspaceGraphQLSchemaFactory.create(
|
||||
objectMetadataCollection,
|
||||
workspaceResolverBuilderMethodNames,
|
||||
// If object metadata is not cached, get it from the database
|
||||
if (!objectMetadataCollection) {
|
||||
objectMetadataCollection =
|
||||
await this.objectMetadataService.getObjectMetadataFromWorkspaceId(
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
await this.workspaceSchemaStorageService.setObjectMetadata(
|
||||
workspaceId,
|
||||
objectMetadataCollection,
|
||||
);
|
||||
}
|
||||
|
||||
// Get typeDefs from cache
|
||||
let typeDefs = await this.workspaceSchemaStorageService.getTypeDefs(
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
// If typeDefs are not cached, generate them
|
||||
if (!typeDefs) {
|
||||
const autoGeneratedSchema =
|
||||
await this.workspaceGraphQLSchemaFactory.create(
|
||||
objectMetadataCollection,
|
||||
workspaceResolverBuilderMethodNames,
|
||||
);
|
||||
typeDefs = printSchema(autoGeneratedSchema);
|
||||
|
||||
await this.workspaceSchemaStorageService.setTypeDefs(
|
||||
workspaceId,
|
||||
typeDefs,
|
||||
);
|
||||
}
|
||||
|
||||
const autoGeneratedResolvers = await this.workspaceResolverFactory.create(
|
||||
workspaceId,
|
||||
objectMetadataCollection,
|
||||
@ -53,7 +83,6 @@ export class WorkspaceFactory {
|
||||
);
|
||||
|
||||
// TODO: Cache the generate type definitions
|
||||
const typeDefs = printSchema(autoGeneratedSchema);
|
||||
const executableSchema = makeExecutableSchema({
|
||||
typeDefs: gql`
|
||||
${typeDefs}
|
||||
|
||||
Reference in New Issue
Block a user