remove apollo for now

This commit is contained in:
2024-08-25 19:07:21 -04:00
parent 8d50344a03
commit 6567466700
17 changed files with 8 additions and 979 deletions
@@ -1,24 +0,0 @@
# NgxDataApollo
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.2.4.
## Code scaffolding
Run `ng generate component component-name --project ngx-data-apollo` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project ngx-data-apollo`.
> Note: Don't forget to add `--project ngx-data-apollo` or else it will be added to the default project in your `angular.json` file.
## Build
Run `ng build ngx-data-apollo` to build the project. The build artifacts will be stored in the `dist/` directory.
## Publishing
After building your library with `ng build ngx-data-apollo`, go to the dist folder `cd dist/ngx-data-apollo` and run `npm publish`.
## Running unit tests
Run `ng test ngx-data-apollo` to execute the unit tests via [Karma](https://karma-runner.github.io).
## Further help
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
@@ -1,32 +0,0 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../../../coverage/poweredsoft/ngx-data-apollo'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};
@@ -1,7 +0,0 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../../dist/poweredsoft/ngx-data-apollo",
"lib": {
"entryFile": "src/public-api.ts"
}
}
@@ -1,18 +0,0 @@
{
"name": "@poweredsoft/ngx-data-apollo",
"version": "0.0.9",
"dependencies": {
"tslib": "^2.0.0"
},
"peerDependencies": {
"@poweredsoft/data": "^0.0.31",
"@angular/common": "^8.2.4",
"@angular/core": "^8.2.4",
"apollo-angular-link-http": "^1.9.0",
"apollo-link": "^1.2.11",
"apollo-client": "^2.6.0",
"apollo-cache-inmemory": "^1.6.0",
"graphql-tag": "^2.10.0",
"graphql": "^14.5.0"
}
}
@@ -1,401 +0,0 @@
import { IDataSourceOptions, IQueryCriteria, IDataSourceTransportOptions, IDataSourceQueryAdapterOptions, IDataSourceCommandAdapterOptions, IAdvanceQueryAdapter, IFilter, IAggregate, ISort, IGroup, ISimpleFilter, ICompositeFilter, IQueryExecutionGroupResult, IQueryExecutionResult, IAggregateResult, IGroupQueryResult, IResolveCommandModelEvent, IDataSourceErrorMessage, IDataSourceValidationError } from '@poweredsoft/data';
import { Apollo } from 'apollo-angular';
import { IGraphQLAdvanceQueryResult, IGraphQLAdvanceQueryInput, IGraphQLAdvanceQueryFilterInput, IGraphQLAdvanceQueryAggregateInput, IGraphQLAdvanceQuerySortInput, IGraphQLAdvanceQueryGroupInput, FilterType, IGraphQLAdvanceQueryAggregateResult, IGraphQLVariantResult, AggregateType, IGraphQLAdvanceGroupResult } from './models';
import gql from 'graphql-tag';
import { DocumentNode, GraphQLError } from 'graphql';
import { map, catchError, switchMap } from 'rxjs/operators';
import { Observable, throwError, of } from 'rxjs';
import { FetchResult } from 'apollo-link';
import { ApolloError } from 'apollo-client';
export class GraphQLDataSourceOptionsBuilder<TModel, TKey> {
private _commands: { [key: string] : IDataSourceCommandAdapterOptions<any> } = {};
private _beforeRead: (TQuery: IGraphQLAdvanceQueryInput<TModel>) => Observable<IGraphQLAdvanceQueryInput<TModel>>;
querySelect: string;
constructor(private apollo: Apollo,
private queryName: string,
private queryInputName: string,
querySelect: string | string[],
private keyResolver: (model: TModel) => TKey,
private defaultCriteria: IQueryCriteria,
private manageNotificationMessage: boolean)
{
if (Array.isArray(querySelect))
this.querySelect = querySelect.join(' ');
else
this.querySelect = querySelect;
}
create(): IDataSourceOptions<TModel> {
let ret: IDataSourceOptions<TModel> = {
resolveIdField: this.keyResolver,
defaultCriteria: this.defaultCriteria,
transport: this.createTransport()
};
return ret;
}
protected createTransport(): IDataSourceTransportOptions<TModel> {
let ret: IDataSourceTransportOptions<TModel> = {
query: this.createQuery(),
commands: this._commands
};
return ret;
}
public beforeRead<TAdvanceQuery extends IGraphQLAdvanceQueryInput<TModel>>(beforeRead: (query: TAdvanceQuery) => Observable<TAdvanceQuery>)
{
this._beforeRead = beforeRead;
return this._beforeRead;
}
protected createQuery(): IDataSourceQueryAdapterOptions<TModel> {
let ret: IDataSourceQueryAdapterOptions<TModel> = {
adapter: <IAdvanceQueryAdapter<IQueryCriteria, TModel>>{
handle: (query: IQueryCriteria) => {
const finalBeforeRead = this._beforeRead || (t => of(t));
const advanceQuery = this.createGraphQLQueryCriteria(query);
return finalBeforeRead(advanceQuery)
.pipe(
switchMap(finalAdvanceQuery => {
const o$ = this.apollo.query<any>({
query: this.createGraphQLQuery(finalAdvanceQuery),
variables: {
criteria: finalAdvanceQuery
}
});
return o$.pipe(
map(result => {
const queryResult = result.data[this.queryName] as IGraphQLAdvanceQueryResult<TModel>;
return this.queryResultFromGraphQLAdvancedResult(finalAdvanceQuery, queryResult);
})
);
})
);
}
}
};
return ret;
}
private queryResultFromGraphQLAdvancedResult(query: IGraphQLAdvanceQueryInput<TModel>, result: IGraphQLAdvanceQueryResult<TModel>): IQueryExecutionResult<TModel> | IQueryExecutionGroupResult<TModel> {
const ret: IQueryExecutionGroupResult<TModel> & IQueryExecutionResult<TModel> = {
data: result.data,
groups: result.groups ? result.groups.map(this.fromGraphQLGroupResult.bind(this)) : null,
totalRecords: result.totalRecords,
numberOfPages: result.numberOfPages,
aggregates: result.aggregates ? result.aggregates.map(this.fromGraphQLAggregateResult.bind(this)) : null
};
return ret;
}
private fromGraphQLGroupResult(group: IGraphQLAdvanceGroupResult<TModel>) : IGroupQueryResult<TModel> {
return {
aggregates: group.aggregates ? group.aggregates.map(this.convertAggregates.bind(this)) : null,
data: group.data,
groupPath: group.groupPath,
groupValue: this.getValueFromVariantResult(group.groupValue),
hasSubGroups: group.hasSubGroups,
subGroups: group.subGroups ? group.subGroups.map(this.fromGraphQLGroupResult.bind(this)) : null
};
}
private fromGraphQLAggregateResult(agg: IGraphQLAdvanceQueryAggregateResult): IAggregateResult {
return {
path: agg.path,
type: this.normalizeFirstLetter(agg.type),
value: this.getValueFromVariantResult(agg.value)
};
}
private normalizeFirstLetter(type: string): string {
if (type) {
const ret = type.toLowerCase();
return ret.substring(0, 1).toUpperCase() + ret.substring(1);
}
return type;
}
private getValueFromVariantResult(variant: IGraphQLVariantResult): any {
if (variant && variant.typeName)
{
if (variant.typeName.toLowerCase() == "int")
return variant.intValue;
else if (variant.typeName.toLowerCase() == "long")
return variant.longValue;
else if (variant.typeName.toLowerCase() == "boolean")
return variant.booleanValue;
else if (variant.typeName.toLowerCase() == "decimal")
return variant.decimalValue;
else if (variant.typeName.toLowerCase() == "datetime")
return variant.dateTimeValue;
else if (variant.typeName.toLowerCase() == "string")
return variant.stringValue;
else if (variant.typeName.toLowerCase() == "json")
return JSON.parse(variant.json);
}
return null;
}
private createGraphQLQuery(query: IGraphQLAdvanceQueryInput<TModel>): DocumentNode {
return gql`
query getAll($criteria: ${this.queryInputName}) {
${this.queryName}(params: $criteria) {
totalRecords
numberOfPages
${this.createAggregateSelect(query)}
${this.createQuerySelect(query)}
}
}
`;
}
private createAggregateSelect(query: IGraphQLAdvanceQueryInput<TModel>): any {
if (query.aggregates && query.aggregates.length)
{
return `
aggregates {
type
path
value {
${this.createSelectVariant()}
}
}
`;
}
return '';
}
private createGraphQLQueryCriteria(query: IQueryCriteria): IGraphQLAdvanceQueryInput<TModel> {
const ret: IGraphQLAdvanceQueryInput<TModel> = {
page: query.page,
pageSize: query.pageSize,
filters: query.filters ? query.filters.map(this.convertFilter.bind(this)) : null,
sorts: query.sorts ? query.sorts.map(this.convertSort.bind(this)) : null,
aggregates: query.aggregates ? query.aggregates.map(this.convertAggregates.bind(this)) : null,
groups: query.groups ? query.groups.map(this.convertGroup.bind(this)) : null
};
return ret;
}
/*public addMutationTest<TMutation, TMutationResult>(name: string, mutationName: string, mutationSelect?: string, resolveCommandModel?: (event: IResolveCommandModelEvent<TModel>) => Observable<TMutation & any>) {
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>> {
adapter: {
handle: this.apollo.use()
},
resolveCommandModel: resolveCommandModel
};
return this;
}*/
public addMutation<TMutation, TMutationResult>(name: string, mutationName: string, handle: (command: TMutation) => Observable<FetchResult<TMutationResult>>, resolveCommandModel?: (event: IResolveCommandModelEvent<TModel>) => Observable<TMutation & any>) {
const handleWrapper = command => {
return handle(command)
.pipe(
map(result => {
return result.data[mutationName];
}),
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
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) => {
prev[attributeName] = extensions[attributeName];
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: error.message
});
})
);
};
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>> {
adapter: {
handle: handleWrapper
},
resolveCommandModel: resolveCommandModel
};
return this;
}
private createGroupSelect(query: IGraphQLAdvanceQueryInput<TModel>, group: IGroup, isLast: boolean) {
let ret = `
groupPath
groupValue {
${this.createSelectVariant()}
}
hasSubGroups
${this.createAggregateSelect(query)}
`;
if (isLast) {
ret += `
data {
${this.querySelect}
}
`;
} else {
ret += `
subGroups {
___INNER___
}
`;
}
return ret;
}
private createSelectVariant() {
return `booleanValue dateTimeValue decimalValue intValue json longValue stringValue typeName`;
}
private createQuerySelect(query: IGraphQLAdvanceQueryInput<TModel>): string {
if (query.groups && query.groups.length) {
const groupSelect = query.groups.reduce((prev, current, currentIndex) => {
const isLast = currentIndex+1 == query.groups.length;
const group = this.createGroupSelect(query, current, isLast);
return prev.replace('___INNER___', group);
}, `
groups {
___INNER___
}
`);
return groupSelect;
}
return `
data {
${this.querySelect}
}
`;
}
private convertGroup(group: IGroup): IGraphQLAdvanceQueryGroupInput {
return {
path: group.path,
ascending: group.ascending
};
}
private convertAggregates(aggregate: IAggregate): IGraphQLAdvanceQueryAggregateInput {
return {
path: aggregate.path,
type: this.resolveAggregateType(aggregate.type)
};
}
private resolveAggregateType(type: string): AggregateType {
return type ? type.toUpperCase() as any : null;
/*
if (type)
{
if (type.toUpperCase() == 'COUNT') return AggregateType.COUNT
if (type.toUpperCase() == 'SUM') return AggregateType.SUM
if (type.toUpperCase() == 'AVG') return AggregateType.AVG
if (type.toUpperCase() == 'LONGCOUNT') return AggregateType.LONGCOUNT
if (type.toUpperCase() == 'MIN') return AggregateType.MIN
if (type.toUpperCase() == 'MAX') return AggregateType.MAX
if (type.toUpperCase() == 'FIRST') return AggregateType.FIRST
if (type.toUpperCase() == 'FIRSTORDEFAULT') return AggregateType.FIRSTORDEFAULT
if (type.toUpperCase() == 'LAST') return AggregateType.LAST
if (type.toUpperCase() == 'LASTORDEFAULT') return AggregateType.LASTORDEFAULT
}
throw new Error('Aggregate type');*/
}
private convertSort(sort: ISort): IGraphQLAdvanceQuerySortInput {
return {
path: sort.path,
ascending: sort.ascending
};
}
private convertFilter(filter: IFilter): IGraphQLAdvanceQueryFilterInput {
if (filter.type == "Composite") {
const compositeFilter = filter as ICompositeFilter;
return {
not: false,
and: compositeFilter.and,
type: FilterType.COMPOSITE,
filters: compositeFilter.filters.map(this.convertFilter.bind(this)),
};
}
const simpleFilter = filter as ISimpleFilter;
return {
filters: null,
and: filter.and,
not: false,
path: simpleFilter.path,
type: this.resolveFilterType(simpleFilter.type),
value: {
stringValue: simpleFilter.value as string
}
};
}
private resolveFilterType(type: string): FilterType {
return type ? type.toUpperCase() as any: null;
/*
if (type)
{
if (type.toUpperCase() == 'EQUAL') return FilterType.EQUAL
if (type.toUpperCase() == 'CONTAINS') return FilterType.CONTAINS
if (type.toUpperCase() == 'STARTSWITH') return FilterType.STARTSWITH
if (type.toUpperCase() == 'ENDSWITH') return FilterType.ENDSWITH
if (type.toUpperCase() == 'COMPOSITE') return FilterType.COMPOSITE
if (type.toUpperCase() == 'NOTEQUAL') return FilterType.NOTEQUAL
if (type.toUpperCase() == 'GREATERTHAN') return FilterType.GREATERTHAN
if (type.toUpperCase() == 'LESSTHANOREQUAL') return FilterType.LESSTHANOREQUAL
if (type.toUpperCase() == 'GREATERTHANOREQUAL') return FilterType.GREATERTHANOREQUAL
if (type.toUpperCase() == 'LESSTHAN') return FilterType.LESSTHAN
if (type.toUpperCase() == 'IN') return FilterType.IN
if (type.toUpperCase() == 'NOTIN') return FilterType.NOTIN
}
throw new Error('unknown filter type');*/
}
}
@@ -1,36 +0,0 @@
import { Injectable } from '@angular/core';
import { IQueryCriteria, IDataSource } from '@poweredsoft/data';
import { Apollo } from 'apollo-angular';
import { GraphQLDataSourceOptionsBuilder } from './GraphQLDataSourceOptionsBuilder';
@Injectable({
providedIn: 'root'
})
export class GraphQLDataSourceService
{
constructor(private apollo: Apollo) {
}
createDataSourceOptionsBuilder<TModel, TKey>(
queryName: string,
queryInputName: string,
querySelect: string | string[],
keyResolver: (model: TModel) => TKey,
defaultCriteria: IQueryCriteria,
manageNotificationMessage: boolean = true) : GraphQLDataSourceOptionsBuilder<TModel, TKey>
{
if (Array.isArray(querySelect))
querySelect = querySelect.join(' ');
return new GraphQLDataSourceOptionsBuilder(
this.apollo,
queryName,
queryInputName,
querySelect,
keyResolver,
defaultCriteria,
manageNotificationMessage
);
}
}
@@ -1,103 +0,0 @@
export enum AggregateType {
COUNT,
SUM,
AVG,
LONGCOUNT,
MIN,
MAX,
FIRST,
FIRSTORDEFAULT,
LAST,
LASTORDEFAULT
}
export enum FilterType {
EQUAL,
CONTAINS,
STARTSWITH,
ENDSWITH,
COMPOSITE,
NOTEQUAL,
GREATERTHAN,
LESSTHANOREQUAL,
GREATERTHANOREQUAL,
LESSTHAN,
IN,
NOTIN
}
export interface IGraphQLVariantInput {
dateTimeValue?: Date
decimalValue?: number
intValue?: number
longValue?: number
stringValue?: string
booleanValue?: boolean;
}
export interface IGraphQLVariantResult {
dateTimeValue?: string;
decimalValue?: number;
intValue?: number;
json?: string;
longValue?: number;
stringValue?: string;
booleanValue?: boolean;
typeName: string;
}
export interface IGraphQLAdvanceQueryAggregateInput {
path?: string;
type: AggregateType;
}
export interface IGraphQLAdvanceQueryAggregateResult {
path: string
type: string
value: IGraphQLVariantResult
}
export interface IGraphQLAdvanceQueryFilterInput {
and?: boolean
filters?: IGraphQLAdvanceQueryFilterInput[]
not?: boolean
path?: string
type: FilterType
value?: IGraphQLVariantInput
}
export interface IGraphQLAdvanceQueryGroupInput {
ascending?: boolean
path: string
}
export interface IGraphQLAdvanceQuerySortInput {
ascending?: boolean
path: string
}
export interface IGraphQLAdvanceQueryInput<T> {
aggregates?: IGraphQLAdvanceQueryAggregateInput[]
filters?: IGraphQLAdvanceQueryFilterInput[]
groups?: IGraphQLAdvanceQueryGroupInput[]
page?: number
pageSize?: number
sorts?: IGraphQLAdvanceQuerySortInput[]
}
export interface IGraphQLAdvanceGroupResult<T> {
aggregates?: IGraphQLAdvanceQueryAggregateResult[]
data?: T[]
groupPath?: string
groupValue?: IGraphQLVariantResult
hasSubGroups?: boolean
subGroups?: IGraphQLAdvanceGroupResult<T>[]
}
export interface IGraphQLAdvanceQueryResult<T> {
aggregates?: IGraphQLAdvanceQueryAggregateResult[];
data?: T[];
groups?: IGraphQLAdvanceGroupResult<T>[];
numberOfPages?: number;
totalRecords: number;
}
@@ -1,9 +0,0 @@
import { GraphQLDataSourceService } from "./lib/graphql-datas-source.service";
/*
* Public API Surface of ngx-data-apollo
*/
export * from './lib/graphql-datas-source.service';
export * from './lib/GraphQLDataSourceOptionsBuilder';
export * from './lib/models';
@@ -1,26 +0,0 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "../../../out-tsc/lib",
"declarationMap": true,
"target": "es2015",
"declaration": true,
"inlineSources": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"enableResourceInlining": true
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
@@ -1,9 +0,0 @@
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"declarationMap": false
},
"angularCompilerOptions": {
"enableIvy": false
}
}
@@ -1,17 +0,0 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "../../../out-tsc/spec",
"types": [
"jasmine",
"node"
]
},
"files": [
"src/test.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
}
@@ -1,17 +0,0 @@
{
"extends": "../../../tslint.json",
"rules": {
"directive-selector": [
true,
"attribute",
"lib",
"camelCase"
],
"component-selector": [
true,
"element",
"lib",
"kebab-case"
]
}
}