Skip to content

Commit 46aebb6

Browse files
committed
Fix checking if list input is sortable
Former breaks because it's a function returning a dynamic class. New check uses duck types instead of nominal prototypes.
1 parent a6f0f2a commit 46aebb6

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/common/pagination.input.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PipeTransform, Type } from '@nestjs/common';
22
import { Args, ArgsOptions, Field, InputType, Int } from '@nestjs/graphql';
3+
import { setHas } from '@seedcompany/common';
34
import { Matches, Max, Min } from 'class-validator';
45
import { stripIndent } from 'common-tags';
56
import { DataObject } from './data-object';
@@ -28,6 +29,14 @@ export abstract class PaginationInput extends DataObject {
2829
readonly page: number = 1;
2930
}
3031

32+
export const isPaginationInput = (input: unknown): input is PaginationInput =>
33+
!!input &&
34+
typeof input === 'object' &&
35+
'count' in input &&
36+
typeof input.count === 'number' &&
37+
'page' in input &&
38+
typeof input.page === 'number';
39+
3140
@InputType({
3241
isAbstract: true,
3342
})
@@ -62,6 +71,16 @@ export interface SortablePaginationInput<SortKey extends string = string>
6271
order: Order;
6372
}
6473

74+
export const isSortablePaginationInput = (
75+
input: unknown,
76+
): input is SortablePaginationInput =>
77+
isPaginationInput(input) &&
78+
'sort' in input &&
79+
typeof input.sort === 'string' &&
80+
'order' in input &&
81+
typeof input.order === 'string' &&
82+
setHas(Order.values, input.order);
83+
6584
export const SortablePaginationInput = <SortKey extends string = string>({
6685
defaultSort,
6786
defaultOrder,

src/core/edgedb/dto.repository.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { AbstractClass } from 'type-fest';
1313
import {
1414
EnhancedResource,
1515
ID,
16+
isSortablePaginationInput,
1617
NotFoundException,
1718
PaginatedListType,
1819
PaginationInput,
@@ -198,8 +199,8 @@ export const RepoFor = <
198199
: e.all(e.set(...filters));
199200
return {
200201
...(filter ? { filter } : {}),
201-
...(input instanceof SortablePaginationInput
202-
? { order_by: this.orderBy(obj, input as SortablePaginationInput) }
202+
...(isSortablePaginationInput(input)
203+
? { order_by: this.orderBy(obj, input) }
203204
: {}),
204205
};
205206
});

0 commit comments

Comments
 (0)