Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Extensions metadata Decorator #512

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/decorators/Extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { SymbolKeysNotSupportedError } from "../errors";
import { getMetadataStorage } from "../metadata/getMetadataStorage";

export function Extensions(extensions: Record<string, any>): MethodDecorator | PropertyDecorator {
return (target, propertyKey, descriptor) => {
if (typeof propertyKey === "symbol") {
throw new SymbolKeysNotSupportedError();
}
getMetadataStorage().collectExtensionsMetadata({
target: target.constructor,
fieldName: propertyKey,
extensions,
});
};
}
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { createParamDecorator } from "./createParamDecorator";
export { createMethodDecorator } from "./createMethodDecorator";
export { Ctx } from "./Ctx";
export { Directive } from "./Directive";
export { Extensions } from "./Extensions";
export { registerEnumType } from "./enums";
export { Field, FieldOptions } from "./Field";
export { FieldResolver } from "./FieldResolver";
Expand Down
5 changes: 5 additions & 0 deletions src/metadata/definitions/extensions-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ExtensionsMetadata {
target: Function;
fieldName: string;
extensions: Record<string, any>;
}
1 change: 1 addition & 0 deletions src/metadata/definitions/field-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export interface FieldMetadata {
roles?: any[];
middlewares?: Array<Middleware<any>>;
directives?: DirectiveMetadata[];
extensions?: Record<string, any>;
simple?: boolean;
}
1 change: 1 addition & 0 deletions src/metadata/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./authorized-metadata";
export * from "./class-metadata";
export * from "./directive-metadata";
export * from "./enum-metadata";
export * from "./extensions-metadata";
export * from "./field-metadata";
export * from "./middleware-metadata";
export * from "./param-metadata";
Expand Down
1 change: 1 addition & 0 deletions src/metadata/definitions/resolver-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface BaseResolverMetadata {
roles?: any[];
middlewares?: Array<Middleware<any>>;
directives?: DirectiveMetadata[];
extensions?: Record<string, any>;
}

export interface ResolverMetadata extends BaseResolverMetadata {
Expand Down
15 changes: 15 additions & 0 deletions src/metadata/metadata-storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ResolverMetadata,
ClassMetadata,
ExtensionsMetadata,
FieldMetadata,
ParamMetadata,
FieldResolverMetadata,
Expand Down Expand Up @@ -40,6 +41,7 @@ export class MetadataStorage {
middlewares: MiddlewareMetadata[] = [];
classDirectives: DirectiveClassMetadata[] = [];
fieldDirectives: DirectiveFieldMetadata[] = [];
extensions: ExtensionsMetadata[] = [];

private resolverClasses: ResolverClassMetadata[] = [];
private fields: FieldMetadata[] = [];
Expand Down Expand Up @@ -108,6 +110,10 @@ export class MetadataStorage {
this.fieldDirectives.push(definition);
}

collectExtensionsMetadata(definition: ExtensionsMetadata) {
this.extensions.push(definition);
}

build() {
// TODO: disable next build attempts

Expand Down Expand Up @@ -143,6 +149,7 @@ export class MetadataStorage {
this.middlewares = [];
this.classDirectives = [];
this.fieldDirectives = [];
this.extensions = [];

this.resolverClasses = [];
this.fields = [];
Expand Down Expand Up @@ -196,6 +203,7 @@ export class MetadataStorage {
def.directives = this.fieldDirectives
.filter(it => it.target === def.target && it.fieldName === def.methodName)
.map(it => it.directive);
def.extensions = this.findExtensions(def.target, def.methodName);
});
}

Expand All @@ -206,6 +214,7 @@ export class MetadataStorage {
def.directives = this.fieldDirectives
.filter(it => it.target === def.target && it.fieldName === def.methodName)
.map(it => it.directive);
def.extensions = this.findExtensions(def.target, def.methodName);
def.getObjectType =
def.kind === "external"
? this.resolverClasses.find(resolver => resolver.target === def.target)!.getObjectType
Expand Down Expand Up @@ -286,4 +295,10 @@ export class MetadataStorage {
}
return authorizedField.roles;
}

private findExtensions(target: Function, fieldName: string): Record<string, any> {
return this.extensions
.filter(entry => entry.target === target && entry.fieldName === fieldName)
.reduce((extensions, entry) => ({ ...extensions, ...entry.extensions }), {});
}
}
3 changes: 3 additions & 0 deletions src/schema/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export abstract class SchemaGenerator {
deprecationReason: field.deprecationReason,
astNode: getFieldDefinitionNode(field.name, type, field.directives),
extensions: {
...field.extensions,
complexity: field.complexity,
},
};
Expand Down Expand Up @@ -399,6 +400,7 @@ export abstract class SchemaGenerator {
type,
defaultValue: field.typeOptions.defaultValue,
astNode: getInputValueDefinitionNode(field.name, type, field.directives),
extensions: field.extensions,
};
return fieldsMap;
},
Expand Down Expand Up @@ -497,6 +499,7 @@ export abstract class SchemaGenerator {
deprecationReason: handler.deprecationReason,
astNode: getFieldDefinitionNode(handler.schemaName, type, handler.directives),
extensions: {
...handler.extensions,
complexity: handler.complexity,
},
};
Expand Down