feat: add custom object create and update (#1869)
This commit is contained in:
@ -6,12 +6,20 @@ import {
|
||||
|
||||
import { GraphQLResolveInfo } from 'graphql';
|
||||
import graphqlFields from 'graphql-fields';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import { DataSourceService } from 'src/metadata/data-source/data-source.service';
|
||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||
import { pascalCase } from 'src/utils/pascal-case';
|
||||
|
||||
import { convertFieldsToGraphQL } from './entity-resolver.util';
|
||||
|
||||
function stringify(obj: any) {
|
||||
const jsonString = JSON.stringify(obj);
|
||||
const jsonWithoutQuotes = jsonString.replace(/"(\w+)"\s*:/g, '$1:');
|
||||
return jsonWithoutQuotes;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class EntityResolverService {
|
||||
constructor(
|
||||
@ -49,7 +57,7 @@ export class EntityResolverService {
|
||||
const graphqlResult = await workspaceDataSource?.query(`
|
||||
SELECT graphql.resolve($$
|
||||
{
|
||||
${entityName}Collection: ${tableName}Collection {
|
||||
findAll${pascalCase(entityName)}: ${tableName}Collection {
|
||||
${graphqlQuery}
|
||||
}
|
||||
}
|
||||
@ -57,7 +65,7 @@ export class EntityResolverService {
|
||||
`);
|
||||
|
||||
const result =
|
||||
graphqlResult?.[0]?.resolve?.data?.[`${entityName}Collection`];
|
||||
graphqlResult?.[0]?.resolve?.data?.[`findAll${pascalCase(entityName)}`];
|
||||
|
||||
if (!result) {
|
||||
throw new BadRequestException('Malformed result from GraphQL query');
|
||||
@ -93,7 +101,9 @@ export class EntityResolverService {
|
||||
const graphqlResult = await workspaceDataSource?.query(`
|
||||
SELECT graphql.resolve($$
|
||||
{
|
||||
${entityName}Collection: : ${tableName}Collection(filter: { id: { eq: "${args.id}" } }) {
|
||||
findOne${pascalCase(
|
||||
entityName,
|
||||
)}: ${tableName}Collection(filter: { id: { eq: "${args.id}" } }) {
|
||||
${graphqlQuery}
|
||||
}
|
||||
}
|
||||
@ -101,10 +111,168 @@ export class EntityResolverService {
|
||||
`);
|
||||
|
||||
const result =
|
||||
graphqlResult?.[0]?.resolve?.data?.[`${entityName}Collection`];
|
||||
graphqlResult?.[0]?.resolve?.data?.[`findOne${pascalCase(entityName)}`];
|
||||
|
||||
if (!result) {
|
||||
return null;
|
||||
throw new BadRequestException('Malformed result from GraphQL query');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async createOne(
|
||||
entityName: string,
|
||||
tableName: string,
|
||||
args: { data: any },
|
||||
workspaceId: string,
|
||||
info: GraphQLResolveInfo,
|
||||
fieldAliases: Record<string, string>,
|
||||
) {
|
||||
if (!this.environmentService.isFlexibleBackendEnabled()) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
const workspaceDataSource =
|
||||
await this.dataSourceService.connectToWorkspaceDataSource(workspaceId);
|
||||
|
||||
const graphqlQuery = await this.prepareGrapQLQuery(
|
||||
workspaceId,
|
||||
info,
|
||||
fieldAliases,
|
||||
);
|
||||
|
||||
await workspaceDataSource?.query(`
|
||||
SET search_path TO ${this.dataSourceService.getSchemaName(workspaceId)};
|
||||
`);
|
||||
const graphqlResult = await workspaceDataSource?.query(`
|
||||
SELECT graphql.resolve($$
|
||||
mutation {
|
||||
createOne${pascalCase(
|
||||
entityName,
|
||||
)}: insertInto${tableName}Collection(objects: [${stringify({
|
||||
id: uuidv4(),
|
||||
...args.data,
|
||||
})}]) {
|
||||
affectedCount
|
||||
records {
|
||||
${graphqlQuery}
|
||||
}
|
||||
}
|
||||
}
|
||||
$$);
|
||||
`);
|
||||
|
||||
const result =
|
||||
graphqlResult?.[0]?.resolve?.data?.[`createOne${pascalCase(entityName)}`]
|
||||
?.records[0];
|
||||
|
||||
if (!result) {
|
||||
throw new BadRequestException('Malformed result from GraphQL query');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async createMany(
|
||||
entityName: string,
|
||||
tableName: string,
|
||||
args: { data: any[] },
|
||||
workspaceId: string,
|
||||
info: GraphQLResolveInfo,
|
||||
fieldAliases: Record<string, string>,
|
||||
) {
|
||||
if (!this.environmentService.isFlexibleBackendEnabled()) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
const workspaceDataSource =
|
||||
await this.dataSourceService.connectToWorkspaceDataSource(workspaceId);
|
||||
|
||||
const graphqlQuery = await this.prepareGrapQLQuery(
|
||||
workspaceId,
|
||||
info,
|
||||
fieldAliases,
|
||||
);
|
||||
|
||||
await workspaceDataSource?.query(`
|
||||
SET search_path TO ${this.dataSourceService.getSchemaName(workspaceId)};
|
||||
`);
|
||||
const graphqlResult = await workspaceDataSource?.query(`
|
||||
SELECT graphql.resolve($$
|
||||
mutation {
|
||||
insertInto${entityName}Collection: insertInto${tableName}Collection(objects: ${stringify(
|
||||
args.data.map((datum) => ({
|
||||
id: uuidv4(),
|
||||
...datum,
|
||||
})),
|
||||
)}) {
|
||||
affectedCount
|
||||
records {
|
||||
${graphqlQuery}
|
||||
}
|
||||
}
|
||||
}
|
||||
$$);
|
||||
`);
|
||||
|
||||
const result =
|
||||
graphqlResult?.[0]?.resolve?.data?.[`insertInto${entityName}Collection`]
|
||||
?.records;
|
||||
|
||||
if (!result) {
|
||||
throw new BadRequestException('Malformed result from GraphQL query');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async updateOne(
|
||||
entityName: string,
|
||||
tableName: string,
|
||||
args: { id: string; data: any },
|
||||
workspaceId: string,
|
||||
info: GraphQLResolveInfo,
|
||||
fieldAliases: Record<string, string>,
|
||||
) {
|
||||
if (!this.environmentService.isFlexibleBackendEnabled()) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
const workspaceDataSource =
|
||||
await this.dataSourceService.connectToWorkspaceDataSource(workspaceId);
|
||||
|
||||
const graphqlQuery = await this.prepareGrapQLQuery(
|
||||
workspaceId,
|
||||
info,
|
||||
fieldAliases,
|
||||
);
|
||||
|
||||
await workspaceDataSource?.query(`
|
||||
SET search_path TO ${this.dataSourceService.getSchemaName(workspaceId)};
|
||||
`);
|
||||
const graphqlResult = await workspaceDataSource?.query(`
|
||||
SELECT graphql.resolve($$
|
||||
mutation {
|
||||
updateOne${pascalCase(
|
||||
entityName,
|
||||
)}: update${tableName}Collection(set: ${stringify(
|
||||
args.data,
|
||||
)}, filter: { id: { eq: "${args.id}" } }) {
|
||||
affectedCount
|
||||
records {
|
||||
${graphqlQuery}
|
||||
}
|
||||
}
|
||||
}
|
||||
$$);
|
||||
`);
|
||||
|
||||
const result =
|
||||
graphqlResult?.[0]?.resolve?.data?.[`updateOne${pascalCase(entityName)}`]
|
||||
?.records[0];
|
||||
|
||||
if (!result) {
|
||||
throw new BadRequestException('Malformed result from GraphQL query');
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user