added error handling for ngx-data-apollo

added handle wrapper
This commit is contained in:
Mathias Beaulieu-Duncan
2019-12-10 16:54:49 -06:00
parent e5133b2f95
commit ab99d5f076
6 changed files with 158 additions and 20 deletions
+4
View File
@@ -8,4 +8,8 @@
<button (click)="testGraphQL()">
Test GraphQL query
</button>
<button (click)="testGraphQLMutation()">
Test GraphQL mutation
</button>
+76 -3
View File
@@ -1,8 +1,12 @@
import { Component, OnInit } from '@angular/core';
import { GenericRestDataSourceService } from 'projects/poweredsoft/ngx-data/src/public-api';
import { of } from 'rxjs';
import { DataSource } from '@poweredsoft/data';
import { of, Observable } from 'rxjs';
import { DataSource, IResolveCommandModelEvent } from '@poweredsoft/data';
import { GraphQLDataSourceService } from 'projects/poweredsoft/ngx-data-apollo/src/public-api';
import { Apollo } from 'apollo-angular';
import gql from 'graphql-tag';
import { map } from 'rxjs/operators';
import { DocumentNode } from 'graphql';
export interface IContact {
@@ -17,6 +21,11 @@ export interface ICustomerModel {
lastName: string;
}
export interface IFooCommand {
amount: number;
comment: string;
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
@@ -26,7 +35,7 @@ export class AppComponent implements OnInit {
title = 'ngx-data';
dataSource: DataSource<ICustomerModel>;
constructor(genericService: GenericRestDataSourceService, private graphQLService: GraphQLDataSourceService) {
constructor(genericService: GenericRestDataSourceService, private apollo: Apollo, private graphQLService: GraphQLDataSourceService) {
const keyResolver = (model: ICustomerModel) => model.id;
const transportOptions = genericService.createStandardRestTransportOptions('api/customer', keyResolver);
@@ -77,6 +86,70 @@ export class AppComponent implements OnInit {
});
}
testGraphQLMutation() {
const builder = this.graphQLService.createDataSourceOptionsBuilder<IContact, number>(
'contacts',
'GraphQLAdvanceQueryOfContactModelInput',
'id firstName lastName',
(m) => m.id,
{
groups: [
{
path: 'sex'
}
],
aggregates: [
{
path: 'id',
type: 'Max'
}
]
}
);
builder.addMutation<IFooCommand, string>('create', 'foo', (command) => {
return this.apollo.mutate<string>({
mutation: gql`mutation executeFoo($command: FooCommandInput) {
foo(params: $command)
}`,
variables: {
command: command
}
});
},
(event) => {
console.log(event);
if (event.model.id)
return of({
firstName: 'hello world'
});
});
const dataSourceOptions = builder.create();
const dataSource = new DataSource<IContact>(dataSourceOptions);
let event: IResolveCommandModelEvent<IContact> = {
command: 'create',
model: {
id: 1,
firstName: 'hello',
lastName: 'world'
}
};
dataSource.resolveCommandModelByName(event)
.subscribe((result) => {
console.log('resolve result', result);
});
dataSource.executeCommandByName('create', {
amount: 0,
//comment: "hello"
}).subscribe((result) => {
console.log(result);
})
}
testGraphQL() {
const builder = this.graphQLService.createDataSourceOptionsBuilder<IContact, number>(
'contacts',
+1 -1
View File
@@ -3,7 +3,7 @@ import {ApolloModule, APOLLO_OPTIONS} from 'apollo-angular';
import {HttpLinkModule, HttpLink} from 'apollo-angular-link-http';
import {InMemoryCache} from 'apollo-cache-inmemory';
const uri = 'https://localhost:5001/graphql'; // <-- add the URL of the GraphQL server here
const uri = 'http://localhost:5000/graphql'; // <-- add the URL of the GraphQL server here
export function createApollo(httpLink: HttpLink) {
return {
link: httpLink.create({uri}),