Nestjs Transaction per request
async create(
ctx: RequestContext,
post: PostCreateInput,
groupName = 'Default Group',
): Promise<PostEntity> {
//Creating a Group
const defaultGroup = await this.connection
.getRepository(ctx, GroupEntity)
.save(new GroupEntity({ name: groupName, description: 'A default group for given post.' }));
const postEntity = new PostEntity({ ...(post as any), groupId: defaultGroup.id });
//Making a required field null, to throw an error
postEntity.testRequiredField = null;
//Error
const savedPost = await this.connection.getRepository(ctx, PostEntity).save(postEntity);
return savedPost;
}
@Transaction()
@Mutation(() => Post)
async createPostTransactional(
@Ctx() ctx: RequestContext,
@Args('postCreateInput') postCreateInput: PostCreateInput,
) {
return await this.postService.create(ctx, postCreateInput, 'Transactional Group Name');
}