-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): add plugin, implement the isolated schema generator
- Loading branch information
1 parent
54ccbd8
commit 23fa7d6
Showing
167 changed files
with
5,375 additions
and
540 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,14 @@ | ||
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, | ||
}; | ||
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,35 @@ | ||
import { parse } from 'graphql'; | ||
import { DirectiveParsingError } from '../schema-builder/errors/directive-parsing.error'; | ||
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); | ||
|
||
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,99 @@ | ||
/** | ||
* 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 { 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>, | ||
) => { | ||
const [typeFunc, options = {}] = isFunction(typeOrOptions) | ||
? [typeOrOptions, fieldOptions] | ||
: [undefined, typeOrOptions as any]; | ||
|
||
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, | ||
}); | ||
} | ||
}; | ||
} |
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,5 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
export function HideField(): PropertyDecorator { | ||
return (target: Record<string, any>, propertyKey: string | symbol) => {}; | ||
} |
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.