customer.resolver.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Mutation, Query, ResolveProperty, Resolver } from '@nestjs/graphql';
  2. import { PaginatedList } from '../../../../shared/shared-types';
  3. import { Address } from '../../entity/address/address.entity';
  4. import { Customer } from '../../entity/customer/customer.entity';
  5. import { CustomerService } from '../../service/customer.service';
  6. import { ApplyIdCodec } from '../common/apply-id-codec-decorator';
  7. @Resolver('Customer')
  8. export class CustomerResolver {
  9. constructor(private customerService: CustomerService) {}
  10. @Query('customers')
  11. @ApplyIdCodec()
  12. async customers(obj, args): Promise<PaginatedList<Customer>> {
  13. return this.customerService.findAll(args.take, args.skip);
  14. }
  15. @Query('customer')
  16. @ApplyIdCodec()
  17. async customer(obj, args): Promise<Customer | undefined> {
  18. return this.customerService.findOne(args.id);
  19. }
  20. @ResolveProperty('addresses')
  21. @ApplyIdCodec()
  22. async addresses(customer: Customer): Promise<Address[]> {
  23. return this.customerService.findAddressesByCustomerId(customer.id);
  24. }
  25. @Mutation()
  26. @ApplyIdCodec()
  27. async createCustomer(_, args): Promise<Customer> {
  28. const { input, password } = args;
  29. return this.customerService.create(input, password);
  30. }
  31. @Mutation()
  32. @ApplyIdCodec()
  33. async createCustomerAddress(_, args): Promise<Address> {
  34. const { customerId, input } = args;
  35. return this.customerService.createAddress(customerId, input);
  36. }
  37. }