Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 0 additions & 83 deletions src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ import 'reflect-metadata';
import * as graphql from 'graphql';

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

export * from './decorator/';
export * from './metadata/options';

export const GQ_QUERY_KEY = 'gq_query';
export const GQ_MUTATION_KEY = 'gq_mutation';
export const GQ_SUBSCRIPTION_KEY = 'gq_subscription';
export const GQ_FIELDS_KEY = 'gq_fields';
export const GQ_DESCRIPTION_KEY = 'gq_description';

Expand Down Expand Up @@ -403,82 +399,3 @@ export function Description(body: string) {
} as Function;
}

export function Query(option?: DefaultOption) {
return function (target: any, propertyKey: any) {
if (Reflect.hasMetadata(GQ_QUERY_KEY, target)) {
let metadata = Reflect.getMetadata(GQ_QUERY_KEY, target);
metadata.push(propertyKey);
Reflect.defineMetadata(GQ_QUERY_KEY, metadata, target);
} else {
Reflect.defineMetadata(GQ_QUERY_KEY, [propertyKey], target);
}

if (option) {
// description
if (option.description) {
setDescriptionMetadata(option.description, target, propertyKey);
}
}

} as Function;
}

export function Mutation(option?: DefaultOption) {
return function (target: any, propertyKey: any) {
if (Reflect.hasMetadata(GQ_MUTATION_KEY, target)) {
let metadata = Reflect.getMetadata(GQ_MUTATION_KEY, target);
metadata.push(propertyKey);
Reflect.defineMetadata(GQ_MUTATION_KEY, metadata, target);
} else {
Reflect.defineMetadata(GQ_MUTATION_KEY, [propertyKey], target);
}

if (option) {
// description
if (option.description) {
setDescriptionMetadata(option.description, target, propertyKey);
}
}

} as Function;
}

export function Subscription(option?: DefaultOption) {
return function (target: any, propertyKey: any) {
if (Reflect.hasMetadata(GQ_SUBSCRIPTION_KEY, target)) {
let metadata = Reflect.getMetadata(GQ_SUBSCRIPTION_KEY, target);
metadata.push(propertyKey);
Reflect.defineMetadata(GQ_SUBSCRIPTION_KEY, metadata, target);
} else {
Reflect.defineMetadata(GQ_SUBSCRIPTION_KEY, [propertyKey], target);
}

if (option) {
// description
if (option.description) {
setDescriptionMetadata(option.description, target, propertyKey);
}
}

} as Function;
}

export function Schema(option?: SchemaOption) {
return function (target: Function) {
Reflect.defineMetadata('gq_schema', {}, target);

if (option) {
// description
if (option.description) {
setDescriptionMetadata(option.description, target);
}
}
} as Function;
}

export function UseContainer(container: any) {
return function (target: Function) {
IoCContainer.INSTANCE = container;
};
}

39 changes: 39 additions & 0 deletions src/decorator/entry-type.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { QueryOption, MutationOption, SubscriptionOption, EntryTypeOption } from '../metadata/options';
import { EntryType } from '../metadata/args';
import { getMetadataArgsStorage } from '../metadata-builder';

/**
* It is used to create a root {@link GraphQLObjectType} object as a schema Query
* See [GraphQL Documentation - Queries and Mutations]{@http://graphql.org/learn/schema/#the-query-and-mutation-types}
*/
export function Query(option?: QueryOption) {
return entry(EntryType.Query, option);
}

/**
* It is used to create a root {@link GraphQLObjectType} object as a schema Mutation
* See [GraphQL Documentation - Queries and Mutations]{@http://graphql.org/learn/schema/#the-query-and-mutation-types}
*/
export function Mutation(option?: MutationOption) {
return entry(EntryType.Mutation, option);
}

/**
* It is used to create a root {@link GraphQLObjectType} object as a schema Subscription
* See [GraphQL Blog - Subscriptions in GraphQL and Relay]{@http://graphql.org/blog/subscriptions-in-graphql-and-relay/}
*/
export function Subscription(option?: SubscriptionOption) {
return entry(EntryType.Subscription, option);
}

function entry(type: EntryType, option?: EntryTypeOption) {
return function (target: any, propertyKey: any) {
getMetadataArgsStorage().entries.push({
target: target,
name: target.name,
description: option ? option.description : null,
property: propertyKey,
type: type,
});
};
}
3 changes: 3 additions & 0 deletions src/decorator/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from './enum-type.decorator';
export * from './union-type.decorator';
export * from './object-type.decorator';
export * from './entry-type.decorator';
export * from './schema.decorator';
export * from './use-container.decorator';
18 changes: 18 additions & 0 deletions src/decorator/schema.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getMetadataArgsStorage } from '../metadata-builder';
import { SchemaOption } from '../metadata';

/**
* GraphQL Schema entry point
* See [GraphQL Documentation - Schema]{@link http://graphql.org/learn/schema/}
*
* @param option Options for an Schema
*/
export function Schema(option?: SchemaOption) {
return function (target: any) {
getMetadataArgsStorage().schemas.push({
target: target,
name: target.name,
description: option ? option.description : null,
});
} as Function;
}
11 changes: 11 additions & 0 deletions src/decorator/use-container.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IoCContainer } from '../ioc-container';

/**
* Sets the IoC container to be used in order to instantiate the decorated class
* @param container a [typedi]{@link https://github.com/pleerock/typedi} container
*/
export function UseContainer(container: any) {
return function (target: Function) {
IoCContainer.INSTANCE = container;
};
}
2 changes: 1 addition & 1 deletion src/field_type_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from './decorator';
import { getMetadataArgsStorage } from './metadata-builder';

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

import { IoCContainer } from './ioc-container';
import { OrderByTypeFactory } from './order-by.type-factory';
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './decorator';
export { schemaFactory } from './schema_factory';
export { schemaFactory } from './type-factory';
export * from './order-by-item';
export * from './pagination.type';
19 changes: 16 additions & 3 deletions src/metadata-builder/metadata-args.storage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {
EnumTypeArg,
EnumValueArg,
UnionTypeArgs,
UnionTypeArg,
ObjectTypeArg,
EntryTypeArg,
EntryType,
SchemaArg,
} from '../metadata/args';

/**
Expand All @@ -20,8 +23,10 @@ export function getMetadataArgsStorage(): MetadataArgsStorage {
export class MetadataArgsStorage {
enums: EnumTypeArg[] = [];
enumValues: EnumValueArg[] = [];
union: UnionTypeArgs[] = [];
union: UnionTypeArg[] = [];
objects: ObjectTypeArg[] = [];
entries: EntryTypeArg[] = [];
schemas: SchemaArg[] = [];

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

filterUnionTypeByClass(target: any): UnionTypeArgs[] {
filterUnionTypeByClass(target: any): UnionTypeArg[] {
return this.union.filter(item => item.target === target);
}

filterObjectTypeByClass(target: any): ObjectTypeArg[] {
return this.objects.filter(item => item.target === target);
}

filterEntryTypesByClassAndType(target: any, type: EntryType): EntryTypeArg[] {
return this.entries.filter(item => item.target === target.prototype && item.type === type);
}

filterSchemaByClass(target: any): SchemaArg[] {
return this.schemas.filter(item => item.target === target);
}
}

27 changes: 26 additions & 1 deletion src/metadata-builder/metadata.builder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
EntryTypeMetadata,
EnumTypeMetadata,
EnumValueMetadata,
UnionTypeMetadata,
ObjectTypeMetadata,
SchemaMetadata,
UnionTypeMetadata,
} from '../metadata/types';

import { EntryType } from '../metadata/args';
import { getMetadataArgsStorage } from './metadata-args.storage';

/**
Expand Down Expand Up @@ -55,6 +58,28 @@ export class MetadataBuilder {
}));
}

buildEntryTypeMetadata(target: any, type: EntryType): EntryTypeMetadata[] | undefined {
return getMetadataArgsStorage()
.filterEntryTypesByClassAndType(target, type)
.map(queryArg => ({
target: queryArg.target,
name: queryArg.name,
description: queryArg.description,
property: queryArg.property,
isSubscription: type === EntryType.Subscription,
}));
}

buildSchemaMetadata(target: any): SchemaMetadata[] | undefined {
return getMetadataArgsStorage()
.filterSchemaByClass(target)
.map(queryArg => ({
target: queryArg.target,
name: queryArg.name,
description: queryArg.description,
}));
}

protected buildEnumValueMetadata(target: any): EnumValueMetadata[] | undefined {
return getMetadataArgsStorage()
.filterEnumValuesByClass(target)
Expand Down
5 changes: 5 additions & 0 deletions src/metadata/args/default.arg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface DefaultArg {
target: any;
name: string;
description?: string;
}
12 changes: 12 additions & 0 deletions src/metadata/args/entry-type.arg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DefaultArg } from './default.arg';

export enum EntryType {
Query = <any>'Query',
Mutation = <any>'Mutation',
Subscription = <any>'Subscription',
}

export interface EntryTypeArg extends DefaultArg {
type: EntryType;
property: string;
}
13 changes: 4 additions & 9 deletions src/metadata/args/enum-type.arg.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
export interface EnumTypeArg {
target: any;
name: string;
description?: string;
}
import { DefaultArg } from './default.arg';

export interface EnumTypeArg extends DefaultArg { }

export interface EnumValueArg {
target: any;
name: string;
description?: string;
export interface EnumValueArg extends DefaultArg {
value: any;
}

2 changes: 2 additions & 0 deletions src/metadata/args/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './enum-type.arg';
export * from './union-type.arg';
export * from './object-type.arg';
export * from './entry-type.arg';
export * from './schema.arg';
7 changes: 3 additions & 4 deletions src/metadata/args/object-type.arg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface ObjectTypeArg {
target: any;
name: string;
description?: string;
import { DefaultArg } from './default.arg';

export interface ObjectTypeArg extends DefaultArg {
isInput: boolean;
}
3 changes: 3 additions & 0 deletions src/metadata/args/schema.arg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DefaultArg } from './default.arg';

export interface SchemaArg extends DefaultArg { }
7 changes: 3 additions & 4 deletions src/metadata/args/union-type.arg.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export interface UnionTypeArgs {
target: any;
name: string;
import { DefaultArg } from './default.arg';

export interface UnionTypeArg extends DefaultArg {
types: any[];
description?: string;
resolver: (obj: any, context: any, info: any) => Promise<string> | string | null;
}
6 changes: 6 additions & 0 deletions src/metadata/options/default.option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface DefaultOption {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default or common? which one is preferable?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about only Option, Metadata and Arg instead of DefaultOption, DefaultMetadata and DefaultArg?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some progress in other refactoring and will do this in my upcoming refactor PR

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Done at ea05219

/**
* (Optional) Description
*/
description?: string;
}
18 changes: 18 additions & 0 deletions src/metadata/options/entry.option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DefaultOption } from './default.option';

export interface EntryTypeOption extends DefaultOption { }

/**
* Arguments for a {@link Query} type on graphql schema
*/
export interface QueryOption extends EntryTypeOption { }

/**
* Arguments for a {@link Mutation} type on graphql schema
*/
export interface MutationOption extends EntryTypeOption { }

/**
* Arguments for a {@link Subscription} type on graphql schema
*/
export interface SubscriptionOption extends EntryTypeOption { }
12 changes: 4 additions & 8 deletions src/metadata/options/enum.option.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { DefaultOption } from './default.option';

/**
* Arguments for an Enum type on graphql schema
*/
export interface EnumOption {
/**
* (Optional) Description
*/
description?: string;
}
export interface EnumOption extends DefaultOption { }

/**
* Arguments for an Enum value on graphql schema
*/
export interface EnumValueOption extends EnumOption {
}
export interface EnumValueOption extends DefaultOption { }
Loading