-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #634 from nestjs/feat/plugin-code-first
feat(): add plugin, implement the isolated schema generator
- Loading branch information
Showing
174 changed files
with
5,561 additions
and
583 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 |
---|---|---|
|
@@ -18,4 +18,5 @@ test-schema.graphql | |
*.test-definitions.ts | ||
|
||
# dist | ||
/lib/src | ||
/dist |
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,17 @@ | ||
import { LazyMetadataStorage } from '../schema-builder/storages/lazy-metadata.storage'; | ||
import { TypeMetadataStorage } from '../schema-builder/storages/type-metadata.storage'; | ||
|
||
/** | ||
* Decorator that marks a class as a resolver arguments type. | ||
*/ | ||
export function ArgsType(): ClassDecorator { | ||
return (target: Function) => { | ||
const metadata = { | ||
name: target.name, | ||
target, | ||
}; | ||
LazyMetadataStorage.store(() => | ||
TypeMetadataStorage.addArgsMetadata(metadata), | ||
); | ||
}; | ||
} |
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 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,38 @@ | ||
import { parse } from 'graphql'; | ||
import { DirectiveParsingError } from '../schema-builder/errors/directive-parsing.error'; | ||
import { LazyMetadataStorage } from '../schema-builder/storages/lazy-metadata.storage'; | ||
import { TypeMetadataStorage } from '../schema-builder/storages/type-metadata.storage'; | ||
|
||
/** | ||
* Adds a directive to specified field, type, or handler. | ||
*/ | ||
export function Directive( | ||
sdl: string, | ||
): MethodDecorator & PropertyDecorator & ClassDecorator { | ||
return (target: Function | Object, key?: string | symbol) => { | ||
validateDirective(sdl); | ||
|
||
LazyMetadataStorage.store(() => { | ||
if (key) { | ||
TypeMetadataStorage.addDirectivePropertyMetadata({ | ||
target: target.constructor, | ||
fieldName: key as string, | ||
sdl, | ||
}); | ||
} else { | ||
TypeMetadataStorage.addDirectiveMetadata({ | ||
target: target as Function, | ||
sdl, | ||
}); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function validateDirective(sdl: string) { | ||
try { | ||
parse(`type String ${sdl}`); | ||
} catch (err) { | ||
throw new DirectiveParsingError(sdl); | ||
} | ||
} |
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,124 @@ | ||
/** | ||
* The API surface of this module has been heavily inspired by the "type-graphql" library (https://github.com/MichalLytek/type-graphql), originally designed & released by Michal Lytek. | ||
* In the v6 major release of NestJS, we introduced the code-first approach as a compatibility layer between this package and the `@nestjs/graphql` module. | ||
* Eventually, our team decided to reimplement all the features from scratch due to a lack of flexibility. | ||
* To avoid numerous breaking changes, the public API is backward-compatible and may resemble "type-graphql". | ||
*/ | ||
|
||
import { isFunction } from '@nestjs/common/utils/shared.utils'; | ||
import { BaseTypeOptions } from '../interfaces/base-type-options.interface'; | ||
import { ReturnTypeFunc } from '../interfaces/return-type-func.interface'; | ||
import { LazyMetadataStorage } from '../schema-builder/storages/lazy-metadata.storage'; | ||
import { TypeMetadataStorage } from '../schema-builder/storages/type-metadata.storage'; | ||
import { reflectTypeFromMetadata } from '../utils/reflection.utilts'; | ||
|
||
/** | ||
* Interface defining options that can be passed to `@InputType()` decorator. | ||
*/ | ||
export interface FieldOptions extends BaseTypeOptions { | ||
/** | ||
* Name of the field. | ||
*/ | ||
name?: string; | ||
/** | ||
* Description of the field. | ||
*/ | ||
description?: string; | ||
/** | ||
* Field deprecation reason (if deprecated). | ||
*/ | ||
deprecationReason?: string; | ||
} | ||
|
||
/** | ||
* @Field() decorator is used to mark a specific class property as a GraphQL field. | ||
* Only properties decorated with this decorator will be defined in the schema. | ||
*/ | ||
export function Field(): PropertyDecorator & MethodDecorator; | ||
/** | ||
* @Field() decorator is used to mark a specific class property as a GraphQL field. | ||
* Only properties decorated with this decorator will be defined in the schema. | ||
*/ | ||
export function Field( | ||
options: FieldOptions, | ||
): PropertyDecorator & MethodDecorator; | ||
/** | ||
* @Field() decorator is used to mark a specific class property as a GraphQL field. | ||
* Only properties decorated with this decorator will be defined in the schema. | ||
*/ | ||
export function Field( | ||
returnTypeFunction?: ReturnTypeFunc, | ||
options?: FieldOptions, | ||
): PropertyDecorator & MethodDecorator; | ||
/** | ||
* @Field() decorator is used to mark a specific class property as a GraphQL field. | ||
* Only properties decorated with this decorator will be defined in the schema. | ||
*/ | ||
export function Field( | ||
typeOrOptions?: ReturnTypeFunc | FieldOptions, | ||
fieldOptions?: FieldOptions, | ||
): PropertyDecorator & MethodDecorator { | ||
return ( | ||
prototype: Object, | ||
propertyKey?: string, | ||
descriptor?: TypedPropertyDescriptor<any>, | ||
) => { | ||
addFieldMetadata( | ||
typeOrOptions, | ||
fieldOptions, | ||
prototype, | ||
propertyKey, | ||
descriptor, | ||
); | ||
}; | ||
} | ||
|
||
export function addFieldMetadata( | ||
typeOrOptions: ReturnTypeFunc | FieldOptions, | ||
fieldOptions: FieldOptions, | ||
prototype: Object, | ||
propertyKey?: string, | ||
descriptor?: TypedPropertyDescriptor<any>, | ||
loadEagerly?: boolean, | ||
) { | ||
const [typeFunc, options = {}] = isFunction(typeOrOptions) | ||
? [typeOrOptions, fieldOptions] | ||
: [undefined, typeOrOptions as any]; | ||
|
||
const applyMetadataFn = () => { | ||
const isResolver = !!descriptor; | ||
const isResolverMethod = !!(descriptor && descriptor.value); | ||
|
||
const { typeFn: getType, options: typeOptions } = reflectTypeFromMetadata({ | ||
metadataKey: isResolverMethod ? 'design:returntype' : 'design:type', | ||
prototype, | ||
propertyKey, | ||
explicitTypeFn: typeFunc as ReturnTypeFunc, | ||
typeOptions: options, | ||
}); | ||
|
||
TypeMetadataStorage.addClassFieldMetadata({ | ||
name: propertyKey, | ||
schemaName: options.name || propertyKey, | ||
typeFn: getType, | ||
options: typeOptions, | ||
target: prototype.constructor, | ||
description: options.description, | ||
deprecationReason: options.deprecationReason, | ||
}); | ||
|
||
if (isResolver) { | ||
TypeMetadataStorage.addResolverPropertyMetadata({ | ||
kind: 'internal', | ||
methodName: propertyKey, | ||
schemaName: options.name || propertyKey, | ||
target: prototype.constructor, | ||
}); | ||
} | ||
}; | ||
if (loadEagerly) { | ||
applyMetadataFn(); | ||
} else { | ||
LazyMetadataStorage.store(applyMetadataFn); | ||
} | ||
} |
Oops, something went wrong.