This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '9-implement-field-decorators' into 'master'
Resolve "Implement Field Decorators" Closes #9 and #10 See merge request Mando75/typeorm-graphql-loader!17
- Loading branch information
Showing
15 changed files
with
617 additions
and
53 deletions.
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
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,98 @@ | ||
import "reflect-metadata"; | ||
import { LoaderFieldConfiguration } from "./types"; | ||
|
||
/** | ||
* Internal keys for mapping entity metadata | ||
* @hidden | ||
*/ | ||
const keys = { | ||
IGNORE_FIELD: Symbol("gqlLoader:ignoreField"), | ||
REQUIRED_FIELD: Symbol("gqlLoader:requiredField") | ||
}; | ||
|
||
/** | ||
* Default args | ||
* @hidden | ||
*/ | ||
const defaultLoaderFieldConfiguration: LoaderFieldConfiguration = { | ||
ignore: false, | ||
required: false | ||
}; | ||
|
||
/** | ||
* An experimental decorator that can be used | ||
* to annotate fields or relations in your TypeORM entities | ||
* and customize the loader resolution logic. | ||
* | ||
* The decorator implementation is still being developed | ||
* and the API may change in the future prior to a 2.0 release. | ||
* | ||
* @example | ||
* ```typescript | ||
* @Entity() | ||
* class Author extends BaseEntity { | ||
* | ||
* // This relation will never be fetched by the dataloader | ||
* @ConfigureLoader({ignore: true}) | ||
* @OneToMany() | ||
* books: [Book] | ||
* | ||
* // This relation will always be fetched by the dataloader | ||
* @ConfigureLoader({required: true}) | ||
* @OneToOne() | ||
* user: User | ||
* } | ||
* ``` | ||
* | ||
* @param options | ||
*/ | ||
export const ConfigureLoader = ( | ||
options: LoaderFieldConfiguration = defaultLoaderFieldConfiguration | ||
) => { | ||
const { required, ignore } = { | ||
...defaultLoaderFieldConfiguration, | ||
...options | ||
}; | ||
|
||
return (target: any, propertyKey: string) => { | ||
const ignoreSettings: Map<string, boolean | undefined> = | ||
Reflect.getMetadata(keys.IGNORE_FIELD, target.constructor) ?? new Map(); | ||
ignoreSettings.set(propertyKey, ignore); | ||
Reflect.defineMetadata( | ||
keys.IGNORE_FIELD, | ||
ignoreSettings, | ||
target.constructor | ||
); | ||
|
||
const requiredSettings: Map<string, boolean | undefined> = | ||
Reflect.getMetadata(keys.REQUIRED_FIELD, target.constructor) ?? new Map(); | ||
requiredSettings.set(propertyKey, required); | ||
Reflect.defineMetadata( | ||
keys.REQUIRED_FIELD, | ||
requiredSettings, | ||
target.constructor | ||
); | ||
}; | ||
}; | ||
|
||
/** | ||
* Fetch the required fields from entity metadata | ||
* @hidden | ||
* @param target | ||
*/ | ||
export const getLoaderRequiredFields = ( | ||
target: any | ||
): Map<string, boolean | undefined> => { | ||
return Reflect.getMetadata(keys.REQUIRED_FIELD, target) ?? new Map(); | ||
}; | ||
|
||
/** | ||
* Fetch the ignored fields from entity metadata | ||
* @hidden | ||
* @param target | ||
*/ | ||
export const getLoaderIgnoredFields = ( | ||
target: any | ||
): Map<string, boolean | undefined> => { | ||
return Reflect.getMetadata(keys.IGNORE_FIELD, target) ?? new Map(); | ||
}; |
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
Oops, something went wrong.