-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add graphql class decorators (#58)
* feat: add graphql class decorators * fix: linting, prettier * feat: add enum support * fix: linting, prettier * feat: new ts types * fix: lint for naming convention
- Loading branch information
Showing
13 changed files
with
252 additions
and
20 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/models/common/decorators/property/classGraphql.decorator.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,50 @@ | ||
import 'reflect-metadata'; | ||
import { getTsTypesMapping } from '../../../layerMetadata/decorators/property/tsTypes.decorator'; | ||
import { IPropGraphQLMapping } from '../../interfaces/propGraphQLMapping.interface'; | ||
import { getGraphQLMapping } from './graphql.decorator'; | ||
|
||
const graphQLMetadataKey = Symbol('graphqlclassmapping'); | ||
type KeyValueDict = Record<string, unknown>; | ||
const target = {}; | ||
|
||
export interface IGraphQLClassMapping { | ||
name: string; | ||
fields: IPropGraphQLMapping[]; | ||
} | ||
|
||
export function graphqlClass(): ClassDecorator { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
return <TFunction extends Function>(graphqlmapping: TFunction) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call | ||
const classInstance = new (graphqlmapping as any)(); | ||
const classData: IGraphQLClassMapping = { | ||
fields: getGraphQLMappings(classInstance), | ||
name: graphqlmapping.name, | ||
}; | ||
|
||
const classDataList = getGraphQLClassMapping() ?? []; | ||
classDataList.push(classData); | ||
Reflect.defineMetadata(graphQLMetadataKey, classDataList, target); | ||
return graphqlmapping; | ||
}; | ||
} | ||
|
||
export function getGraphQLClassMapping(): IGraphQLClassMapping[] | undefined { | ||
return Reflect.getMetadata(graphQLMetadataKey, target) as IGraphQLClassMapping[]; | ||
} | ||
|
||
export function getGraphQLMappings(object: KeyValueDict): IPropGraphQLMapping[] { | ||
const ret = []; | ||
for (const prop in object) { | ||
const graphQLMap = getGraphQLMapping(object, prop); | ||
const tsTypesMap = getTsTypesMapping(object, prop); | ||
if (graphQLMap && tsTypesMap) { | ||
ret.push({ | ||
prop: prop, | ||
...graphQLMap, | ||
...tsTypesMap, | ||
}); | ||
} | ||
} | ||
return ret; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/models/common/decorators/property/graphql.decorator.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,16 @@ | ||
import 'reflect-metadata'; | ||
|
||
const graphQLMetadataKey = Symbol('graphqlmapping'); | ||
|
||
export interface IGraphQLMapping { | ||
nullable?: boolean; | ||
} | ||
|
||
export function graphql(graphqlmapping?: IGraphQLMapping): PropertyDecorator { | ||
const defaultMapping: IGraphQLMapping = { nullable: false }; | ||
return Reflect.metadata(graphQLMetadataKey, graphqlmapping ?? defaultMapping); | ||
} | ||
|
||
export function getGraphQLMapping<T>(target: T, propertyKey: string): IGraphQLMapping | undefined { | ||
return Reflect.getMetadata(graphQLMetadataKey, target, propertyKey) as IGraphQLMapping; | ||
} |
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,6 @@ | ||
export * from './decorators/property/graphql.decorator'; | ||
export * from './decorators/property/classGraphql.decorator'; | ||
export * from './interfaces/ormCatalog.interface'; | ||
export * from './interfaces/propCatalogDBMapping.interface'; | ||
export * from './interfaces/propGraphQLMapping.interface'; | ||
export * from '../layerMetadata/link'; |
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
import { IGraphQLMapping } from '../decorators/property/graphql.decorator'; | ||
import { ITsTypesMapping } from '../../layerMetadata/decorators/property/tsTypes.decorator'; | ||
|
||
export interface IPropGraphQLMapping extends IGraphQLMapping, ITsTypesMapping { | ||
prop: string; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './common/index'; | ||
export * from './layerMetadata/index'; | ||
export * from './discreteIngestion/index'; | ||
export * from './layerMetadata/pycswLayerCatalogRecord'; |
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
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
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
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
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,34 @@ | ||
import { graphqlClass } from '../common/decorators/property/classGraphql.decorator'; | ||
import { graphql } from '../common/decorators/property/graphql.decorator'; | ||
import { TsTypes, tsTypes } from './decorators/property/tsTypes.decorator'; | ||
|
||
@graphqlClass() | ||
export class Link { | ||
@tsTypes({ | ||
mappingType: TsTypes.STRING, | ||
}) | ||
@graphql({ | ||
nullable: true, | ||
}) | ||
public name?: string = undefined; | ||
|
||
@tsTypes({ | ||
mappingType: TsTypes.STRING, | ||
}) | ||
@graphql({ | ||
nullable: true, | ||
}) | ||
public description?: string = undefined; | ||
|
||
@tsTypes({ | ||
mappingType: TsTypes.STRING, | ||
}) | ||
@graphql() | ||
public protocol?: string = undefined; | ||
|
||
@tsTypes({ | ||
mappingType: TsTypes.STRING, | ||
}) | ||
@graphql() | ||
public url?: string = undefined; | ||
} |
Oops, something went wrong.