-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
702bb6a
commit 5b95f12
Showing
20 changed files
with
475 additions
and
414 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { PipeTransform, Type } from '@nestjs/common'; | ||
import { isObject, isString } from '@nestjs/common/utils/shared.utils'; | ||
import * as optional from 'optional'; | ||
import 'reflect-metadata'; | ||
import { GqlParamtype } from '../enums/gql-paramtype.enum'; | ||
import { BasicOptions } from '../external/type-graphql.types'; | ||
import { addPipesMetadata } from './param.utils'; | ||
|
||
const { Arg: TypeGqlArg, Args: TypeGqlArgs } = | ||
optional('type-graphql') || ({} as any); | ||
|
||
export interface ArgsOptions extends BasicOptions { | ||
name?: string; | ||
type: () => any; | ||
} | ||
export function Args(); | ||
export function Args(...pipes: (Type<PipeTransform> | PipeTransform)[]); | ||
export function Args( | ||
property: string, | ||
...pipes: (Type<PipeTransform> | PipeTransform)[] | ||
); | ||
export function Args( | ||
options: ArgsOptions, | ||
...pipes: (Type<PipeTransform> | PipeTransform)[] | ||
); | ||
export function Args( | ||
propertyOrOptions?: | ||
| string | ||
| (Type<PipeTransform> | PipeTransform) | ||
| ArgsOptions, | ||
...pipes: (Type<PipeTransform> | PipeTransform)[] | ||
) { | ||
let typeFn = undefined; | ||
let argOptions = {} as BasicOptions; | ||
let property = propertyOrOptions; | ||
|
||
if (propertyOrOptions && isObject(propertyOrOptions)) { | ||
property = (propertyOrOptions as Record<string, any>).name; | ||
typeFn = (propertyOrOptions as Record<string, any>).type; | ||
argOptions = { | ||
description: (propertyOrOptions as BasicOptions).description, | ||
nullable: (propertyOrOptions as BasicOptions).nullable, | ||
defaultValue: (propertyOrOptions as BasicOptions).defaultValue, | ||
}; | ||
} | ||
|
||
return (target, key, index) => { | ||
addPipesMetadata(GqlParamtype.ARGS, property, pipes, target, key, index); | ||
property && isString(property) | ||
? TypeGqlArg && | ||
TypeGqlArg(property, typeFn, argOptions)(target, key, index) | ||
: TypeGqlArgs && TypeGqlArgs(typeFn)(target, key, index); | ||
}; | ||
} |
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 { PipeTransform, Type } from '@nestjs/common'; | ||
import 'reflect-metadata'; | ||
import { GqlParamtype } from '../enums/gql-paramtype.enum'; | ||
import { createGqlPipesParamDecorator } from './param.utils'; | ||
|
||
export function Context(); | ||
export function Context(...pipes: (Type<PipeTransform> | PipeTransform)[]); | ||
export function Context( | ||
property: string, | ||
...pipes: (Type<PipeTransform> | PipeTransform)[] | ||
); | ||
export function Context( | ||
property?: string | (Type<PipeTransform> | PipeTransform), | ||
...pipes: (Type<PipeTransform> | PipeTransform)[] | ||
) { | ||
return createGqlPipesParamDecorator(GqlParamtype.CONTEXT)(property, ...pipes); | ||
} |
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,3 @@ | ||
import { createDelegateDecorator } from './resolvers.utils'; | ||
|
||
export const DelegateProperty = createDelegateDecorator; |
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,12 @@ | ||
export * from './args.decorator'; | ||
export * from './context.decorator'; | ||
export * from './delegate-property.decorator'; | ||
export * from './info.decorator'; | ||
export * from './mutation.decorator'; | ||
export * from './parent.decorator'; | ||
export * from './query.decorator'; | ||
export * from './resolve-property.decorator'; | ||
export * from './resolver.decorator'; | ||
export * from './root.decorator'; | ||
export * from './scalar.decorator'; | ||
export * from './subscription.decorator'; |
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,8 @@ | ||
import { PipeTransform, Type } from '@nestjs/common'; | ||
import 'reflect-metadata'; | ||
import { GqlParamtype } from '../enums/gql-paramtype.enum'; | ||
import { createGqlPipesParamDecorator } from './param.utils'; | ||
|
||
export function Info(...pipes: (Type<PipeTransform> | PipeTransform)[]) { | ||
return createGqlPipesParamDecorator(GqlParamtype.INFO)(undefined, ...pipes); | ||
} |
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,41 @@ | ||
import { isString } from '@nestjs/common/utils/shared.utils'; | ||
import * as optional from 'optional'; | ||
import { Resolvers } from '../enums/resolvers.enum'; | ||
import { | ||
AdvancedOptions, | ||
ReturnTypeFunc, | ||
} from './../external/type-graphql.types'; | ||
import { addResolverMetadata } from './resolvers.utils'; | ||
|
||
const { Mutation: TypeGqlMutation } = optional('type-graphql') || ({} as any); | ||
|
||
export function Mutation(): MethodDecorator; | ||
export function Mutation(name: string): MethodDecorator; | ||
export function Mutation( | ||
typeFunc: ReturnTypeFunc, | ||
options?: AdvancedOptions, | ||
): MethodDecorator; | ||
export function Mutation( | ||
nameOrType?: string | ReturnTypeFunc, | ||
options?: AdvancedOptions, | ||
): MethodDecorator { | ||
return ( | ||
target: Object | Function, | ||
key?: string | symbol, | ||
descriptor?: any, | ||
) => { | ||
const name = isString(nameOrType) | ||
? nameOrType | ||
: (options && options.name) || undefined; | ||
|
||
addResolverMetadata(Resolvers.MUTATION, name, target, key, descriptor); | ||
if (nameOrType && !isString(nameOrType)) { | ||
TypeGqlMutation && | ||
TypeGqlMutation(nameOrType, options)( | ||
target as Function, | ||
key, | ||
descriptor, | ||
); | ||
} | ||
}; | ||
} |
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,70 @@ | ||
import { PipeTransform, Type } from '@nestjs/common'; | ||
import { isNil, isString } from '@nestjs/common/utils/shared.utils'; | ||
import 'reflect-metadata'; | ||
import { GqlParamtype } from '../enums/gql-paramtype.enum'; | ||
import { PARAM_ARGS_METADATA } from '../graphql.constants'; | ||
|
||
export type ParamData = object | string | number; | ||
export interface ParamsMetadata { | ||
[prop: number]: { | ||
index: number; | ||
data?: ParamData; | ||
}; | ||
} | ||
|
||
const assignMetadata = ( | ||
args: ParamsMetadata, | ||
paramtype: GqlParamtype, | ||
index: number, | ||
data?: ParamData, | ||
...pipes: (Type<PipeTransform> | PipeTransform)[] | ||
) => ({ | ||
...args, | ||
[`${paramtype}:${index}`]: { | ||
index, | ||
data, | ||
pipes, | ||
}, | ||
}); | ||
|
||
export const createGqlParamDecorator = (paramtype: GqlParamtype) => { | ||
return (data?: ParamData): ParameterDecorator => (target, key, index) => { | ||
const args = | ||
Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {}; | ||
Reflect.defineMetadata( | ||
PARAM_ARGS_METADATA, | ||
assignMetadata(args, paramtype, index, data), | ||
target.constructor, | ||
key, | ||
); | ||
}; | ||
}; | ||
|
||
export const addPipesMetadata = ( | ||
paramtype: GqlParamtype, | ||
data: any, | ||
pipes: (Type<PipeTransform> | PipeTransform)[], | ||
target: Object, | ||
key: string | symbol, | ||
index: number, | ||
) => { | ||
const args = | ||
Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {}; | ||
const hasParamData = isNil(data) || isString(data); | ||
const paramData = hasParamData ? data : undefined; | ||
const paramPipes = hasParamData ? pipes : [data, ...pipes]; | ||
|
||
Reflect.defineMetadata( | ||
PARAM_ARGS_METADATA, | ||
assignMetadata(args, paramtype, index, paramData, ...paramPipes), | ||
target.constructor, | ||
key, | ||
); | ||
}; | ||
|
||
export const createGqlPipesParamDecorator = (paramtype: GqlParamtype) => ( | ||
data?, | ||
...pipes: (Type<PipeTransform> | PipeTransform)[] | ||
): ParameterDecorator => (target, key, index) => { | ||
addPipesMetadata(paramtype, data, pipes, target, key, index); | ||
}; |
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,6 @@ | ||
import { GqlParamtype } from '../enums/gql-paramtype.enum'; | ||
import { createGqlParamDecorator } from './param.utils'; | ||
|
||
export const Parent: () => ParameterDecorator = createGqlParamDecorator( | ||
GqlParamtype.ROOT, | ||
); |
Oops, something went wrong.