Query dynamic cache key computation (#12814)
In this PR: - add query hashKey to ObjectMetadataItems query graphql cache to avoid caching outdated queries - improve performance by removing ResolveField at FieldLevel and adding this at resolver level
This commit is contained in:
@ -145,14 +145,15 @@ export const SettingsObjectNewFieldConfigure = () => {
|
||||
});
|
||||
}
|
||||
|
||||
navigate(SettingsPath.ObjectDetail, {
|
||||
objectNamePlural,
|
||||
});
|
||||
|
||||
// TODO: fix optimistic update logic
|
||||
// Forcing a refetch for now but it's not ideal
|
||||
await apolloClient.refetchQueries({
|
||||
include: ['FindManyViews', 'CombinedFindManyRecords'],
|
||||
});
|
||||
navigate(SettingsPath.ObjectDetail, {
|
||||
objectNamePlural,
|
||||
});
|
||||
setIsSaving(false);
|
||||
} catch (error) {
|
||||
setIsSaving(false);
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
import { createHash } from 'crypto';
|
||||
|
||||
import { isDefined } from 'class-validator';
|
||||
import { Plugin } from 'graphql-yoga';
|
||||
|
||||
@ -20,8 +22,11 @@ export function useCachedMetadata(config: CacheMetadataPluginConfig): Plugin {
|
||||
const localeCacheKey = isDefined(serverContext.req.headers['x-locale'])
|
||||
? `:${locale}`
|
||||
: '';
|
||||
const queryHash = createHash('sha256')
|
||||
.update(serverContext.req.body.query)
|
||||
.digest('hex');
|
||||
|
||||
return `graphql:operations:${operationName}:${workspaceId}:${workspaceMetadataVersion}${localeCacheKey}`;
|
||||
return `graphql:operations:${operationName}:${workspaceId}:${workspaceMetadataVersion}${localeCacheKey}:${queryHash}`;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import DataLoader from 'dataloader';
|
||||
import { APP_LOCALES } from 'twenty-shared/translations';
|
||||
|
||||
import { FieldMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata.interface';
|
||||
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
|
||||
@ -37,6 +38,7 @@ export type RelationLoaderPayload = {
|
||||
export type FieldMetadataLoaderPayload = {
|
||||
workspaceId: string;
|
||||
objectMetadata: Pick<ObjectMetadataInterface, 'id'>;
|
||||
locale?: keyof typeof APP_LOCALES;
|
||||
};
|
||||
|
||||
export type IndexMetadataLoaderPayload = {
|
||||
@ -140,11 +142,34 @@ export class DataloaderService {
|
||||
Object.values(objectMetadataMaps.byId[id].fieldsById).map(
|
||||
// TODO: fix this as we should merge FieldMetadataEntity and FieldMetadataInterface
|
||||
(fieldMetadata) => {
|
||||
const overridesFieldToCompute = [
|
||||
'icon',
|
||||
'label',
|
||||
'description',
|
||||
] as const satisfies (keyof FieldMetadataInterface)[];
|
||||
|
||||
const overrides = overridesFieldToCompute.reduce<
|
||||
Partial<
|
||||
Record<(typeof overridesFieldToCompute)[number], string>
|
||||
>
|
||||
>(
|
||||
(acc, field) => ({
|
||||
...acc,
|
||||
[field]: this.fieldMetadataService.resolveOverridableString(
|
||||
fieldMetadata,
|
||||
field,
|
||||
dataLoaderParams[0].locale,
|
||||
),
|
||||
}),
|
||||
{},
|
||||
);
|
||||
|
||||
return {
|
||||
...fieldMetadata,
|
||||
createdAt: new Date(fieldMetadata.createdAt),
|
||||
updatedAt: new Date(fieldMetadata.updatedAt),
|
||||
workspaceId: workspaceId,
|
||||
...overrides,
|
||||
};
|
||||
},
|
||||
),
|
||||
|
||||
@ -55,43 +55,6 @@ export class FieldMetadataResolver {
|
||||
private readonly beforeUpdateOneField: BeforeUpdateOneField<UpdateFieldInput>,
|
||||
) {}
|
||||
|
||||
@ResolveField(() => String, { nullable: true })
|
||||
async label(
|
||||
@Parent() fieldMetadata: FieldMetadataDTO,
|
||||
@Context() context: I18nContext,
|
||||
): Promise<string> {
|
||||
return this.fieldMetadataService.resolveOverridableString(
|
||||
fieldMetadata,
|
||||
'label',
|
||||
context.req.headers['x-locale'],
|
||||
);
|
||||
}
|
||||
|
||||
@ResolveField(() => String, { nullable: true })
|
||||
async description(
|
||||
@Parent() fieldMetadata: FieldMetadataDTO,
|
||||
@Context() context: I18nContext,
|
||||
): Promise<string> {
|
||||
return this.fieldMetadataService.resolveOverridableString(
|
||||
fieldMetadata,
|
||||
'description',
|
||||
context.req.headers['x-locale'],
|
||||
);
|
||||
}
|
||||
|
||||
@UseGuards(SettingsPermissionsGuard(SettingPermissionType.DATA_MODEL))
|
||||
@ResolveField(() => String, { nullable: true })
|
||||
async icon(
|
||||
@Parent() fieldMetadata: FieldMetadataDTO,
|
||||
@Context() context: I18nContext,
|
||||
): Promise<string> {
|
||||
return this.fieldMetadataService.resolveOverridableString(
|
||||
fieldMetadata,
|
||||
'icon',
|
||||
context.req.headers['x-locale'],
|
||||
);
|
||||
}
|
||||
|
||||
@UseGuards(SettingsPermissionsGuard(SettingPermissionType.DATA_MODEL))
|
||||
@Mutation(() => FieldMetadataDTO)
|
||||
async createOneField(
|
||||
|
||||
@ -4,7 +4,7 @@ import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
|
||||
import { i18n } from '@lingui/core';
|
||||
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
||||
import isEmpty from 'lodash.isempty';
|
||||
import { APP_LOCALES, SOURCE_LOCALE } from 'twenty-shared/translations';
|
||||
import { APP_LOCALES } from 'twenty-shared/translations';
|
||||
import { FieldMetadataType } from 'twenty-shared/types';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { DataSource, FindOneOptions, In, Repository } from 'typeorm';
|
||||
@ -608,26 +608,18 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
|
||||
return fieldMetadataInput;
|
||||
}
|
||||
|
||||
async resolveOverridableString(
|
||||
fieldMetadata: FieldMetadataDTO,
|
||||
resolveOverridableString(
|
||||
fieldMetadata: Pick<
|
||||
FieldMetadataDTO,
|
||||
'label' | 'description' | 'icon' | 'isCustom' | 'standardOverrides'
|
||||
>,
|
||||
labelKey: 'label' | 'description' | 'icon',
|
||||
locale: keyof typeof APP_LOCALES | undefined,
|
||||
): Promise<string> {
|
||||
): string {
|
||||
if (fieldMetadata.isCustom) {
|
||||
return fieldMetadata[labelKey] ?? '';
|
||||
}
|
||||
|
||||
if (!locale || locale === SOURCE_LOCALE) {
|
||||
if (
|
||||
fieldMetadata.standardOverrides &&
|
||||
isDefined(fieldMetadata.standardOverrides[labelKey])
|
||||
) {
|
||||
return fieldMetadata.standardOverrides[labelKey] as string;
|
||||
}
|
||||
|
||||
return fieldMetadata[labelKey] ?? '';
|
||||
}
|
||||
|
||||
const translationValue =
|
||||
// @ts-expect-error legacy noImplicitAny
|
||||
fieldMetadata.standardOverrides?.translations?.[locale]?.[labelKey];
|
||||
|
||||
@ -134,13 +134,14 @@ export class ObjectMetadataResolver {
|
||||
async fieldsList(
|
||||
@AuthWorkspace() workspace: Workspace,
|
||||
@Parent() objectMetadata: ObjectMetadataDTO,
|
||||
@Context() context: { loaders: IDataloaders },
|
||||
@Context() context: { loaders: IDataloaders } & I18nContext,
|
||||
): Promise<FieldMetadataDTO[]> {
|
||||
try {
|
||||
const fieldMetadataItems = await context.loaders.fieldMetadataLoader.load(
|
||||
{
|
||||
objectMetadata,
|
||||
workspaceId: workspace.id,
|
||||
locale: context.req.headers['x-locale'],
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user