chore: drop findMany and findUnique resolvers for custom objects (#1897)
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
This commit is contained in:
@ -1,35 +0,0 @@
|
|||||||
import { ArgsType, Field, Int } from '@nestjs/graphql';
|
|
||||||
|
|
||||||
import GraphQLJSON from 'graphql-type-json';
|
|
||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
|
||||||
|
|
||||||
import { ConnectionArgs } from 'src/utils/pagination';
|
|
||||||
|
|
||||||
import { UniversalEntityInput } from './universal-entity.input';
|
|
||||||
import { UniversalEntityOrderByRelationInput } from './universal-entity-order-by-relation.input';
|
|
||||||
|
|
||||||
@ArgsType()
|
|
||||||
export class FindManyUniversalArgs extends ConnectionArgs {
|
|
||||||
@Field(() => String)
|
|
||||||
@IsNotEmpty()
|
|
||||||
@IsString()
|
|
||||||
entity: string;
|
|
||||||
|
|
||||||
@Field(() => UniversalEntityInput, { nullable: true })
|
|
||||||
where?: UniversalEntityInput;
|
|
||||||
|
|
||||||
@Field(() => UniversalEntityOrderByRelationInput, { nullable: true })
|
|
||||||
orderBy?: UniversalEntityOrderByRelationInput;
|
|
||||||
|
|
||||||
@Field(() => GraphQLJSON, { nullable: true })
|
|
||||||
cursor?: UniversalEntityInput;
|
|
||||||
|
|
||||||
@Field(() => Int, { nullable: true })
|
|
||||||
take?: number;
|
|
||||||
|
|
||||||
@Field(() => Int, { nullable: true })
|
|
||||||
skip?: number;
|
|
||||||
|
|
||||||
@Field(() => [String], { nullable: true })
|
|
||||||
distinct?: Array<string>;
|
|
||||||
}
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
import { ArgsType, Field } from '@nestjs/graphql';
|
|
||||||
|
|
||||||
import { BaseUniversalArgs } from './base-universal.args';
|
|
||||||
import { UniversalEntityInput } from './universal-entity.input';
|
|
||||||
|
|
||||||
@ArgsType()
|
|
||||||
export class FindUniqueUniversalArgs extends BaseUniversalArgs {
|
|
||||||
@Field(() => UniversalEntityInput, { nullable: true })
|
|
||||||
where?: UniversalEntityInput;
|
|
||||||
}
|
|
||||||
@ -2,11 +2,10 @@ import { Module } from '@nestjs/common';
|
|||||||
|
|
||||||
import { DataSourceModule } from 'src/metadata/data-source/data-source.module';
|
import { DataSourceModule } from 'src/metadata/data-source/data-source.module';
|
||||||
|
|
||||||
import { UniversalService } from './universal.service';
|
|
||||||
import { UniversalResolver } from './universal.resolver';
|
import { UniversalResolver } from './universal.resolver';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [DataSourceModule],
|
imports: [DataSourceModule],
|
||||||
providers: [UniversalService, UniversalResolver],
|
providers: [UniversalResolver],
|
||||||
})
|
})
|
||||||
export class UniversalModule {}
|
export class UniversalModule {}
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { Test, TestingModule } from '@nestjs/testing';
|
|||||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||||
|
|
||||||
import { UniversalResolver } from './universal.resolver';
|
import { UniversalResolver } from './universal.resolver';
|
||||||
import { UniversalService } from './universal.service';
|
|
||||||
|
|
||||||
describe('UniversalResolver', () => {
|
describe('UniversalResolver', () => {
|
||||||
let resolver: UniversalResolver;
|
let resolver: UniversalResolver;
|
||||||
@ -12,10 +11,6 @@ describe('UniversalResolver', () => {
|
|||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [
|
providers: [
|
||||||
UniversalResolver,
|
UniversalResolver,
|
||||||
{
|
|
||||||
provide: UniversalService,
|
|
||||||
useValue: {},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
provide: EnvironmentService,
|
provide: EnvironmentService,
|
||||||
useValue: {},
|
useValue: {},
|
||||||
|
|||||||
@ -1,49 +1,15 @@
|
|||||||
import { Args, Query, Resolver } from '@nestjs/graphql';
|
import { Query, Resolver } from '@nestjs/graphql';
|
||||||
import { ForbiddenException, UseGuards } from '@nestjs/common';
|
import { ForbiddenException, UseGuards } from '@nestjs/common';
|
||||||
|
|
||||||
import { Workspace } from '@prisma/client';
|
|
||||||
|
|
||||||
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
||||||
import { AuthWorkspace } from 'src/decorators/auth-workspace.decorator';
|
|
||||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||||
|
|
||||||
import { UniversalEntity, PaginatedUniversalEntity } from './universal.entity';
|
import { UniversalEntity } from './universal.entity';
|
||||||
import { UniversalService } from './universal.service';
|
|
||||||
|
|
||||||
import { FindManyUniversalArgs } from './args/find-many-universal.args';
|
|
||||||
import { FindUniqueUniversalArgs } from './args/find-unique-universal.args';
|
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Resolver(() => UniversalEntity)
|
@Resolver(() => UniversalEntity)
|
||||||
export class UniversalResolver {
|
export class UniversalResolver {
|
||||||
constructor(
|
constructor(private readonly environmentService: EnvironmentService) {}
|
||||||
private readonly customService: UniversalService,
|
|
||||||
private readonly environmentService: EnvironmentService,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
@Query(() => PaginatedUniversalEntity)
|
|
||||||
findMany(
|
|
||||||
@Args() args: FindManyUniversalArgs,
|
|
||||||
@AuthWorkspace() workspace: Workspace,
|
|
||||||
): Promise<PaginatedUniversalEntity> {
|
|
||||||
if (!this.environmentService.isFlexibleBackendEnabled()) {
|
|
||||||
throw new ForbiddenException();
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.customService.findManyUniversal(args, workspace);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Query(() => UniversalEntity)
|
|
||||||
findUnique(
|
|
||||||
@Args() args: FindUniqueUniversalArgs,
|
|
||||||
@AuthWorkspace() workspace: Workspace,
|
|
||||||
): Promise<UniversalEntity | undefined> {
|
|
||||||
if (!this.environmentService.isFlexibleBackendEnabled()) {
|
|
||||||
throw new ForbiddenException();
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.customService.findUniqueUniversal(args, workspace);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Query(() => UniversalEntity)
|
@Query(() => UniversalEntity)
|
||||||
updateOneCustom(): UniversalEntity {
|
updateOneCustom(): UniversalEntity {
|
||||||
|
|||||||
@ -1,32 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
|
|
||||||
import { DataSourceService } from 'src/metadata/data-source/data-source.service';
|
|
||||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
|
||||||
|
|
||||||
import { UniversalService } from './universal.service';
|
|
||||||
|
|
||||||
describe('UniversalService', () => {
|
|
||||||
let service: UniversalService;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [
|
|
||||||
UniversalService,
|
|
||||||
{
|
|
||||||
provide: DataSourceService,
|
|
||||||
useValue: {},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: EnvironmentService,
|
|
||||||
useValue: {},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<UniversalService>(UniversalService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -1,100 +0,0 @@
|
|||||||
import { Injectable, InternalServerErrorException } from '@nestjs/common';
|
|
||||||
|
|
||||||
import { Workspace } from '@prisma/client';
|
|
||||||
|
|
||||||
import { DataSourceService } from 'src/metadata/data-source/data-source.service';
|
|
||||||
import { findManyCursorConnection } from 'src/utils/pagination';
|
|
||||||
|
|
||||||
import { UniversalEntity, PaginatedUniversalEntity } from './universal.entity';
|
|
||||||
import {
|
|
||||||
getRawTypeORMOrderByClause,
|
|
||||||
getRawTypeORMWhereClause,
|
|
||||||
} from './universal.util';
|
|
||||||
|
|
||||||
import { FindManyUniversalArgs } from './args/find-many-universal.args';
|
|
||||||
import { FindUniqueUniversalArgs } from './args/find-unique-universal.args';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class UniversalService {
|
|
||||||
constructor(private readonly dataSourceService: DataSourceService) {}
|
|
||||||
|
|
||||||
async findManyUniversal(
|
|
||||||
args: FindManyUniversalArgs,
|
|
||||||
workspace: Workspace,
|
|
||||||
): Promise<PaginatedUniversalEntity> {
|
|
||||||
await this.dataSourceService.createWorkspaceSchema(workspace.id);
|
|
||||||
|
|
||||||
const workspaceDataSource =
|
|
||||||
await this.dataSourceService.connectToWorkspaceDataSource(workspace.id);
|
|
||||||
|
|
||||||
let query = workspaceDataSource
|
|
||||||
?.createQueryBuilder()
|
|
||||||
.select()
|
|
||||||
.from(args.entity, args.entity);
|
|
||||||
|
|
||||||
if (!query) {
|
|
||||||
throw new InternalServerErrorException();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (query && args.where) {
|
|
||||||
const { where, parameters } = getRawTypeORMWhereClause(
|
|
||||||
args.entity,
|
|
||||||
args.where,
|
|
||||||
);
|
|
||||||
|
|
||||||
query = query.where(where, parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (query && args.orderBy) {
|
|
||||||
const orderBy = getRawTypeORMOrderByClause(args.entity, args.orderBy);
|
|
||||||
|
|
||||||
query = query.orderBy(orderBy);
|
|
||||||
}
|
|
||||||
|
|
||||||
return findManyCursorConnection(query, args, {
|
|
||||||
recordToEdge({ id, createdAt, updatedAt, ...data }) {
|
|
||||||
return {
|
|
||||||
node: {
|
|
||||||
id,
|
|
||||||
data,
|
|
||||||
createdAt,
|
|
||||||
updatedAt,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async findUniqueUniversal(
|
|
||||||
args: FindUniqueUniversalArgs,
|
|
||||||
workspace: Workspace,
|
|
||||||
): Promise<UniversalEntity | undefined> {
|
|
||||||
await this.dataSourceService.createWorkspaceSchema(workspace.id);
|
|
||||||
|
|
||||||
const workspaceDataSource =
|
|
||||||
await this.dataSourceService.connectToWorkspaceDataSource(workspace.id);
|
|
||||||
|
|
||||||
let query = workspaceDataSource
|
|
||||||
?.createQueryBuilder()
|
|
||||||
.select()
|
|
||||||
.from(args.entity, args.entity);
|
|
||||||
|
|
||||||
if (query && args.where) {
|
|
||||||
const { where, parameters } = getRawTypeORMWhereClause(
|
|
||||||
args.entity,
|
|
||||||
args.where,
|
|
||||||
);
|
|
||||||
|
|
||||||
query = query.where(where, parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { id, createdAt, updatedAt, ...data } = await query?.getRawOne();
|
|
||||||
|
|
||||||
return {
|
|
||||||
id,
|
|
||||||
data,
|
|
||||||
createdAt,
|
|
||||||
updatedAt,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user