Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/core/src/services/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export interface QueryService<DTO, C = DeepPartial<DTO>, U = DeepPartial<DTO>> {
* @param item - the record to create.
* @returns the created record.
*/
createOne(item: C): Promise<DTO>
createOne(item: C, opts?: UpdateOneOptions<DTO>): Promise<DTO>

/**
* Creates a multiple record.
Expand Down
5 changes: 2 additions & 3 deletions packages/query-graphql/src/resolvers/create.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,10 @@ export const Creatable =
@AuthorizerFilter({
operationGroup: OperationGroup.CREATE,
many: false
}) // eslint-disable-next-line @typescript-eslint/no-unused-vars
})
authorizeFilter?: Filter<DTO>
): Promise<DTO> {
// Ignore `authorizeFilter` for now but give users the ability to throw an UnauthorizedException
const created = await this.service.createOne(input.input.input)
const created = await this.service.createOne(input.input.input, { filter: authorizeFilter ?? {} })
if (enableOneSubscriptions) {
await this.publishCreatedEvent(created, authorizeFilter)
}
Expand Down
9 changes: 8 additions & 1 deletion packages/query-typeorm/src/services/typeorm-query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AggregateOptions,
AggregateQuery,
AggregateResponse,
applyFilter,
Class,
CountOptions,
DeepPartial,
Expand Down Expand Up @@ -191,9 +192,15 @@ export class TypeOrmQueryService<Entity>
* ```
* @param record - The entity to create.
*/
public async createOne(record: DeepPartial<Entity>): Promise<Entity> {
public async createOne(record: DeepPartial<Entity>, opts?: UpdateOneOptions<Entity>): Promise<Entity> {
const entity = await this.ensureIsEntityAndDoesNotExist(record)

const passesFilter = applyFilter(entity, opts.filter)

if (!passesFilter) {
throw new Error('Entity does not meet creation constraints')
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.repo.save(entity)
Expand Down