Skip to content

Commit

Permalink
chore(): publish 6.0.3 release
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Mar 24, 2019
1 parent 702bb6a commit 5b95f12
Show file tree
Hide file tree
Showing 20 changed files with 475 additions and 414 deletions.
54 changes: 54 additions & 0 deletions lib/decorators/args.decorator.ts
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);
};
}
17 changes: 17 additions & 0 deletions lib/decorators/context.decorator.ts
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);
}
3 changes: 3 additions & 0 deletions lib/decorators/delegate-property.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createDelegateDecorator } from './resolvers.utils';

export const DelegateProperty = createDelegateDecorator;
12 changes: 12 additions & 0 deletions lib/decorators/index.ts
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';
8 changes: 8 additions & 0 deletions lib/decorators/info.decorator.ts
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);
}
41 changes: 41 additions & 0 deletions lib/decorators/mutation.decorator.ts
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,
);
}
};
}
143 changes: 0 additions & 143 deletions lib/decorators/param.decorators.ts

This file was deleted.

70 changes: 70 additions & 0 deletions lib/decorators/param.utils.ts
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);
};
6 changes: 6 additions & 0 deletions lib/decorators/parent.decorator.ts
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,
);
Loading

0 comments on commit 5b95f12

Please sign in to comment.