From 39755ddf43ba75e04db9049ed20311ab1a09859d Mon Sep 17 00:00:00 2001 From: Yubing325 <35515298+Yubing325@users.noreply.github.com> Date: Thu, 4 Jun 2020 18:46:31 -0500 Subject: [PATCH] add mutation --- .../services/IChangeMerchantNameCommand.ts | 4 ++ src/app/data/services/IMerchant.ts | 5 ++ src/app/data/services/merchant.service.ts | 61 +++++++++++++------ 3 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 src/app/data/services/IChangeMerchantNameCommand.ts create mode 100644 src/app/data/services/IMerchant.ts diff --git a/src/app/data/services/IChangeMerchantNameCommand.ts b/src/app/data/services/IChangeMerchantNameCommand.ts new file mode 100644 index 0000000..7e93063 --- /dev/null +++ b/src/app/data/services/IChangeMerchantNameCommand.ts @@ -0,0 +1,4 @@ +export interface IChangeMerchantNameCommand { + merchantId: string; + newName: string; +} diff --git a/src/app/data/services/IMerchant.ts b/src/app/data/services/IMerchant.ts new file mode 100644 index 0000000..98cd34f --- /dev/null +++ b/src/app/data/services/IMerchant.ts @@ -0,0 +1,5 @@ +export interface IMerchant { + id: string; + name: string; + address: string; +} diff --git a/src/app/data/services/merchant.service.ts b/src/app/data/services/merchant.service.ts index cd9e687..0f8234f 100644 --- a/src/app/data/services/merchant.service.ts +++ b/src/app/data/services/merchant.service.ts @@ -1,36 +1,63 @@ import { Injectable } from '@angular/core'; import { GraphQLDataSourceService } from '@poweredsoft/ngx-data-apollo'; import { IDataSource, DataSource } from '@poweredsoft/data'; - -export interface IMerchant{ - id: string, - name: string, - address: string -} +import { Apollo } from 'apollo-angular'; +import { gql } from 'graphql-tag'; +import { of } from 'rxjs'; +import { IChangeMerchantNameCommand } from './IChangeMerchantNameCommand'; +import { IMerchant } from './IMerchant'; @Injectable({ - providedIn: 'root' + providedIn: 'root', }) export class MerchantService { + constructor( + private dataSourceGenericService: GraphQLDataSourceService, + private apollo: Apollo + ) {} - constructor(private dataSourceGenericService: GraphQLDataSourceService) { - - dataSourceGenericService - } - - createMerchantDataSource() : IDataSource { - const optionsBuilder = this.dataSourceGenericService.createDataSourceOptionsBuilder( + createMerchantDataSource(): IDataSource { + const builder = this.dataSourceGenericService.createDataSourceOptionsBuilder< + IMerchant, + string + >( 'merchants', 'GraphQLAdvanceQueryOfMerchantInput', 'id, name, address', (model) => model.id, { page: 1, - pageSize: 4 - }, + pageSize: 4, + }, true ); - const options = optionsBuilder.create(); + + builder.addMutation( + 'changeMerchantName', //<-- command name + 'changeMerchantName', //<-- graph ql mutation name + + // implementation of the command. + command => { + return this.apollo.use('command').mutate({ + mutation: gql` + mutation executeChangeName($command: changeMerchantNameInput) { + changeMerchantName(params: $command) + } + `, + variables: { + command: command, + }, + }); + }, + + // viewModel -> transform to the form model for that command -> IChangeMerchantName + e => of({ + merchantId: e.model.id, + newName: e.model.name, + }) + ); + + const options = builder.create(); return new DataSource(options); } }