doug-martin / nestjs-query

Easy CRUD for GraphQL.
https://doug-martin.github.io/nestjs-query
MIT License
822 stars 142 forks source link

Create Hooks #1547

Open kingrootid opened 2 years ago

kingrootid commented 2 years ago

Hello,

i was created hooks for CreateOne, CreateMany, UpdatedOne, UpdatedMany

but i want to make BeforeFindOne and BeforeFindMany using hooks

i dont now how to write in hooks @Injectable() export class findByUserHook<T extends CreatedBy> implements BeforeFindOneHook<UserContext> { run( instance: FindOneArgsType, context: UserContext, ): Promise<FindOneArgsType>; async run( instance: FindOneArgsType, context: UserContext, ): Promise<FindOneArgsType> { instance.createdBy = { eq: context.req.user.id }; // error in here }

TriPSs commented 2 years ago

You can also overwrite the service and then overwrite the query methods

kingrootid commented 1 year ago

@BeforeCreateOne( (input: CreateOneInputType<CytologyDto>, context: UserContext) => { console.log('inputan', input); return input; }, ) @BeforeUpdateOne( (input: UpdateOneInputType<CytologyDto>, context: UserContext) => { console.log('inputan', input); return input; }, ) ref: https://doug-martin.github.io/nestjs-query/docs/graphql/hooks/#hook-fnction and the console not printed

TriPSs commented 1 year ago

Could you please share your whole DTO? And put it between ``` for better reading.

kingrootid commented 1 year ago
import { TransactionDto } from '../transaction/transaction.dto';
import { UserDto } from '../../master/user/user.dto';
import { TransactionBaseDto } from '@/src/config/transaction_base.dto';

import {
  BeforeCreateOne,
  BeforeDeleteMany,
  BeforeDeleteOne,
  BeforeUpdateOne,
  CreateOneInputType,
  FilterableField,
  FilterableRelation,
  IDField,
  PagingStrategies,
  QueryOptions,
  UnPagedRelation,
  UpdateOneInputType,
} from '@nestjs-query/query-graphql';
import {
  Field,
  Float,
  GraphQLISODateTime,
  ID,
  ObjectType,
  PartialType,
} from '@nestjs/graphql';
import { IsArray, IsBoolean, isNumber, IsNumber } from 'class-validator';
import { DeleteByAdminHook } from '../../../common/hooks/admin_hook';
import { CustomerDto } from '../../master/customer/customer.dto';
import { LabDto } from '../../master/lab/lab.dto';
import { PatientDto } from '../../master/patient/patient.dto';
import { CreateLogHook } from '../../../common/hooks/log_hook';
import { UserContext } from '../../auth/auth.types';

@ObjectType('cytology')
@BeforeCreateOne(
  (input: CreateOneInputType<CytologyDto>, context: UserContext) => {
    console.log('inputan', input);
    return input;
  },
)
@BeforeUpdateOne(
  (input: UpdateOneInputType<CytologyDto>, context: UserContext) => {
    console.log('inputan', input);
    return input;
  },
)
@BeforeDeleteOne(DeleteByAdminHook)
@BeforeDeleteMany(DeleteByAdminHook)
@QueryOptions({
  pagingStrategy: PagingStrategies.OFFSET,
  enableTotalCount: true,
})
@FilterableRelation('transaction', () => TransactionDto, {
  disableRemove: true,
  disableUpdate: true,
})
@FilterableRelation('user1stApprover', () => UserDto, {
  disableRemove: true,
  disableUpdate: true,
  nullable: true,
})
@FilterableRelation('user2ndApprover', () => UserDto, {
  disableRemove: true,
  disableUpdate: true,
  nullable: true,
})
export class CytologyDto {
  @FilterableField({ nullable: true })
  approval_status: string;

  @FilterableField({ nullable: true })
  user_1st_approver_id: string;

  @FilterableField({ nullable: true })
  user_2nd_approver_id: string;

  @IDField(() => ID)
  id: string;

  @FilterableField({ name: 'transaction_id' })
  transaction_id: string;

  // REQUEST FORM
  @FilterableField({ nullable: true })
  rf_notes: string;
  @FilterableField({ nullable: true })
  rf_result_reporting_method: string;
  @FilterableField({ nullable: true })
  rf_result_reporting_email: string;

  // SAMPLE INFORMATION
  @FilterableField({ nullable: true })
  si_specimen_location: string;
  @FilterableField({ nullable: true })
  si_hormon_status: string;
  @FilterableField({ nullable: true })
  si_hormon_status_others: string;
  @FilterableField({ nullable: true })
  si_contraception: string;
  @FilterableField({ nullable: true })
  si_gynaecology_status: string;
  @FilterableField({ nullable: true })
  si_gynaecology_status_others: string;
  @FilterableField({ nullable: true })
  si_fixation: string;
  @FilterableField({ nullable: true })
  si_fixation_others: string;
  @FilterableField({ nullable: true })

  // TEST REQUEST
  // tr_gc_fnab: boolean;
  // @FilterableField({ nullable: true })
  // tr_gc_tbna: boolean;
  // @FilterableField({ nullable: true })
  // tr_gc_ttna: boolean;
  // @FilterableField({ nullable: true })
  // tr_gc_bal: boolean;
  // @FilterableField({ nullable: true })
  // tr_gc_bronchial_brushing: boolean;
  // @FilterableField({ nullable: true })
  // tr_gc_bronchial_washing: boolean;
  // @FilterableField({ nullable: true })
  // tr_gc_body_fluid: string;
  // @FilterableField({ nullable: true })
  // tr_gc_body_fluid_others: string;
  // @FilterableField({ nullable: true })
  // tr_gc_sputum_1x: boolean;
  // @FilterableField({ nullable: true })
  // tr_gc_sputum_3x: boolean;
  // @FilterableField({ nullable: true })
  // tr_gc_urine: boolean;
  // @FilterableField({ nullable: true })
  // tr_gc_tb_suspected_review: boolean;
  // @FilterableField({ nullable: true })
  // tr_as_add_unstained_slide: number;
  // @FilterableField({ nullable: true })
  // tr_as_add_papanicolaou_slide: number;
  // @FilterableField({ nullable: true })
  // tr_as_add_giemsa_slide: number;
  // @FilterableField({ nullable: true })
  // tr_as_add_h_n_e_slide: number;
  // @FilterableField({ nullable: true })
  // tr_as_produce_a_cell_block: boolean;
  // @FilterableField({ nullable: true })
  // tr_as_processing_cytology_pa_only_without_review: boolean;
  // @FilterableField({ nullable: true })
  // tr_as_pathology_review_or_second_opinion_per_2_slides: boolean;
  // @FilterableField({ nullable: true })
  // tr_as_slide: number;
  // @FilterableField({ nullable: true })
  // tr_as_ffpe: number;
  // @FilterableField({ nullable: true })
  // tr_as_conventional_pap_smear_review: boolean;
  // @FilterableField({ nullable: true })

  // // CYTOLOGY INFORMATION
  ci_clinical_diagnosis: string;
  @FilterableField({ nullable: true })
  ci_main_symptom_n_desc_of_the_disease: string;
  @FilterableField({ nullable: true })
  ci_prev_cytology_or_histopathology_testing_result: string;

  // TERM AND CONDITIONS
  // @FilterableField({ nullable: true })
  @FilterableField({ nullable: true })
  tnc: string;
  @FilterableField({ name: 'other_order_or_comment', nullable: true })
  other_order_or_comment: string;

  @FilterableField(() => GraphQLISODateTime)
  createdAt: Date;

  @FilterableField(() => GraphQLISODateTime)
  updatedAt: Date;
}
TriPSs commented 1 year ago

What version are you using?

FYI this repo is no longer maintained (See #1538) so you could try to use my fork to see if that one works for you.

kingrootid commented 1 year ago

"@nestjs-query/query-graphql": "^0.30.0",

TriPSs commented 1 year ago

When do you expect it to be called? With one of the methods this lib creates for you for the DTO? Or did you create your own resolver?

Could you also share the module file?