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 => { | ||||
|                         return result.data[mutationName]; | ||||
|                     }), | ||||
|                     catchError((error: any) => { | ||||
|                     catchError((error: ApolloError) => { | ||||
|                         console.log(error); | ||||
|                         // should handle bad request with exception
 | ||||
|                         // should handle bad request with validation
 | ||||
|                         // should handle forbidden result 403
 | ||||
|                         // should handle not authorized result 401
 | ||||
| 
 | ||||
|                         const apolloError : ApolloError = error; | ||||
|                         if (!apolloError.networkError) { | ||||
|                             const validationError = apolloError.graphQLErrors.find(t => t.extensions.code == 'ValidationError'); | ||||
|                         if (!error.networkError) { | ||||
|                             const validationError = error.graphQLErrors.find(t => t.extensions.code == 'ValidationError'); | ||||
|                             const authenticationError = error.graphQLErrors.find(t => t.extensions.code == 'AuthenticationError'); | ||||
| 
 | ||||
|                             if (validationError) { | ||||
|                                 const extensions = validationError.extensions; | ||||
|                                 const result = Object.keys(extensions).filter(t => t != 'code').reduce((prev, attributeName) => { | ||||
| @ -225,16 +226,24 @@ export class GraphQLDataSourceOptionsBuilder<TModel, TKey> { | ||||
|                                     return prev; | ||||
|                                 }, {}); | ||||
| 
 | ||||
|                                 console.log('error result', result); | ||||
| 
 | ||||
|                                 return throwError(<IDataSourceValidationError>{ | ||||
|                                     type: 'validation', | ||||
|                                     errors: result | ||||
|                                 }); | ||||
|                             } | ||||
|                             else if (authenticationError) { | ||||
|                                 return throwError(<IDataSourceErrorMessage>{ | ||||
|                                     type: 'message', | ||||
|                                     message: "Unauthorized" | ||||
|                                 }); | ||||
|                             } | ||||
|                         } | ||||
| 
 | ||||
|                         return throwError(<IDataSourceErrorMessage>{ | ||||
|                             type: 'message', | ||||
|                             message: apolloError.message | ||||
|                             message: error.message | ||||
|                         }); | ||||
|                     }) | ||||
|                 ); | ||||
|  | ||||
| @ -1,15 +1,3 @@ | ||||
| <button (click)="onCreate()"> | ||||
|   Create | ||||
| </button> | ||||
| 
 | ||||
| <button (click)="onDelete()"> | ||||
|   Delete | ||||
| </button> | ||||
| 
 | ||||
| <button (click)="testGraphQL()"> | ||||
|   Test GraphQL query | ||||
| </button> | ||||
| 
 | ||||
| <button (click)="testGraphQLMutation()"> | ||||
|   Test GraphQL mutation | ||||
| <button class="ui button" (click)="testValidation()"> | ||||
|   Test Validation | ||||
| </button> | ||||
| @ -8,6 +8,7 @@ import gql from 'graphql-tag'; | ||||
| import { map } from 'rxjs/operators'; | ||||
| import { DocumentNode } from 'graphql'; | ||||
| import { GraphQLDataSourceService, IGraphQLAdvanceQueryInput } from 'projects/poweredsoft/ngx-data-apollo/src/public-api'; | ||||
| import { TestService, ITestModel, IValidationTestCommand } from './services/test.service'; | ||||
| 
 | ||||
| 
 | ||||
| export interface IContact { | ||||
| @ -39,150 +40,32 @@ export interface IContactDetailQuery extends IGraphQLAdvanceQueryInput<IContactM | ||||
| }) | ||||
| export class AppComponent implements OnInit { | ||||
|   title = 'ngx-data'; | ||||
|   dataSource: DataSource<IContactModel>; | ||||
|   dataSource: DataSource<ITestModel>; | ||||
| 
 | ||||
|   constructor(genericService: GenericRestDataSourceService, private apollo: Apollo, private graphQLService: GraphQLDataSourceService) { | ||||
|     const keyResolver = (model: IContactModel) => model.id; | ||||
| 
 | ||||
|     const transportOptions = genericService.createStandardRestTransportOptions('api/customer', keyResolver); | ||||
| 
 | ||||
|     this.dataSource = new DataSource<IContactModel>({ | ||||
|       resolveIdField: keyResolver, | ||||
|       transport: transportOptions, | ||||
|       defaultCriteria: { | ||||
|   constructor(private testService: TestService) { | ||||
|     this.dataSource = testService.generateDatasource({ | ||||
|       criteria: { | ||||
|         page: 1, | ||||
|         pageSize: 5, | ||||
|         groups: [ | ||||
|           { path: 'lastName' } | ||||
|         ] | ||||
|         pageSize: 10 | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   ngOnInit(): void { | ||||
|     this.dataSource.notifyMessage$.subscribe((notification) => { | ||||
|       //console.log(notification);
 | ||||
|       console.log('notifcation', notification); | ||||
|     }); | ||||
| 
 | ||||
|     this.dataSource.validationError$.subscribe((notification) => { | ||||
|       //console.log(notification);
 | ||||
|       console.log('error', notification); | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   onCreate(): void { | ||||
|     //console.log('excuting command!');
 | ||||
|     this.dataSource.executeCommandByName('create', { | ||||
|       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"
 | ||||
|   testValidation() { | ||||
|     this.dataSource.executeCommandByName<IValidationTestCommand, string>('validationTest', { | ||||
|       value: 'test' | ||||
|     }).subscribe((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 {ApolloModule, APOLLO_OPTIONS, Apollo} from 'apollo-angular'; | ||||
| import {ApolloModule, Apollo} from 'apollo-angular'; | ||||
| import {HttpLinkModule, HttpLink} from 'apollo-angular-link-http'; | ||||
| 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