Skip to content

Commit eea376e

Browse files
authored
Merge pull request #11 from indigotech/feature/orderby-ignore-schema
Feature - @orderby ignore schema fields
2 parents 5abb712 + db5940f commit eea376e

File tree

3 files changed

+101
-28
lines changed

3 files changed

+101
-28
lines changed

src/decorator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,11 @@ export function Ctx() {
370370
} as Function;
371371
}
372372

373-
export function OrderBy(extraColumnsToSortBy: string[] = []) {
373+
export function OrderBy(params?: {extraColumns: string[], shouldIgnoreSchemaFields?: boolean } | string[]) {
374374
return function (target: any, propertyKey: any, index: number) {
375375
setArgumentMetadata(target, propertyKey, index, {
376376
name: 'orderBy',
377-
extraParams: extraColumnsToSortBy,
377+
extraParams: params.constructor === Array ? { extraColumns: params } : params,
378378
});
379379
} as Function;
380380
}

src/order-by.type-factory.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,17 @@ export class OrderByTypeFactory {
6868
metadata.args.length > 0) {
6969

7070
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)
71+
if ( sortArg &&
72+
sortArg.extraParams &&
73+
sortArg.extraParams.extraColumns &&
74+
sortArg.extraParams.extraColumns.constructor === Array) {
75+
76+
if (sortArg.extraParams.shouldIgnoreSchemaFields) {
77+
// remove all previous items from `orderByFieldArray`
78+
orderByFieldArray.splice(0, Number.POSITIVE_INFINITY);
79+
}
80+
81+
sortArg.extraParams.extraColumns.filter((item: any) => item && item.constructor === String)
7382
.forEach((item: string) => orderByFieldArray.push({
7483
name: item,
7584
description: item,

src/schema_factory.spec.ts

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -135,34 +135,98 @@ describe('schemaFactory', function() {
135135
describe('Pagination', function() {
136136

137137
it('returns a GraphQL Pagination object with custom @OrberBy fields', async function() {
138+
@D.ObjectType()
139+
class Obj {
140+
@D.Description('a field')
141+
@D.Field({ type: GraphQLString })
142+
aField: string;
143+
}
138144

139-
@D.ObjectType()
140-
class Obj {
141-
@D.Description('a field')
142-
@D.Field({ type: GraphQLString })
143-
aField: string;
144-
}
145+
@D.ObjectType() class Query {
146+
@D.Field({ type: Obj })
147+
@D.Pagination()
148+
async paginate(
149+
@D.OrderBy({extraColumns: ['extraField']}) orderBy?: OrderByItem[],
150+
): Promise<[Obj, number]> {
151+
return [{ aField: null }, 0];
152+
}
153+
}
154+
@D.Schema() class Schema { @D.Query() query: Query; }
155+
const schema = schemaFactory(Schema);
156+
const ast = parse(`
157+
query {
158+
paginate(orderBy: [{sort: aField, direction: ASC}, {sort: extraField, direction: DESC}]) {
159+
count
160+
}
161+
}`);
162+
assert.deepEqual(validate(schema, ast), []);
163+
});
145164

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-
}
165+
it('returns a GraphQL Pagination object with custom @OrberBy fields (backwards compatibility)', async function() {
166+
@D.ObjectType()
167+
class Obj {
168+
@D.Description('a field')
169+
@D.Field({ type: GraphQLString })
170+
aField: string;
171+
}
172+
173+
@D.ObjectType() class Query {
174+
@D.Field({ type: Obj })
175+
@D.Pagination()
176+
async paginate(
177+
@D.OrderBy(['extraField']) orderBy?: OrderByItem[],
178+
): Promise<[Obj, number]> {
179+
return [{ aField: null }, 0];
180+
}
181+
}
182+
@D.Schema() class Schema { @D.Query() query: Query; }
183+
const schema = schemaFactory(Schema);
184+
const ast = parse(`
185+
query {
186+
paginate(orderBy: [{sort: aField, direction: ASC}, {sort: extraField, direction: DESC}]) {
187+
count
188+
}
189+
}`);
190+
assert.deepEqual(validate(schema, ast), []);
191+
});
192+
193+
it('returns a GraphQL Pagination object with custom @OrberBy fields ignoring schema fields', async function() {
194+
195+
@D.ObjectType()
196+
class Obj {
197+
@D.Description('a field')
198+
@D.Field({ type: GraphQLString })
199+
aField: string;
200+
}
201+
202+
@D.ObjectType() class Query {
203+
@D.Field({ type: Obj })
204+
@D.Pagination()
205+
async paginate(
206+
@D.OrderBy({extraColumns: ['extraField'], shouldIgnoreSchemaFields: true}) orderBy?: OrderByItem[],
207+
): Promise<[Obj, number]> {
208+
return [{ aField: null }, 0];
154209
}
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-
});
210+
}
211+
@D.Schema() class Schema { @D.Query() query: Query; }
212+
const schema = schemaFactory(Schema);
213+
const astToIgnore = parse(`
214+
query {
215+
paginate(orderBy: [{sort: extraField, direction: DESC}]) {
216+
count
217+
}
218+
}`);
219+
const astToError = parse(`
220+
query {
221+
paginate(orderBy: [{sort: aField, direction: ASC}, {sort: extraField, direction: DESC}]) {
222+
count
223+
}
224+
}`);
225+
assert.deepEqual(validate(schema, astToIgnore), [], 'should ignore schema fields');
226+
assert.equal(validate(schema, astToError).length, 1, 'should error if an schema fields is provided');
165227

166228
});
167229

230+
});
231+
168232
});

0 commit comments

Comments
 (0)