Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor graphql query runner + fix nested or #6986

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export enum GraphqlQueryRunnerExceptionCode {
FIELD_NOT_FOUND = 'FIELD_NOT_FOUND',
OBJECT_METADATA_NOT_FOUND = 'OBJECT_METADATA_NOT_FOUND',
RECORD_NOT_FOUND = 'RECORD_NOT_FOUND',
INVALID_ARGS_FIRST = 'INVALID_ARGS_FIRST',
INVALID_ARGS_LAST = 'INVALID_ARGS_LAST',
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ export class GraphqlQueryFilterConditionParser {
}

const result: FindOptionsWhere<ObjectLiteral> = {};
let orCondition: FindOptionsWhere<ObjectLiteral>[] | null = null;

for (const [key, value] of Object.entries(conditions)) {
switch (key) {
case 'and':
Object.assign(result, this.parseAndCondition(value, isNegated));
break;
case 'or':
orCondition = this.parseOrCondition(value, isNegated);
break;
case 'and': {
const andConditions = this.parseAndCondition(value, isNegated);

return andConditions.map((condition) => ({
...result,
...condition,
}));
Weiko marked this conversation as resolved.
Show resolved Hide resolved
}
case 'or': {
const orConditions = this.parseOrCondition(value, isNegated);

return orConditions.map((condition) => ({ ...result, ...condition }));
}
case 'not':
Object.assign(result, this.parse(value, !isNegated));
break;
Expand All @@ -47,10 +53,6 @@ export class GraphqlQueryFilterConditionParser {
}
}

if (orCondition) {
return orCondition.map((condition) => ({ ...result, ...condition }));
}

return result;
}

Expand Down Expand Up @@ -84,32 +86,28 @@ export class GraphqlQueryFilterConditionParser {
combineType: 'and' | 'or',
): FindOptionsWhere<ObjectLiteral>[] {
if (combineType === 'and') {
let result: FindOptionsWhere<ObjectLiteral>[] = [{}];

for (const condition of conditions) {
if (Array.isArray(condition)) {
const newResult: FindOptionsWhere<ObjectLiteral>[] = [];

for (const existingCondition of result) {
for (const orCondition of condition) {
newResult.push({
...existingCondition,
...orCondition,
});
}
return conditions.reduce<FindOptionsWhere<ObjectLiteral>[]>(
(acc, condition) => {
if (Array.isArray(condition)) {
return acc.flatMap((accCondition) =>
condition.map((subCondition) => ({
...accCondition,
...subCondition,
})),
);
}
result = newResult;
} else {
result = result.map((existingCondition) => ({
...existingCondition,

return acc.map((accCondition) => ({
...accCondition,
...condition,
}));
}
}

return result;
},
[{}],
);
}

return conditions.flat();
return conditions.flatMap((condition) =>
Array.isArray(condition) ? condition : [condition],
);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Injectable } from '@nestjs/common';

import { isDefined } from 'class-validator';
import graphqlFields from 'graphql-fields';
import { FindManyOptions, ObjectLiteral } from 'typeorm';

import {
Record as IRecord,
RecordFilter,
Expand All @@ -16,16 +12,8 @@ import {
FindOneResolverArgs,
} 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 {
GraphqlQueryRunnerException,
GraphqlQueryRunnerExceptionCode,
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
import { GraphqlQueryParser } from 'src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query.parser';
import { ObjectRecordsToGraphqlConnectionMapper } from 'src/engine/api/graphql/graphql-query-runner/orm-mappers/object-records-to-graphql-connection.mapper';
import { applyRangeFilter } from 'src/engine/api/graphql/graphql-query-runner/utils/apply-range-filter.util';
import { convertObjectMetadataToMap } from 'src/engine/api/graphql/graphql-query-runner/utils/convert-object-metadata-to-map.util';
import { decodeCursor } from 'src/engine/api/graphql/graphql-query-runner/utils/cursors.util';
import { GraphqlQueryFindManyResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-find-many-resolver.service';
import { GraphqlQueryFindOneResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-find-one-resolver.service';
import { LogExecutionTime } from 'src/engine/decorators/observability/log-execution-time.decorator';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';

Expand All @@ -43,48 +31,10 @@ export class GraphqlQueryRunnerService {
args: FindOneResolverArgs<Filter>,
options: WorkspaceQueryRunnerOptions,
): Promise<ObjectRecord | undefined> {
const { authContext, objectMetadataItem, info, objectMetadataCollection } =
options;
const repository = await this.getRepository(
authContext.workspace.id,
objectMetadataItem.nameSingular,
);
const objectMetadataMap = convertObjectMetadataToMap(
objectMetadataCollection,
);
const objectMetadata = this.getObjectMetadata(
objectMetadataMap,
objectMetadataItem.nameSingular,
);
const graphqlQueryParser = new GraphqlQueryParser(
objectMetadata.fields,
objectMetadataMap,
);

const { select, relations } = graphqlQueryParser.parseSelectedFields(
objectMetadataItem,
graphqlFields(info),
);
const where = graphqlQueryParser.parseFilter(args.filter ?? ({} as Filter));

const objectRecord = await repository.findOne({ where, select, relations });

if (!objectRecord) {
throw new GraphqlQueryRunnerException(
'Record not found',
GraphqlQueryRunnerExceptionCode.RECORD_NOT_FOUND,
);
}
const graphqlQueryFindOneResolverService =
new GraphqlQueryFindOneResolverService(this.twentyORMGlobalManager);
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider injecting this service instead of creating a new instance


const typeORMObjectRecordsParser =
new ObjectRecordsToGraphqlConnectionMapper(objectMetadataMap);

return typeORMObjectRecordsParser.processRecord(
objectRecord,
objectMetadataItem.nameSingular,
1,
1,
) as ObjectRecord;
return graphqlQueryFindOneResolverService.findOne(args, options);
}

@LogExecutionTime()
Expand All @@ -96,180 +46,9 @@ export class GraphqlQueryRunnerService {
args: FindManyResolverArgs<Filter, OrderBy>,
options: WorkspaceQueryRunnerOptions,
): Promise<IConnection<ObjectRecord>> {
const { authContext, objectMetadataItem, info, objectMetadataCollection } =
options;

this.validateArgsOrThrow(args);

const repository = await this.getRepository(
authContext.workspace.id,
objectMetadataItem.nameSingular,
);
const objectMetadataMap = convertObjectMetadataToMap(
objectMetadataCollection,
);
const objectMetadata = this.getObjectMetadata(
objectMetadataMap,
objectMetadataItem.nameSingular,
);
const graphqlQueryParser = new GraphqlQueryParser(
objectMetadata.fields,
objectMetadataMap,
);

const { select, relations } = graphqlQueryParser.parseSelectedFields(
objectMetadataItem,
graphqlFields(info),
);
const isForwardPagination = !isDefined(args.before);
const order = graphqlQueryParser.parseOrder(
args.orderBy ?? [],
isForwardPagination,
);
const where = graphqlQueryParser.parseFilter(
args.filter ?? ({} as Filter),
true,
);

const cursor = this.getCursor(args);
const limit = this.getLimit(args);

this.addOrderByColumnsToSelect(order, select);

const findOptions: FindManyOptions<ObjectLiteral> = {
where,
order,
select,
relations,
take: limit + 1,
};
const totalCount = await repository.count({ where });

if (cursor) {
applyRangeFilter(where, cursor, isForwardPagination);
}

const objectRecords = await repository.find(findOptions);
const { hasNextPage, hasPreviousPage } = this.getPaginationInfo(
objectRecords,
limit,
isForwardPagination,
);

if (objectRecords.length > limit) {
objectRecords.pop();
}

const typeORMObjectRecordsParser =
new ObjectRecordsToGraphqlConnectionMapper(objectMetadataMap);

return typeORMObjectRecordsParser.createConnection(
objectRecords as ObjectRecord[],
objectMetadataItem.nameSingular,
limit,
totalCount,
order,
hasNextPage,
hasPreviousPage,
);
}

private async getRepository(workspaceId: string, objectName: string) {
return this.twentyORMGlobalManager.getRepositoryForWorkspace(
workspaceId,
objectName,
);
}

private getObjectMetadata(
objectMetadataMap: Record<string, any>,
objectName: string,
) {
const objectMetadata = objectMetadataMap[objectName];

if (!objectMetadata) {
throw new GraphqlQueryRunnerException(
`Object metadata not found for ${objectName}`,
GraphqlQueryRunnerExceptionCode.OBJECT_METADATA_NOT_FOUND,
);
}

return objectMetadata;
}

private getCursor(
args: FindManyResolverArgs<any, any>,
): Record<string, any> | undefined {
if (args.after) return decodeCursor(args.after);
if (args.before) return decodeCursor(args.before);

return undefined;
}

private getLimit(args: FindManyResolverArgs<any, any>): number {
return args.first ?? args.last ?? QUERY_MAX_RECORDS;
}

private addOrderByColumnsToSelect(
order: Record<string, any>,
select: Record<string, boolean>,
) {
for (const column of Object.keys(order || {})) {
if (!select[column]) {
select[column] = true;
}
}
}

private getPaginationInfo(
objectRecords: any[],
limit: number,
isForwardPagination: boolean,
) {
const hasMoreRecords = objectRecords.length > limit;

return {
hasNextPage: isForwardPagination && hasMoreRecords,
hasPreviousPage: !isForwardPagination && hasMoreRecords,
};
}
const graphqlQueryFindManyResolverService =
new GraphqlQueryFindManyResolverService(this.twentyORMGlobalManager);
Comment on lines +49 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider injecting this service instead of creating a new instance


private validateArgsOrThrow(args: FindManyResolverArgs<any, any>) {
if (args.first && args.last) {
throw new GraphqlQueryRunnerException(
'Cannot provide both first and last',
GraphqlQueryRunnerExceptionCode.ARGS_CONFLICT,
);
}
if (args.before && args.after) {
throw new GraphqlQueryRunnerException(
'Cannot provide both before and after',
GraphqlQueryRunnerExceptionCode.ARGS_CONFLICT,
);
}
if (args.before && args.first) {
throw new GraphqlQueryRunnerException(
'Cannot provide both before and first',
GraphqlQueryRunnerExceptionCode.ARGS_CONFLICT,
);
}
if (args.after && args.last) {
throw new GraphqlQueryRunnerException(
'Cannot provide both after and last',
GraphqlQueryRunnerExceptionCode.ARGS_CONFLICT,
);
}
if (args.first !== undefined && args.first < 0) {
throw new GraphqlQueryRunnerException(
'First argument must be non-negative',
GraphqlQueryRunnerExceptionCode.INVALID_QUERY_INPUT,
);
}
if (args.last !== undefined && args.last < 0) {
throw new GraphqlQueryRunnerException(
'Last argument must be non-negative',
GraphqlQueryRunnerExceptionCode.INVALID_QUERY_INPUT,
);
}
return graphqlQueryFindManyResolverService.findMany(args, options);
}
}
Loading
Loading