From c71e87ec5084261ddfe5bd45ca0e6e9504ea1e06 Mon Sep 17 00:00:00 2001 From: Michael Schramm Date: Mon, 14 Mar 2022 16:40:50 +0100 Subject: [PATCH] fix build --- src/dto/form/form.field.model.ts | 15 ----- src/resolver/form/form.field.resolver.ts | 73 ++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 src/resolver/form/form.field.resolver.ts diff --git a/src/dto/form/form.field.model.ts b/src/dto/form/form.field.model.ts index f3c7a88..85060e3 100644 --- a/src/dto/form/form.field.model.ts +++ b/src/dto/form/form.field.model.ts @@ -1,8 +1,5 @@ import { Field, ID, ObjectType } from '@nestjs/graphql' import { FormFieldEntity } from '../../entity/form.field.entity' -import { FormFieldLogicModel } from './form.field.logic.model' -import { FormFieldOptionModel } from './form.field.option.model' -import { FormFieldRatingModel } from './form.field.rating.model' @ObjectType('FormField') export class FormFieldModel { @@ -32,15 +29,6 @@ export class FormFieldModel { @Field({ nullable: true }) readonly defaultValue: string - @Field(() => [FormFieldOptionModel]) - readonly options: FormFieldOptionModel[] - - @Field(() => [FormFieldLogicModel]) - readonly logic: FormFieldLogicModel[] - - @Field(() => FormFieldRatingModel, { nullable: true }) - readonly rating: FormFieldRatingModel - constructor(id: string, document: FormFieldEntity) { this._id = document.id this.id = id @@ -51,8 +39,5 @@ export class FormFieldModel { this.description = document.description this.required = document.required this.defaultValue = document.defaultValue - this.options = document.options?.map(option => new FormFieldOptionModel(option)) || [] - this.logic = document.logic?.map(logic => new FormFieldLogicModel(logic)) || [] - this.rating = document.rating ? new FormFieldRatingModel(document.rating) : null } } diff --git a/src/resolver/form/form.field.resolver.ts b/src/resolver/form/form.field.resolver.ts new file mode 100644 index 0000000..369d6fc --- /dev/null +++ b/src/resolver/form/form.field.resolver.ts @@ -0,0 +1,73 @@ +import { Context, Parent, ResolveField, Resolver } from '@nestjs/graphql' +import { FormFieldLogicModel } from '../../dto/form/form.field.logic.model' +import { FormFieldModel } from '../../dto/form/form.field.model' +import { FormFieldOptionModel } from '../../dto/form/form.field.option.model' +import { FormFieldRatingModel } from '../../dto/form/form.field.rating.model' +import { FormFieldEntity } from '../../entity/form.field.entity' +import { IdService } from '../../service/id.service' +import { ContextCache } from '../context.cache' + +@Resolver(FormFieldModel) +export class FormFieldResolver { + constructor( + private readonly idService: IdService, + ) { + } + + @ResolveField(() => [FormFieldOptionModel]) + async options( + @Parent() parent: FormFieldModel, + @Context('cache') cache: ContextCache, + ): Promise { + const field = await cache.get(cache.getCacheKey( + FormFieldEntity.name, + parent._id + )) + + if (!field.options) { + return [] + } + + return field.options.map(option => new FormFieldOptionModel( + this.idService.encode(option.id), + option, + )) + } + + @ResolveField(() => [FormFieldLogicModel]) + async logic( + @Parent() parent: FormFieldModel, + @Context('cache') cache: ContextCache, + ): Promise { + const field = await cache.get(cache.getCacheKey( + FormFieldEntity.name, + parent._id + )) + + if (!field.logic) { + return [] + } + + return field.logic.map(logic => new FormFieldLogicModel( + this.idService.encode(logic.id), + logic, + )) + } + + @ResolveField(() => FormFieldRatingModel, { nullable: true }) + async rating( + @Parent() parent: FormFieldModel, + @Context('cache') cache: ContextCache, + ): Promise { + const field = await cache.get(cache.getCacheKey( + FormFieldEntity.name, + parent._id + )) + + if (!field.rating) { + return null + } + + return new FormFieldRatingModel(field.rating) + } +}