9ad8287dbc
# Introduction In this PR we've migrated `twenty-shared` from a `vite` app [libary-mode](https://vite.dev/guide/build#library-mode) to a [preconstruct](https://preconstruct.tools/) "atomic" application ( in the future would like to introduce preconstruct to handle of all our atomic dependencies such as `twenty-emails` `twenty-ui` etc it will be integrated at the monorepo's root directly, would be to invasive in the first, starting incremental via `twenty-shared`) For more information regarding the motivations please refer to nor: - https://github.com/twentyhq/core-team-issues/issues/587 - https://github.com/twentyhq/core-team-issues/issues/281#issuecomment-2630949682 close https://github.com/twentyhq/core-team-issues/issues/589 close https://github.com/twentyhq/core-team-issues/issues/590 ## How to test In order to ease the review this PR will ship all the codegen at the very end, the actual meaning full diff is `+2,411 −114` In order to migrate existing dependent packages to `twenty-shared` multi barrel new arch you need to run in local: ```sh yarn tsx packages/twenty-shared/scripts/migrateFromSingleToMultiBarrelImport.ts && \ npx nx run-many -t lint --fix -p twenty-front twenty-ui twenty-server twenty-emails twenty-shared twenty-zapier ``` Note that `migrateFromSingleToMultiBarrelImport` is idempotent, it's atm included in the PR but should not be merged. ( such as codegen will be added before merging this script will be removed ) ## Misc - related opened issue preconstruct https://github.com/preconstruct/preconstruct/issues/617 ## Closed related PR - https://github.com/twentyhq/twenty/pull/11028 - https://github.com/twentyhq/twenty/pull/10993 - https://github.com/twentyhq/twenty/pull/10960 ## Upcoming enhancement: ( in others dedicated PRs ) - 1/ refactor generate barrel to export atomic module instead of `*` - 2/ generate barrel own package with several files and tests - 3/ Migration twenty-ui the same way - 4/ Use `preconstruct` at monorepo global level ## Conclusion As always any suggestions are welcomed !
159 lines
5.6 KiB
TypeScript
159 lines
5.6 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
|
|
import { Brackets } from 'typeorm';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
|
|
import {
|
|
GraphqlQueryBaseResolverService,
|
|
GraphqlQueryResolverExecutionArgs,
|
|
} from 'src/engine/api/graphql/graphql-query-runner/interfaces/base-resolver-service';
|
|
import {
|
|
ObjectRecord,
|
|
ObjectRecordFilter,
|
|
OrderByDirection,
|
|
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
|
|
import { IConnection } from 'src/engine/api/graphql/workspace-query-runner/interfaces/connection.interface';
|
|
import { WorkspaceQueryRunnerOptions } from 'src/engine/api/graphql/workspace-query-runner/interfaces/query-runner-option.interface';
|
|
import { SearchResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
|
|
|
|
import { QUERY_MAX_RECORDS } from 'src/engine/api/graphql/graphql-query-runner/constants/query-max-records.constant';
|
|
import { ObjectRecordsToGraphqlConnectionHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/object-records-to-graphql-connection.helper';
|
|
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
|
|
import { formatSearchTerms } from 'src/engine/core-modules/global-search/utils/format-search-terms';
|
|
import { SEARCH_VECTOR_FIELD } from 'src/engine/metadata-modules/constants/search-vector-field.constants';
|
|
import { formatResult } from 'src/engine/twenty-orm/utils/format-result.util';
|
|
|
|
@Injectable()
|
|
export class GraphqlQuerySearchResolverService extends GraphqlQueryBaseResolverService<
|
|
SearchResolverArgs,
|
|
IConnection<ObjectRecord>
|
|
> {
|
|
async resolve(
|
|
executionArgs: GraphqlQueryResolverExecutionArgs<SearchResolverArgs>,
|
|
featureFlagsMap: Record<FeatureFlagKey, boolean>,
|
|
): Promise<IConnection<ObjectRecord>> {
|
|
const { authContext, objectMetadataMaps, objectMetadataItemWithFieldMaps } =
|
|
executionArgs.options;
|
|
|
|
const typeORMObjectRecordsParser =
|
|
new ObjectRecordsToGraphqlConnectionHelper(
|
|
objectMetadataMaps,
|
|
featureFlagsMap,
|
|
);
|
|
|
|
if (!isDefined(executionArgs.args.searchInput)) {
|
|
return typeORMObjectRecordsParser.createConnection({
|
|
objectRecords: [],
|
|
objectName: objectMetadataItemWithFieldMaps.nameSingular,
|
|
take: 0,
|
|
totalCount: 0,
|
|
order: [{ id: OrderByDirection.AscNullsFirst }],
|
|
hasNextPage: false,
|
|
hasPreviousPage: false,
|
|
});
|
|
}
|
|
|
|
const searchTerms = formatSearchTerms(
|
|
executionArgs.args.searchInput,
|
|
'and',
|
|
);
|
|
const searchTermsOr = formatSearchTerms(
|
|
executionArgs.args.searchInput,
|
|
'or',
|
|
);
|
|
|
|
const limit = executionArgs.args?.limit ?? QUERY_MAX_RECORDS;
|
|
|
|
const queryBuilder = executionArgs.repository.createQueryBuilder(
|
|
objectMetadataItemWithFieldMaps.nameSingular,
|
|
);
|
|
|
|
executionArgs.graphqlQueryParser.applyFilterToBuilder(
|
|
queryBuilder,
|
|
objectMetadataItemWithFieldMaps.nameSingular,
|
|
executionArgs.args.filter ?? ({} as ObjectRecordFilter),
|
|
);
|
|
|
|
const countQueryBuilder = queryBuilder.clone();
|
|
|
|
const resultsQueryBuilder =
|
|
searchTerms !== ''
|
|
? queryBuilder
|
|
.andWhere(
|
|
new Brackets((qb) => {
|
|
qb.where(
|
|
`"${SEARCH_VECTOR_FIELD.name}" @@ to_tsquery('simple', :searchTerms)`,
|
|
{ searchTerms },
|
|
).orWhere(
|
|
`"${SEARCH_VECTOR_FIELD.name}" @@ to_tsquery('simple', :searchTermsOr)`,
|
|
{ searchTermsOr },
|
|
);
|
|
}),
|
|
)
|
|
.orderBy(
|
|
`ts_rank_cd("${SEARCH_VECTOR_FIELD.name}", to_tsquery(:searchTerms))`,
|
|
'DESC',
|
|
)
|
|
.addOrderBy(
|
|
`ts_rank("${SEARCH_VECTOR_FIELD.name}", to_tsquery(:searchTermsOr))`,
|
|
'DESC',
|
|
)
|
|
.setParameter('searchTerms', searchTerms)
|
|
.setParameter('searchTermsOr', searchTermsOr)
|
|
.take(limit)
|
|
: queryBuilder
|
|
.andWhere(
|
|
new Brackets((qb) => {
|
|
qb.where(`"${SEARCH_VECTOR_FIELD.name}" IS NOT NULL`);
|
|
}),
|
|
)
|
|
.take(limit);
|
|
|
|
const resultsWithTsVector =
|
|
(await resultsQueryBuilder.getMany()) as ObjectRecord[];
|
|
|
|
const objectRecords = formatResult<ObjectRecord[]>(
|
|
resultsWithTsVector,
|
|
objectMetadataItemWithFieldMaps,
|
|
objectMetadataMaps,
|
|
);
|
|
|
|
const totalCount = isDefined(
|
|
executionArgs.graphqlQuerySelectedFieldsResult.aggregate.totalCount,
|
|
)
|
|
? await countQueryBuilder.getCount()
|
|
: 0;
|
|
const order = undefined;
|
|
|
|
if (executionArgs.graphqlQuerySelectedFieldsResult.relations) {
|
|
await this.processNestedRelationsHelper.processNestedRelations({
|
|
objectMetadataMaps,
|
|
parentObjectMetadataItem: objectMetadataItemWithFieldMaps,
|
|
parentObjectRecords: objectRecords,
|
|
relations: executionArgs.graphqlQuerySelectedFieldsResult.relations,
|
|
aggregate: executionArgs.graphqlQuerySelectedFieldsResult.aggregate,
|
|
limit,
|
|
authContext,
|
|
dataSource: executionArgs.dataSource,
|
|
isNewRelationEnabled:
|
|
featureFlagsMap[FeatureFlagKey.IsNewRelationEnabled],
|
|
});
|
|
}
|
|
|
|
return typeORMObjectRecordsParser.createConnection({
|
|
objectRecords: objectRecords ?? [],
|
|
objectName: objectMetadataItemWithFieldMaps.nameSingular,
|
|
take: limit,
|
|
totalCount,
|
|
order,
|
|
hasNextPage: false,
|
|
hasPreviousPage: false,
|
|
});
|
|
}
|
|
|
|
async validate(
|
|
_args: SearchResolverArgs,
|
|
_options: WorkspaceQueryRunnerOptions,
|
|
): Promise<void> {}
|
|
}
|