Skip to content

Commit

Permalink
Adding functional tests and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
felipesabino committed Nov 3, 2017
1 parent 3f012fb commit 25b3235
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 44 deletions.
4 changes: 2 additions & 2 deletions examples/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
},
"dependencies": {
"graphql": "0.11.7",
"graphql-schema-decorator": "file:../../",
"typescript": "2.3.4"
"graphql-schema-decorator": "file:../../"
},
"keywords": [],
"author": "Quramy",
Expand All @@ -20,6 +19,7 @@
},
"license": "ISC",
"devDependencies": {
"typescript": "2.3.4",
"@types/node": "7.0.5"
}
}
5 changes: 5 additions & 0 deletions src/decorator/field.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SchemaFactoryError, SchemaFactoryErrorType } from '../type-factory';

import { FieldOption } from '../metadata';
import { PaginationMiddleware } from '../pagination.middleware';
import { getMetadataArgsStorage } from '../metadata-builder';

/**
Expand All @@ -20,10 +21,14 @@ export function Field(option?: FieldOption) {
target: target,
name: propertyKey,
description: option ? option.description : null,
property: propertyKey,
type: option ? option.type : null,
nonNull: option ? option.nonNull : null,
isList: option ? option.isList : null,
pagination: option ? option.pagination : null,
});
if (option && option.pagination) {
return PaginationMiddleware(target, propertyKey, methodDescriptor);
}
} as Function;
}
20 changes: 10 additions & 10 deletions src/metadata-builder/metadata-args.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,24 @@ export class MetadataArgsStorage {
return this.fields.filter(item => item.target === target);
}

filterArgumentByClass(target: any): ArgumentArg[] {
return this.arguments.filter(item => item.target === target);
filterArgumentByClassAndProperty(target: any, property: string): ArgumentArg[] {
return this.arguments.filter(item => item.target === target && item.property === property);
}

filterContextByClass(target: any): ContextArg[] {
return this.contexts.filter(item => item.target === target);
filterContextByClassAndProperty(target: any, property: string): ContextArg[] {
return this.contexts.filter(item => item.target === target && item.property === property);
}

filterRootByClass(target: any): RootArg[] {
return this.roots.filter(item => item.target === target);
filterRootByClassAndProperty(target: any, property: string): RootArg[] {
return this.roots.filter(item => item.target === target && item.property === property);
}

filterOrderByByClass(target: any): OrderByArg[] {
return this.orderBys.filter(item => item.target === target);
filterOrderByByClassAndProperty(target: any, property: string): OrderByArg[] {
return this.orderBys.filter(item => item.target === target && item.property === property);
}

filterBeforeByByClass(target: any): BeforeArg[] {
return this.befores.filter(item => item.target === target);
filterBeforeByByClassAndProperty(target: any, property: string): BeforeArg[] {
return this.befores.filter(item => item.target === target && item.property === property);
}
}

31 changes: 16 additions & 15 deletions src/metadata-builder/metadata.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,18 @@ export class MetadataBuilder {
isNonNull: !!arg.nonNull,
isList: !!arg.isList,
isPagination: !!arg.pagination,
arguments: this.buildArgumentMetadata(target),
context: this.buildContextMetadata(target),
root: this.buildRootMetadata(target),
orderBy: this.buildOrderByMetadata(target),
before: this.buildBeforeMetadata(target),
property: arg.property,
arguments: this.buildArgumentMetadata(target, arg.property),
context: this.buildContextMetadata(target, arg.property),
root: this.buildRootMetadata(target, arg.property),
orderBy: this.buildOrderByMetadata(target, arg.property),
before: this.buildBeforeMetadata(target, arg.property),
}));
}

protected buildArgumentMetadata(target: any): ArgumentMetadata[] | undefined {
protected buildArgumentMetadata(target: any, property: string): ArgumentMetadata[] | undefined {
return getMetadataArgsStorage()
.filterArgumentByClass(target)
.filterArgumentByClassAndProperty(target, property)
.map(arg => ({
type: arg.type,
target: arg.target,
Expand All @@ -120,9 +121,9 @@ export class MetadataBuilder {
}));
}

protected buildContextMetadata(target: any): ContextMetadata | undefined {
protected buildContextMetadata(target: any, property: string): ContextMetadata | undefined {
return getMetadataArgsStorage()
.filterContextByClass(target)
.filterContextByClassAndProperty(target, property)
.map(arg => ({
target: arg.target,
name: arg.name,
Expand All @@ -133,9 +134,9 @@ export class MetadataBuilder {
.find((_, index) => index === 0);
}

protected buildRootMetadata(target: any): RootMetadata | undefined {
protected buildRootMetadata(target: any, property: string): RootMetadata | undefined {
return getMetadataArgsStorage()
.filterRootByClass(target)
.filterRootByClassAndProperty(target, property)
.map(arg => ({
target: arg.target,
name: arg.name,
Expand All @@ -146,9 +147,9 @@ export class MetadataBuilder {
.find((_, index) => index === 0);
}

protected buildOrderByMetadata(target: any): OrderByMetadata | undefined {
protected buildOrderByMetadata(target: any, property: string): OrderByMetadata | undefined {
return getMetadataArgsStorage()
.filterOrderByByClass(target)
.filterOrderByByClassAndProperty(target, property)
.map(arg => ({
target: arg.target,
name: arg.name,
Expand All @@ -163,9 +164,9 @@ export class MetadataBuilder {
.find((_, index) => index === 0);
}

protected buildBeforeMetadata(target: any): BeforeMetadata | undefined {
protected buildBeforeMetadata(target: any, property: string): BeforeMetadata | undefined {
return getMetadataArgsStorage()
.filterBeforeByByClass(target)
.filterBeforeByByClassAndProperty(target, property)
.map(arg => ({
target: arg.target,
name: arg.name,
Expand Down
1 change: 1 addition & 0 deletions src/metadata/args/field.arg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface FieldArg extends Argument {
nonNull?: boolean;
isList?: boolean;
pagination?: boolean;
property: string;
}
1 change: 1 addition & 0 deletions src/metadata/types/field.metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface FieldMetadata extends Metadata {
isNonNull: boolean;
isList: boolean;
isPagination: boolean;
property: string;
arguments: ArgumentMetadata[];
context?: ContextMetadata;
root?: RootMetadata;
Expand Down
27 changes: 27 additions & 0 deletions src/pagination.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'reflect-metadata';

import { FieldMetadata } from './metadata';
import { PageInfo } from './page-info.type';
import { PaginationResponse } from './pagination.type';
import { getMetadataBuilder } from './metadata-builder';

export function PaginationMiddleware(target: any, propertyKey: string, methodDescriptor: TypedPropertyDescriptor<any>): any {
return {
value: async function (...args: any[]) {
let [data, count] = await methodDescriptor.value.apply(this, args);

return getMetadataBuilder().buildFieldMetadata(target)
.filter(metadata => metadata.property === propertyKey)
.map(metadata => metadata.arguments)
.map(fieldArguments => {
const paginationValues = fieldArguments
.reduce((indexMap, metadata) => {
indexMap[metadata.name] = metadata.index;
return indexMap;
}, {} as { [name: string]: number; });
return new PaginationResponse(count, data, new PageInfo(count, paginationValues['offset'], paginationValues['limit']));
})
.find((_, index) => index === 0);
},
};
}
10 changes: 6 additions & 4 deletions src/pagination.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as graphql from 'graphql';

import { PageInfo, PageInfoType } from './page-info.type';

import { OrderByItem } from './order-by-item';
import { PageInfoType, PageInfo } from './page-info.type';

export class PaginationResponse<T> {

Expand All @@ -25,8 +27,8 @@ export class PaginationType {
public static build(name: string, type: any): any {

return new graphql.GraphQLObjectType({
name: name + 'Connection',
description: 'Connecton object that connects ' + name + ' to the server',
name: `${name}Connection`,
description: `Connecton object that connects '${name}' to the server`,
fields: function () {
return {
count: {
Expand All @@ -39,7 +41,7 @@ export class PaginationType {
},
pageInfo: {
type: PageInfoType,
description: 'Page information',
description: 'Pagination information',
},
};
},
Expand Down
Loading

0 comments on commit 25b3235

Please sign in to comment.