Skip to content

Commit 54a4f99

Browse files
authored
Merge pull request #10 from indigotech/feature/order-by-extra-columns
Adding ability to add custom sort params to @orderby params
2 parents d9b7bda + cd1cc14 commit 54a4f99

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

src/decorator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface TypeMetadata {
2626
isPagination?: boolean;
2727
explicitType?: any;
2828
beforeMiddleware?: Middleware;
29+
extraParams?: any;
2930
}
3031

3132
export interface ArgumentMetadata extends TypeMetadata {
@@ -369,10 +370,11 @@ export function Ctx() {
369370
} as Function;
370371
}
371372

372-
export function OrderBy() {
373+
export function OrderBy(extraColumnsToSortBy: string[] = []) {
373374
return function (target: any, propertyKey: any, index: number) {
374375
setArgumentMetadata(target, propertyKey, index, {
375376
name: 'orderBy',
377+
extraParams: extraColumnsToSortBy,
376378
});
377379
} as Function;
378380
}

src/object_type_factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ export function objectTypeFactory(target: Function, isInput?: boolean) {
2424
}
2525
if (!Reflect.hasMetadata(GQ_FIELDS_KEY, target.prototype)) {
2626
// tslint:disable-next-line:max-line-length
27-
throw new SchemaFactoryError('Class annotated by @ObjectType() should has one or more fields annotated by @Filed()', SchemaFactoryErrorType.NO_FIELD);
27+
throw new SchemaFactoryError('Class annotated by @ObjectType() should has one or more fields annotated by @Field()', SchemaFactoryErrorType.NO_FIELD);
2828
}
2929
const fieldMetadataList = Reflect.getMetadata(GQ_FIELDS_KEY, target.prototype) as FieldTypeMetadata[];
3030
const fields: {[key: string]: any} = {};
3131
fieldMetadataList.forEach(def => {
3232
let field = fieldTypeFactory(target, def, isInput);
3333
if (!field) {
3434
// tslint:disable-next-line:max-line-length
35-
throw new SchemaFactoryError(`@ObjectType()'s ${def.name} is annotated by @Filed() but no type could be inferred`, SchemaFactoryErrorType.NO_FIELD);
35+
throw new SchemaFactoryError(`@ObjectType()'s ${def.name} is annotated by @Field() but no type could be inferred`, SchemaFactoryErrorType.NO_FIELD);
3636
}
3737
fields[def.name] = field;
3838
});

src/order-by.type-factory.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ export class OrderByTypeFactory {
6363
orderByFieldArray.push(def);
6464
}
6565
});
66+
67+
if (metadata.args &&
68+
metadata.args.length > 0) {
69+
70+
let sortArg = metadata.args.filter(arg => arg.name === 'orderBy')[0];
71+
if ( sortArg && sortArg.extraParams && sortArg.extraParams.constructor === Array) {
72+
sortArg.extraParams.filter((item: any) => item && item.constructor === String)
73+
.forEach((item: string) => orderByFieldArray.push({
74+
name: item,
75+
description: item,
76+
}));
77+
}
78+
}
6679
let orderBySortEnumObject = OrderByTypeFactory.orderByFieldEnumFactory(metadata.name, orderByFieldArray);
6780
let orderByDirectionEnumObject = OrderByTypeFactory.orderByDirectionEnumFactory(metadata.name);
6881
let orderByInputObject = OrderByTypeFactory.orderByInputObjectFactory(metadata.name, orderBySortEnumObject,

src/schema_factory.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as graphql from 'graphql';
55

66
import { SchemaFactoryError, SchemaFactoryErrorType, schemaFactory } from './schema_factory';
77

8+
import { GraphQLString } from 'graphql';
9+
import { OrderByItem } from './order-by-item';
810
import { clearObjectTypeRepository } from './object_type_factory';
911
import { execute } from 'graphql/execution';
1012
import { parse } from 'graphql/language';
@@ -130,4 +132,37 @@ describe('schemaFactory', function() {
130132
done();
131133
});
132134

135+
describe('Pagination', function() {
136+
137+
it('returns a GraphQL Pagination object with custom @OrberBy fields', async function() {
138+
139+
@D.ObjectType()
140+
class Obj {
141+
@D.Description('a field')
142+
@D.Field({ type: GraphQLString })
143+
aField: string;
144+
}
145+
146+
@D.ObjectType() class Query {
147+
@D.Field({ type: Obj })
148+
@D.Pagination()
149+
async paginate(
150+
@D.OrderBy(['extraField']) orderBy?: OrderByItem[],
151+
): Promise<[Obj, number]> {
152+
return [{ aField: null }, 0];
153+
}
154+
}
155+
@D.Schema() class Schema { @D.Query() query: Query; }
156+
const schema = schemaFactory(Schema);
157+
const ast = parse(`
158+
query {
159+
paginate(orderBy: [{sort: aField, direction: ASC}, {sort: extraField, direction: DESC}]) {
160+
count
161+
}
162+
}`);
163+
assert.deepEqual(validate(schema, ast), []);
164+
});
165+
166+
});
167+
133168
});

0 commit comments

Comments
 (0)