This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
forked from MichalLytek/type-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Add Extensions metadata Decorator #1
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,28 @@ | ||
import { MethodAndPropDecorator } from "./types"; | ||
import { SymbolKeysNotSupportedError } from "../errors"; | ||
import { getMetadataStorage } from "../metadata/getMetadataStorage"; | ||
|
||
export function Extensions( | ||
extensions: Record<string, any>, | ||
): MethodAndPropDecorator & ClassDecorator; | ||
export function Extensions( | ||
extensions: Record<string, any>, | ||
): MethodDecorator | PropertyDecorator | ClassDecorator { | ||
return (targetOrPrototype, propertyKey, descriptor) => { | ||
if (typeof propertyKey === "symbol") { | ||
throw new SymbolKeysNotSupportedError(); | ||
} | ||
if (propertyKey) { | ||
getMetadataStorage().collectExtensionsFieldMetadata({ | ||
target: targetOrPrototype.constructor, | ||
fieldName: propertyKey, | ||
extensions, | ||
}); | ||
} else { | ||
getMetadataStorage().collectExtensionsClassMetadata({ | ||
target: targetOrPrototype as Function, | ||
extensions, | ||
}); | ||
} | ||
}; | ||
} |
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,10 @@ | ||
export interface ExtensionsClassMetadata { | ||
target: Function; | ||
extensions: Record<string, any>; | ||
} | ||
|
||
export interface ExtensionsFieldMetadata { | ||
target: Function; | ||
fieldName: string; | ||
extensions: Record<string, any>; | ||
} |
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 |
---|---|---|
|
@@ -3,6 +3,8 @@ import { | |
BaseResolverMetadata, | ||
MiddlewareMetadata, | ||
FieldResolverMetadata, | ||
ExtensionsClassMetadata, | ||
ExtensionsFieldMetadata, | ||
} from "./definitions"; | ||
import { Middleware } from "../interfaces/Middleware"; | ||
import { isThrowing } from "../helpers/isThrowing"; | ||
|
@@ -57,3 +59,10 @@ export function ensureReflectMetadataExists() { | |
throw new ReflectMetadataMissingError(); | ||
} | ||
} | ||
|
||
export function flattenExtensions( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not such a fan of having a "util" just for this but I didn't see a proper alternative. I could make it more generic by accepting a key by which we would flatten instead of hardcoding |
||
extensions: Record<string, any>, | ||
entry: ExtensionsClassMetadata | ExtensionsFieldMetadata, | ||
): Record<string, any> { | ||
return { ...extensions, ...entry.extensions }; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary or else the first decorator declared will have priority, e.g.:
would end up with the object having
duplicate: "one"
, it seems counter intuitive to me and I think this is why it was done in the "directives" feature in the same way. I would have added a code comment but since there was none in the directives PR, I'm not sure it would be appreciated. Will ask the question to the maintainer