Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
523 additions
and
144 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
packages/graphql/lib/schema-builder/collections/array-with-global-cache.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export class ArrayWithGlobalCacheCollection<T> { | ||
private readonly array: T[] = []; | ||
|
||
constructor(private globalArray: Array<T>) {} | ||
|
||
getAll() { | ||
return this.array; | ||
} | ||
|
||
push(...items): number { | ||
this.globalArray.push(...items); | ||
return this.array.push(...items); | ||
} | ||
|
||
unshift(...items): number { | ||
this.globalArray.unshift(...items); | ||
return this.array.unshift(...items); | ||
} | ||
|
||
reverse() { | ||
return this.array.reverse(); | ||
} | ||
|
||
reduce<U>( | ||
callbackfn: ( | ||
previousValue: U, | ||
currentValue: T, | ||
currentIndex: number, | ||
array: T[], | ||
) => U, | ||
initialValue: U, | ||
): U { | ||
return this.array.reduce(callbackfn, initialValue); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/graphql/lib/schema-builder/collections/field-directive.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { MetadataListByNameCollection } from './metadata-list-by-name.collection'; | ||
import { PropertyDirectiveMetadata } from '../metadata'; | ||
|
||
export class FieldDirectiveCollection extends MetadataListByNameCollection<PropertyDirectiveMetadata> { | ||
sdls = new Set<string>(); | ||
fieldNames = new Set<string>(); | ||
|
||
add(value: PropertyDirectiveMetadata) { | ||
if (this.sdls.has(value.sdl) && this.fieldNames.has(value.fieldName)) { | ||
return; | ||
} | ||
|
||
super.add(value, value.fieldName); | ||
|
||
this.sdls.add(value.sdl); | ||
this.fieldNames.add(value.fieldName); | ||
this.globalArray?.push(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from './metadata.storage.collection.list'; | ||
export * from './field-directive.collection'; | ||
export * from './array-with-global-cache.collection'; | ||
export * from './metada.collection.model.interface'; | ||
export * from './metadata-by-name.collection'; | ||
export * from './metadata-list-by-name.collection'; | ||
export * from './metadata.storage.collection'; |
14 changes: 14 additions & 0 deletions
14
packages/graphql/lib/schema-builder/collections/metada.collection.model.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ClassMetadata, ResolverClassMetadata } from '../metadata'; | ||
import { ObjectTypeMetadata } from '../metadata/object-type.metadata'; | ||
|
||
export interface MetadataCollectionModelInterface { | ||
argumentType: ClassMetadata[]; | ||
interface: ClassMetadata[]; | ||
inputType: ClassMetadata[]; | ||
objectType: ObjectTypeMetadata[]; | ||
resolver: ResolverClassMetadata[]; | ||
classDirectives: []; | ||
classExtensions: []; | ||
fieldDirectives: []; | ||
fieldExtensions: []; | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/graphql/lib/schema-builder/collections/metadata-by-name.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export class MetadataByNameCollection<T> { | ||
protected map = new Map<string, T>(); | ||
protected all: (T extends any[] ? T[number] : T)[] = []; | ||
|
||
getAll() { | ||
return this.all; | ||
} | ||
|
||
getByName(name: string) { | ||
return this.map.get(name); | ||
} | ||
|
||
add(value: T extends any[] ? T[number] : T, name: string) { | ||
if (this.map.has(name)) return; | ||
|
||
this.map.set(name, value); | ||
this.all.push(value); | ||
} | ||
|
||
unshift(value: T extends any[] ? T[number] : T, name: string) { | ||
if (this.map.has(name)) return; | ||
|
||
this.map.set(name, value); | ||
this.all.unshift(value); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/graphql/lib/schema-builder/collections/metadata-list-by-name.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { MetadataByNameCollection } from './metadata-by-name.collection'; | ||
|
||
export class MetadataListByNameCollection<T> extends MetadataByNameCollection< | ||
T[] | ||
> { | ||
constructor(protected globalArray: Array<T> = null) { | ||
super(); | ||
} | ||
|
||
getByName(name: string): T[] { | ||
return super.getByName(name) || []; | ||
} | ||
|
||
add(value: T, name: string) { | ||
let arrayResult = super.getByName(name); | ||
if (!arrayResult) { | ||
arrayResult = []; | ||
this.map.set(name, arrayResult); | ||
} | ||
|
||
arrayResult.push(value); | ||
this.all.push(value); | ||
this.globalArray && this.globalArray.push(value); | ||
} | ||
|
||
unshift(value: T, name: string) { | ||
let arrayResult = super.getByName(name); | ||
if (!arrayResult) { | ||
arrayResult = []; | ||
this.map.set(name, arrayResult); | ||
} | ||
|
||
arrayResult.unshift(value); | ||
this.all.push(value); | ||
this.globalArray && this.globalArray.unshift(value); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/graphql/lib/schema-builder/collections/metadata.storage.collection.list.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { MetadataStorageCollection } from './metadata.storage.collection'; | ||
import { MetadataCollectionModelInterface } from './metada.collection.model.interface'; | ||
|
||
export class MetadataStorageCollectionList { | ||
private storageMap = new Map<Function, MetadataStorageCollection>(); | ||
private storageList = new Array<MetadataStorageCollection>(); | ||
|
||
public all: MetadataCollectionModelInterface = { | ||
argumentType: [], | ||
interface: [], | ||
inputType: [], | ||
objectType: [], | ||
resolver: [], | ||
classDirectives: [], | ||
classExtensions: [], | ||
fieldDirectives: [], | ||
fieldExtensions: [], | ||
}; | ||
|
||
get(target: Function) { | ||
let metadata = this.storageMap.get(target); | ||
|
||
if (!metadata) { | ||
metadata = new MetadataStorageCollection(this.all); | ||
this.storageMap.set(target, metadata); | ||
this.storageList.push(metadata); | ||
} | ||
|
||
return metadata; | ||
} | ||
|
||
compile() { | ||
this.reversePredicate((t) => t.classDirectives.getAll()); | ||
this.reversePredicate((t) => t.classExtensions.getAll()); | ||
this.reversePredicate((t) => t.fieldDirectives.getAll()); | ||
this.reversePredicate((t) => t.fieldExtensions.getAll()); | ||
} | ||
|
||
private reversePredicate<V>( | ||
predicate: (t: MetadataStorageCollection) => Array<V>, | ||
) { | ||
this.storageList.forEach((t) => predicate(t).reverse()); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
packages/graphql/lib/schema-builder/collections/metadata.storage.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { MetadataByNameCollection } from './metadata-by-name.collection'; | ||
import { | ||
ClassDirectiveMetadata, | ||
ClassExtensionsMetadata, | ||
ClassMetadata, | ||
MethodArgsMetadata, | ||
PropertyExtensionsMetadata, | ||
PropertyMetadata, | ||
ResolverClassMetadata, | ||
} from '../metadata'; | ||
import { MetadataListByNameCollection } from './metadata-list-by-name.collection'; | ||
import { FieldDirectiveCollection } from './field-directive.collection'; | ||
import { ObjectTypeMetadata } from '../metadata/object-type.metadata'; | ||
import { ArrayWithGlobalCacheCollection } from './array-with-global-cache.collection'; | ||
import { MetadataCollectionModelInterface } from './metada.collection.model.interface'; | ||
|
||
export class MetadataStorageCollection { | ||
constructor(private all: MetadataCollectionModelInterface) {} | ||
|
||
fields = new MetadataByNameCollection<PropertyMetadata>(); | ||
params = new MetadataListByNameCollection<MethodArgsMetadata>(); | ||
fieldDirectives = new FieldDirectiveCollection(this.all.fieldDirectives); | ||
fieldExtensions = | ||
new MetadataListByNameCollection<PropertyExtensionsMetadata>( | ||
this.all.fieldExtensions, | ||
); | ||
classDirectives = new ArrayWithGlobalCacheCollection<ClassDirectiveMetadata>( | ||
this.all.classDirectives, | ||
); | ||
classExtensions = new ArrayWithGlobalCacheCollection<ClassExtensionsMetadata>( | ||
this.all.classExtensions, | ||
); | ||
|
||
set argumentType(val: ClassMetadata) { | ||
this._argumentType = val; | ||
this.all.argumentType.push(val); | ||
} | ||
|
||
get argumentType() { | ||
return this._argumentType; | ||
} | ||
|
||
set interface(val: ClassMetadata) { | ||
this._interface = val; | ||
this.all.interface.push(val); | ||
} | ||
|
||
get interface() { | ||
return this._interface; | ||
} | ||
|
||
set inputType(val: ClassMetadata) { | ||
this._inputType = val; | ||
this.all.inputType.push(val); | ||
} | ||
|
||
get inputType() { | ||
return this._inputType; | ||
} | ||
|
||
set objectType(val: ObjectTypeMetadata) { | ||
this._objectType = val; | ||
this.all.objectType.push(val); | ||
} | ||
|
||
get objectType() { | ||
return this._objectType; | ||
} | ||
|
||
set resolver(val: ResolverClassMetadata) { | ||
this._resolver = val; | ||
this.all.resolver.push(val); | ||
} | ||
|
||
get resolver() { | ||
return this._resolver; | ||
} | ||
|
||
_argumentType: ClassMetadata; | ||
_interface: ClassMetadata; | ||
_inputType: ClassMetadata; | ||
_objectType: ObjectTypeMetadata; | ||
_resolver: ResolverClassMetadata; | ||
} |
Oops, something went wrong.