added validation test for the framework test project
This commit is contained in:
parent
4e40ecac91
commit
b235b2ad1b
@ -208,16 +208,17 @@ export class GraphQLDataSourceOptionsBuilder<TModel, TKey> {
|
|||||||
map(result => {
|
map(result => {
|
||||||
return result.data[mutationName];
|
return result.data[mutationName];
|
||||||
}),
|
}),
|
||||||
catchError((error: any) => {
|
catchError((error: ApolloError) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
// should handle bad request with exception
|
// should handle bad request with exception
|
||||||
// should handle bad request with validation
|
// should handle bad request with validation
|
||||||
// should handle forbidden result 403
|
// should handle forbidden result 403
|
||||||
// should handle not authorized result 401
|
// should handle not authorized result 401
|
||||||
|
|
||||||
const apolloError : ApolloError = error;
|
if (!error.networkError) {
|
||||||
if (!apolloError.networkError) {
|
const validationError = error.graphQLErrors.find(t => t.extensions.code == 'ValidationError');
|
||||||
const validationError = apolloError.graphQLErrors.find(t => t.extensions.code == 'ValidationError');
|
const authenticationError = error.graphQLErrors.find(t => t.extensions.code == 'AuthenticationError');
|
||||||
|
|
||||||
if (validationError) {
|
if (validationError) {
|
||||||
const extensions = validationError.extensions;
|
const extensions = validationError.extensions;
|
||||||
const result = Object.keys(extensions).filter(t => t != 'code').reduce((prev, attributeName) => {
|
const result = Object.keys(extensions).filter(t => t != 'code').reduce((prev, attributeName) => {
|
||||||
@ -225,16 +226,24 @@ export class GraphQLDataSourceOptionsBuilder<TModel, TKey> {
|
|||||||
return prev;
|
return prev;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
|
console.log('error result', result);
|
||||||
|
|
||||||
return throwError(<IDataSourceValidationError>{
|
return throwError(<IDataSourceValidationError>{
|
||||||
type: 'validation',
|
type: 'validation',
|
||||||
errors: result
|
errors: result
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
else if (authenticationError) {
|
||||||
|
return throwError(<IDataSourceErrorMessage>{
|
||||||
|
type: 'message',
|
||||||
|
message: "Unauthorized"
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return throwError(<IDataSourceErrorMessage>{
|
return throwError(<IDataSourceErrorMessage>{
|
||||||
type: 'message',
|
type: 'message',
|
||||||
message: apolloError.message
|
message: error.message
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -1,15 +1,3 @@
|
|||||||
<button (click)="onCreate()">
|
<button class="ui button" (click)="testValidation()">
|
||||||
Create
|
Test Validation
|
||||||
</button>
|
|
||||||
|
|
||||||
<button (click)="onDelete()">
|
|
||||||
Delete
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<button (click)="testGraphQL()">
|
|
||||||
Test GraphQL query
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<button (click)="testGraphQLMutation()">
|
|
||||||
Test GraphQL mutation
|
|
||||||
</button>
|
</button>
|
@ -8,6 +8,7 @@ import gql from 'graphql-tag';
|
|||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
import { DocumentNode } from 'graphql';
|
import { DocumentNode } from 'graphql';
|
||||||
import { GraphQLDataSourceService, IGraphQLAdvanceQueryInput } from 'projects/poweredsoft/ngx-data-apollo/src/public-api';
|
import { GraphQLDataSourceService, IGraphQLAdvanceQueryInput } from 'projects/poweredsoft/ngx-data-apollo/src/public-api';
|
||||||
|
import { TestService, ITestModel, IValidationTestCommand } from './services/test.service';
|
||||||
|
|
||||||
|
|
||||||
export interface IContact {
|
export interface IContact {
|
||||||
@ -39,150 +40,32 @@ export interface IContactDetailQuery extends IGraphQLAdvanceQueryInput<IContactM
|
|||||||
})
|
})
|
||||||
export class AppComponent implements OnInit {
|
export class AppComponent implements OnInit {
|
||||||
title = 'ngx-data';
|
title = 'ngx-data';
|
||||||
dataSource: DataSource<IContactModel>;
|
dataSource: DataSource<ITestModel>;
|
||||||
|
|
||||||
constructor(genericService: GenericRestDataSourceService, private apollo: Apollo, private graphQLService: GraphQLDataSourceService) {
|
constructor(private testService: TestService) {
|
||||||
const keyResolver = (model: IContactModel) => model.id;
|
this.dataSource = testService.generateDatasource({
|
||||||
|
criteria: {
|
||||||
const transportOptions = genericService.createStandardRestTransportOptions('api/customer', keyResolver);
|
|
||||||
|
|
||||||
this.dataSource = new DataSource<IContactModel>({
|
|
||||||
resolveIdField: keyResolver,
|
|
||||||
transport: transportOptions,
|
|
||||||
defaultCriteria: {
|
|
||||||
page: 1,
|
page: 1,
|
||||||
pageSize: 5,
|
pageSize: 10
|
||||||
groups: [
|
|
||||||
{ path: 'lastName' }
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.dataSource.notifyMessage$.subscribe((notification) => {
|
this.dataSource.notifyMessage$.subscribe((notification) => {
|
||||||
//console.log(notification);
|
console.log('notifcation', notification);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.dataSource.validationError$.subscribe((notification) => {
|
this.dataSource.validationError$.subscribe((notification) => {
|
||||||
//console.log(notification);
|
console.log('error', notification);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onCreate(): void {
|
testValidation() {
|
||||||
//console.log('excuting command!');
|
this.dataSource.executeCommandByName<IValidationTestCommand, string>('validationTest', {
|
||||||
this.dataSource.executeCommandByName('create', {
|
value: 'test'
|
||||||
firstName: "",
|
|
||||||
lastName: "Baba"
|
|
||||||
}).subscribe(() => {
|
|
||||||
|
|
||||||
}, error => {
|
|
||||||
//console.log(error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
onDelete(): void {
|
|
||||||
//console.log('excuting command!');
|
|
||||||
this.dataSource.executeCommandByName('delete', {
|
|
||||||
id: 1
|
|
||||||
}).subscribe(() => {
|
|
||||||
|
|
||||||
}, error => {
|
|
||||||
//console.log(error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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) => {
|
}).subscribe((result) => {
|
||||||
console.log(result);
|
console.log(result);
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
testGraphQL() {
|
|
||||||
|
|
||||||
const builder = this.graphQLService.createDataSourceOptionsBuilder<IContact, number>(
|
|
||||||
'contacts',
|
|
||||||
'ContactDetailQueryInput',
|
|
||||||
'id firstName lastName',
|
|
||||||
(m) => m.id,
|
|
||||||
{
|
|
||||||
aggregates: [
|
|
||||||
{
|
|
||||||
path: 'id',
|
|
||||||
type: 'Max'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
builder.beforeRead<IContactDetailQuery>(query => {
|
|
||||||
return of({ ...query, sex: "Male"});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const dataSourceOptions = builder.create();
|
|
||||||
const dataSource = new DataSource<IContact>(dataSourceOptions);
|
|
||||||
|
|
||||||
const subscription = dataSource.data$.subscribe(contacts => {
|
|
||||||
console.log(contacts);
|
|
||||||
});
|
|
||||||
dataSource.refresh();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {NgModule} from '@angular/core';
|
import {NgModule} from '@angular/core';
|
||||||
import {ApolloModule, APOLLO_OPTIONS, Apollo} from 'apollo-angular';
|
import {ApolloModule, Apollo} from 'apollo-angular';
|
||||||
import {HttpLinkModule, HttpLink} from 'apollo-angular-link-http';
|
import {HttpLinkModule, HttpLink} from 'apollo-angular-link-http';
|
||||||
import {InMemoryCache} from 'apollo-cache-inmemory';
|
import {InMemoryCache} from 'apollo-cache-inmemory';
|
||||||
|
|
||||||
|
68
src/app/services/test.service.ts
Normal file
68
src/app/services/test.service.ts
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { IGraphQLAdvanceQueryInput, GraphQLDataSourceService } from 'projects/poweredsoft/ngx-data-apollo/src/public-api';
|
||||||
|
import { IQueryCriteria, DataSource } from '@poweredsoft/data';
|
||||||
|
import { of } from 'rxjs';
|
||||||
|
import { Apollo } from 'apollo-angular';
|
||||||
|
import gql from 'graphql-tag';
|
||||||
|
|
||||||
|
export interface IValidationTestCommand {
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ITestModel {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ITestQuery extends IGraphQLAdvanceQueryInput<ITestModel> {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IGenerateDatasource<T, TAdvanceQuery extends IGraphQLAdvanceQueryInput<T>> {
|
||||||
|
criteria: IQueryCriteria;
|
||||||
|
beforeReadQueryCriteria?: TAdvanceQuery
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IGenerateItemDatasource extends IGenerateDatasource<ITestModel, ITestQuery> {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class TestService {
|
||||||
|
constructor(private graphQLService: GraphQLDataSourceService, private apollo: Apollo) { }
|
||||||
|
|
||||||
|
generateDatasource(options: IGenerateItemDatasource) {
|
||||||
|
const keyResolver = (m: ITestModel) => m.id;
|
||||||
|
let builder = this.graphQLService.createDataSourceOptionsBuilder<ITestModel, string>(
|
||||||
|
"test",
|
||||||
|
"TestQueryInput",
|
||||||
|
[
|
||||||
|
'id'
|
||||||
|
],
|
||||||
|
keyResolver,
|
||||||
|
options.criteria
|
||||||
|
);
|
||||||
|
|
||||||
|
if (options.beforeReadQueryCriteria) {
|
||||||
|
builder.beforeRead<ITestQuery>(query => {
|
||||||
|
return of({ ...query, ...options.beforeReadQueryCriteria });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.addMutation<IValidationTestCommand, string>(
|
||||||
|
'validationTest',
|
||||||
|
'validationTest',
|
||||||
|
(command) => {
|
||||||
|
return this.apollo.mutate<string>({
|
||||||
|
mutation:gql`mutation executeValidationTest($command: ValidationTestCommandInput) {
|
||||||
|
validationTest(params: $command)
|
||||||
|
}`,
|
||||||
|
variables: {
|
||||||
|
command: command
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return new DataSource<ITestModel>(builder.create());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user