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
4 changes: 2 additions & 2 deletions src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,11 @@ export function Ctx() {
} as Function;
}

export function OrderBy(extraColumnsToSortBy: string[] = []) {
export function OrderBy(params?: {extraColumns: string[], shouldIgnoreSchemaFields?: boolean } | string[]) {
return function (target: any, propertyKey: any, index: number) {
setArgumentMetadata(target, propertyKey, index, {
name: 'orderBy',
extraParams: extraColumnsToSortBy,
extraParams: params.constructor === Array ? { extraColumns: params } : params,
});
} as Function;
}
Expand Down
13 changes: 11 additions & 2 deletions src/order-by.type-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,17 @@ export class OrderByTypeFactory {
metadata.args.length > 0) {

let sortArg = metadata.args.filter(arg => arg.name === 'orderBy')[0];
if ( sortArg && sortArg.extraParams && sortArg.extraParams.constructor === Array) {
sortArg.extraParams.filter((item: any) => item && item.constructor === String)
if ( sortArg &&
sortArg.extraParams &&
sortArg.extraParams.extraColumns &&
sortArg.extraParams.extraColumns.constructor === Array) {

if (sortArg.extraParams.shouldIgnoreSchemaFields) {
// remove all previous items from `orderByFieldArray`
orderByFieldArray.splice(0, Number.POSITIVE_INFINITY);
}

sortArg.extraParams.extraColumns.filter((item: any) => item && item.constructor === String)
.forEach((item: string) => orderByFieldArray.push({
name: item,
description: item,
Expand Down
112 changes: 88 additions & 24 deletions src/schema_factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,98 @@ describe('schemaFactory', function() {
describe('Pagination', function() {

it('returns a GraphQL Pagination object with custom @OrberBy fields', async function() {
@D.ObjectType()
class Obj {
@D.Description('a field')
@D.Field({ type: GraphQLString })
aField: string;
}

@D.ObjectType()
class Obj {
@D.Description('a field')
@D.Field({ type: GraphQLString })
aField: string;
}
@D.ObjectType() class Query {
@D.Field({ type: Obj })
@D.Pagination()
async paginate(
@D.OrderBy({extraColumns: ['extraField']}) orderBy?: OrderByItem[],
): Promise<[Obj, number]> {
return [{ aField: null }, 0];
}
}
@D.Schema() class Schema { @D.Query() query: Query; }
const schema = schemaFactory(Schema);
const ast = parse(`
query {
paginate(orderBy: [{sort: aField, direction: ASC}, {sort: extraField, direction: DESC}]) {
count
}
}`);
assert.deepEqual(validate(schema, ast), []);
});

@D.ObjectType() class Query {
@D.Field({ type: Obj })
@D.Pagination()
async paginate(
@D.OrderBy(['extraField']) orderBy?: OrderByItem[],
): Promise<[Obj, number]> {
return [{ aField: null }, 0];
}
it('returns a GraphQL Pagination object with custom @OrberBy fields (backwards compatibility)', async function() {
@D.ObjectType()
class Obj {
@D.Description('a field')
@D.Field({ type: GraphQLString })
aField: string;
}

@D.ObjectType() class Query {
@D.Field({ type: Obj })
@D.Pagination()
async paginate(
@D.OrderBy(['extraField']) orderBy?: OrderByItem[],
): Promise<[Obj, number]> {
return [{ aField: null }, 0];
}
}
@D.Schema() class Schema { @D.Query() query: Query; }
const schema = schemaFactory(Schema);
const ast = parse(`
query {
paginate(orderBy: [{sort: aField, direction: ASC}, {sort: extraField, direction: DESC}]) {
count
}
}`);
assert.deepEqual(validate(schema, ast), []);
});

it('returns a GraphQL Pagination object with custom @OrberBy fields ignoring schema fields', async function() {

@D.ObjectType()
class Obj {
@D.Description('a field')
@D.Field({ type: GraphQLString })
aField: string;
}

@D.ObjectType() class Query {
@D.Field({ type: Obj })
@D.Pagination()
async paginate(
@D.OrderBy({extraColumns: ['extraField'], shouldIgnoreSchemaFields: true}) orderBy?: OrderByItem[],
): Promise<[Obj, number]> {
return [{ aField: null }, 0];
}
@D.Schema() class Schema { @D.Query() query: Query; }
const schema = schemaFactory(Schema);
const ast = parse(`
query {
paginate(orderBy: [{sort: aField, direction: ASC}, {sort: extraField, direction: DESC}]) {
count
}
}`);
assert.deepEqual(validate(schema, ast), []);
});
}
@D.Schema() class Schema { @D.Query() query: Query; }
const schema = schemaFactory(Schema);
const astToIgnore = parse(`
query {
paginate(orderBy: [{sort: extraField, direction: DESC}]) {
count
}
}`);
const astToError = parse(`
query {
paginate(orderBy: [{sort: aField, direction: ASC}, {sort: extraField, direction: DESC}]) {
count
}
}`);
assert.deepEqual(validate(schema, astToIgnore), [], 'should ignore schema fields');
assert.equal(validate(schema, astToError).length, 1, 'should error if an schema fields is provided');

});

});

});