add mutation

This commit is contained in:
Yubing325 2020-06-04 18:46:31 -05:00
parent c1edf10fa9
commit 39755ddf43
3 changed files with 53 additions and 17 deletions

View File

@ -0,0 +1,4 @@
export interface IChangeMerchantNameCommand {
merchantId: string;
newName: string;
}

View File

@ -0,0 +1,5 @@
export interface IMerchant {
id: string;
name: string;
address: string;
}

View File

@ -1,36 +1,63 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { GraphQLDataSourceService } from '@poweredsoft/ngx-data-apollo'; import { GraphQLDataSourceService } from '@poweredsoft/ngx-data-apollo';
import { IDataSource, DataSource } from '@poweredsoft/data'; import { IDataSource, DataSource } from '@poweredsoft/data';
import { Apollo } from 'apollo-angular';
export interface IMerchant{ import { gql } from 'graphql-tag';
id: string, import { of } from 'rxjs';
name: string, import { IChangeMerchantNameCommand } from './IChangeMerchantNameCommand';
address: string import { IMerchant } from './IMerchant';
}
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root',
}) })
export class MerchantService { export class MerchantService {
constructor(
private dataSourceGenericService: GraphQLDataSourceService,
private apollo: Apollo
) {}
constructor(private dataSourceGenericService: GraphQLDataSourceService) { createMerchantDataSource(): IDataSource<IMerchant> {
const builder = this.dataSourceGenericService.createDataSourceOptionsBuilder<
dataSourceGenericService IMerchant,
} string
>(
createMerchantDataSource() : IDataSource<IMerchant> {
const optionsBuilder = this.dataSourceGenericService.createDataSourceOptionsBuilder<IMerchant, string>(
'merchants', 'merchants',
'GraphQLAdvanceQueryOfMerchantInput', 'GraphQLAdvanceQueryOfMerchantInput',
'id, name, address', 'id, name, address',
(model) => model.id, (model) => model.id,
{ {
page: 1, page: 1,
pageSize: 4 pageSize: 4,
}, },
true true
); );
const options = optionsBuilder.create();
builder.addMutation<IChangeMerchantNameCommand, string>(
'changeMerchantName', //<-- command name
'changeMerchantName', //<-- graph ql mutation name
// implementation of the command.
command => {
return this.apollo.use('command').mutate<string>({
mutation: gql`
mutation executeChangeName($command: changeMerchantNameInput) {
changeMerchantName(params: $command)
}
`,
variables: {
command: command,
},
});
},
// viewModel -> transform to the form model for that command -> IChangeMerchantName
e => of(<IChangeMerchantNameCommand>{
merchantId: e.model.id,
newName: e.model.name,
})
);
const options = builder.create();
return new DataSource<IMerchant>(options); return new DataSource<IMerchant>(options);
} }
} }