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 { 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<IMerchant> {
const optionsBuilder = this.dataSourceGenericService.createDataSourceOptionsBuilder<IMerchant, string>(
createMerchantDataSource(): IDataSource<IMerchant> {
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<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);
}
}