Skip to content

Commit 6864c51

Browse files
authored
Merge pull request #32 from indigotech/feature/refactor-decorators
Feature - Refactoring more decorators
2 parents 87551ce + a38c86f commit 6864c51

39 files changed

+305
-230
lines changed

src/decorator.ts

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ import 'reflect-metadata';
33
import * as graphql from 'graphql';
44

55
import { GraphQLType } from 'graphql';
6-
import { IoCContainer } from './ioc-container';
76
import { OrderByTypeFactory } from './order-by.type-factory';
87
import { PageInfo } from './page-info.type';
98
import { PaginationResponse } from './pagination.type';
109

1110
export * from './decorator/';
1211
export * from './metadata/options';
1312

14-
export const GQ_QUERY_KEY = 'gq_query';
15-
export const GQ_MUTATION_KEY = 'gq_mutation';
16-
export const GQ_SUBSCRIPTION_KEY = 'gq_subscription';
1713
export const GQ_FIELDS_KEY = 'gq_fields';
1814
export const GQ_DESCRIPTION_KEY = 'gq_description';
1915

@@ -403,82 +399,3 @@ export function Description(body: string) {
403399
} as Function;
404400
}
405401

406-
export function Query(option?: DefaultOption) {
407-
return function (target: any, propertyKey: any) {
408-
if (Reflect.hasMetadata(GQ_QUERY_KEY, target)) {
409-
let metadata = Reflect.getMetadata(GQ_QUERY_KEY, target);
410-
metadata.push(propertyKey);
411-
Reflect.defineMetadata(GQ_QUERY_KEY, metadata, target);
412-
} else {
413-
Reflect.defineMetadata(GQ_QUERY_KEY, [propertyKey], target);
414-
}
415-
416-
if (option) {
417-
// description
418-
if (option.description) {
419-
setDescriptionMetadata(option.description, target, propertyKey);
420-
}
421-
}
422-
423-
} as Function;
424-
}
425-
426-
export function Mutation(option?: DefaultOption) {
427-
return function (target: any, propertyKey: any) {
428-
if (Reflect.hasMetadata(GQ_MUTATION_KEY, target)) {
429-
let metadata = Reflect.getMetadata(GQ_MUTATION_KEY, target);
430-
metadata.push(propertyKey);
431-
Reflect.defineMetadata(GQ_MUTATION_KEY, metadata, target);
432-
} else {
433-
Reflect.defineMetadata(GQ_MUTATION_KEY, [propertyKey], target);
434-
}
435-
436-
if (option) {
437-
// description
438-
if (option.description) {
439-
setDescriptionMetadata(option.description, target, propertyKey);
440-
}
441-
}
442-
443-
} as Function;
444-
}
445-
446-
export function Subscription(option?: DefaultOption) {
447-
return function (target: any, propertyKey: any) {
448-
if (Reflect.hasMetadata(GQ_SUBSCRIPTION_KEY, target)) {
449-
let metadata = Reflect.getMetadata(GQ_SUBSCRIPTION_KEY, target);
450-
metadata.push(propertyKey);
451-
Reflect.defineMetadata(GQ_SUBSCRIPTION_KEY, metadata, target);
452-
} else {
453-
Reflect.defineMetadata(GQ_SUBSCRIPTION_KEY, [propertyKey], target);
454-
}
455-
456-
if (option) {
457-
// description
458-
if (option.description) {
459-
setDescriptionMetadata(option.description, target, propertyKey);
460-
}
461-
}
462-
463-
} as Function;
464-
}
465-
466-
export function Schema(option?: SchemaOption) {
467-
return function (target: Function) {
468-
Reflect.defineMetadata('gq_schema', {}, target);
469-
470-
if (option) {
471-
// description
472-
if (option.description) {
473-
setDescriptionMetadata(option.description, target);
474-
}
475-
}
476-
} as Function;
477-
}
478-
479-
export function UseContainer(container: any) {
480-
return function (target: Function) {
481-
IoCContainer.INSTANCE = container;
482-
};
483-
}
484-
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { QueryOption, MutationOption, SubscriptionOption, EntryTypeOption } from '../metadata/options';
2+
import { EntryType } from '../metadata/args';
3+
import { getMetadataArgsStorage } from '../metadata-builder';
4+
5+
/**
6+
* It is used to create a root {@link GraphQLObjectType} object as a schema Query
7+
* See [GraphQL Documentation - Queries and Mutations]{@http://graphql.org/learn/schema/#the-query-and-mutation-types}
8+
*/
9+
export function Query(option?: QueryOption) {
10+
return entry(EntryType.Query, option);
11+
}
12+
13+
/**
14+
* It is used to create a root {@link GraphQLObjectType} object as a schema Mutation
15+
* See [GraphQL Documentation - Queries and Mutations]{@http://graphql.org/learn/schema/#the-query-and-mutation-types}
16+
*/
17+
export function Mutation(option?: MutationOption) {
18+
return entry(EntryType.Mutation, option);
19+
}
20+
21+
/**
22+
* It is used to create a root {@link GraphQLObjectType} object as a schema Subscription
23+
* See [GraphQL Blog - Subscriptions in GraphQL and Relay]{@http://graphql.org/blog/subscriptions-in-graphql-and-relay/}
24+
*/
25+
export function Subscription(option?: SubscriptionOption) {
26+
return entry(EntryType.Subscription, option);
27+
}
28+
29+
function entry(type: EntryType, option?: EntryTypeOption) {
30+
return function (target: any, propertyKey: any) {
31+
getMetadataArgsStorage().entries.push({
32+
target: target,
33+
name: target.name,
34+
description: option ? option.description : null,
35+
property: propertyKey,
36+
type: type,
37+
});
38+
};
39+
}

src/decorator/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export * from './enum-type.decorator';
22
export * from './union-type.decorator';
33
export * from './object-type.decorator';
4+
export * from './entry-type.decorator';
5+
export * from './schema.decorator';
6+
export * from './use-container.decorator';

src/decorator/schema.decorator.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getMetadataArgsStorage } from '../metadata-builder';
2+
import { SchemaOption } from '../metadata';
3+
4+
/**
5+
* GraphQL Schema entry point
6+
* See [GraphQL Documentation - Schema]{@link http://graphql.org/learn/schema/}
7+
*
8+
* @param option Options for an Schema
9+
*/
10+
export function Schema(option?: SchemaOption) {
11+
return function (target: any) {
12+
getMetadataArgsStorage().schemas.push({
13+
target: target,
14+
name: target.name,
15+
description: option ? option.description : null,
16+
});
17+
} as Function;
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { IoCContainer } from '../ioc-container';
2+
3+
/**
4+
* Sets the IoC container to be used in order to instantiate the decorated class
5+
* @param container a [typedi]{@link https://github.com/pleerock/typedi} container
6+
*/
7+
export function UseContainer(container: any) {
8+
return function (target: Function) {
9+
IoCContainer.INSTANCE = container;
10+
};
11+
}

src/field_type_factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from './decorator';
1212
import { getMetadataArgsStorage } from './metadata-builder';
1313

14-
import { SchemaFactoryError, SchemaFactoryErrorType } from './schema_factory';
14+
import { SchemaFactoryError, SchemaFactoryErrorType } from './type-factory';
1515

1616
import { IoCContainer } from './ioc-container';
1717
import { OrderByTypeFactory } from './order-by.type-factory';

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './decorator';
2-
export { schemaFactory } from './schema_factory';
2+
export { schemaFactory } from './type-factory';
33
export * from './order-by-item';
44
export * from './pagination.type';

src/metadata-builder/metadata-args.storage.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {
22
EnumTypeArg,
33
EnumValueArg,
4-
UnionTypeArgs,
4+
UnionTypeArg,
55
ObjectTypeArg,
6+
EntryTypeArg,
7+
EntryType,
8+
SchemaArg,
69
} from '../metadata/args';
710

811
/**
@@ -20,8 +23,10 @@ export function getMetadataArgsStorage(): MetadataArgsStorage {
2023
export class MetadataArgsStorage {
2124
enums: EnumTypeArg[] = [];
2225
enumValues: EnumValueArg[] = [];
23-
union: UnionTypeArgs[] = [];
26+
union: UnionTypeArg[] = [];
2427
objects: ObjectTypeArg[] = [];
28+
entries: EntryTypeArg[] = [];
29+
schemas: SchemaArg[] = [];
2530

2631
filterEnumsByClass(target: any): EnumTypeArg[] {
2732
return this.enums.filter(item => item.target === target);
@@ -31,12 +36,20 @@ export class MetadataArgsStorage {
3136
return this.enumValues.filter(item => item.target === target.prototype);
3237
}
3338

34-
filterUnionTypeByClass(target: any): UnionTypeArgs[] {
39+
filterUnionTypeByClass(target: any): UnionTypeArg[] {
3540
return this.union.filter(item => item.target === target);
3641
}
3742

3843
filterObjectTypeByClass(target: any): ObjectTypeArg[] {
3944
return this.objects.filter(item => item.target === target);
4045
}
46+
47+
filterEntryTypesByClassAndType(target: any, type: EntryType): EntryTypeArg[] {
48+
return this.entries.filter(item => item.target === target.prototype && item.type === type);
49+
}
50+
51+
filterSchemaByClass(target: any): SchemaArg[] {
52+
return this.schemas.filter(item => item.target === target);
53+
}
4154
}
4255

src/metadata-builder/metadata.builder.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import {
2+
EntryTypeMetadata,
23
EnumTypeMetadata,
34
EnumValueMetadata,
4-
UnionTypeMetadata,
55
ObjectTypeMetadata,
6+
SchemaMetadata,
7+
UnionTypeMetadata,
68
} from '../metadata/types';
79

10+
import { EntryType } from '../metadata/args';
811
import { getMetadataArgsStorage } from './metadata-args.storage';
912

1013
/**
@@ -55,6 +58,28 @@ export class MetadataBuilder {
5558
}));
5659
}
5760

61+
buildEntryTypeMetadata(target: any, type: EntryType): EntryTypeMetadata[] | undefined {
62+
return getMetadataArgsStorage()
63+
.filterEntryTypesByClassAndType(target, type)
64+
.map(queryArg => ({
65+
target: queryArg.target,
66+
name: queryArg.name,
67+
description: queryArg.description,
68+
property: queryArg.property,
69+
isSubscription: type === EntryType.Subscription,
70+
}));
71+
}
72+
73+
buildSchemaMetadata(target: any): SchemaMetadata[] | undefined {
74+
return getMetadataArgsStorage()
75+
.filterSchemaByClass(target)
76+
.map(queryArg => ({
77+
target: queryArg.target,
78+
name: queryArg.name,
79+
description: queryArg.description,
80+
}));
81+
}
82+
5883
protected buildEnumValueMetadata(target: any): EnumValueMetadata[] | undefined {
5984
return getMetadataArgsStorage()
6085
.filterEnumValuesByClass(target)

src/metadata/args/default.arg.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface DefaultArg {
2+
target: any;
3+
name: string;
4+
description?: string;
5+
}

0 commit comments

Comments
 (0)