Skip to content

Commit

Permalink
feat(graphql,#609): Allow disabling maxResultSize
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Mar 17, 2021
1 parent ca7b713 commit a3cd664
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
4 changes: 4 additions & 0 deletions documentation/docs/graphql/dtos.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ By default the max number records that can be returned is `50`.

To override the default you can override the following options specifying the `maxResultSize` option.

:::note
You can disable `maxResultSize` by setting the option to `-1`.
:::

In this example we specify limit the max number of records an end user can request to 20.

```ts title="todo-item.dto.ts" {5}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('QueryArgsType with decorator options', (): void => {
});

describe('max result size', () => {
it('allow validate a maxResultsSize for paging.first', () => {
it('should validate a maxResultsSize for paging.first', () => {
const queryObj: CursorQueryOptionsArgs = {
paging: { first: 10 },
};
Expand All @@ -112,7 +112,7 @@ describe('QueryArgsType with decorator options', (): void => {
]);
});

it('allow validate a maxResultsSize for paging.last', () => {
it('should validate a maxResultsSize for paging.last', () => {
const queryObj: CursorQueryOptionsArgs = {
paging: { last: 10, before: 'abc' },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('Cursor paging strategy QueryArgsType with manual options', (): void =>
return expectSDL([TestCursorQueryManualOptionsResolver], cursorQueryArgsOptionsTypeSDL);
});

it('allow validate a maxResultsSize for paging.first', () => {
it('should validate a maxResultsSize for paging.first', () => {
const queryObj: TestCursorQuery = {
paging: { first: 10 },
};
Expand All @@ -183,7 +183,7 @@ describe('Cursor paging strategy QueryArgsType with manual options', (): void =>
]);
});

it('allow validate a maxResultsSize for paging.last', () => {
it('should validate a maxResultsSize for paging.last', () => {
const queryObj: TestCursorQuery = {
paging: { last: 10, before: 'abc' },
};
Expand All @@ -200,5 +200,19 @@ describe('Cursor paging strategy QueryArgsType with manual options', (): void =>
},
]);
});

it('should ignore a maxResultsSize for paging.first and paging.last if maxResultSize === -1', () => {
class NoMaxQueryArgsTpe extends QueryArgsType(TestDto, { maxResultsSize: -1 }) {}
const queryObjFirst: NoMaxQueryArgsTpe = {
paging: { first: 1000 },
};
expect(validateSync(plainToClass(NoMaxQueryArgsTpe, queryObjFirst))).toEqual([]);

const queryObjLast: NoMaxQueryArgsTpe = {
paging: { last: 1000, before: 'abc' },
};
const queryInstance = plainToClass(NoMaxQueryArgsTpe, queryObjLast);
expect(validateSync(queryInstance)).toEqual([]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Offset paging strategy QueryArgsType with decorator options', (): void
return expectSDL([TestOffsetQueryOptionsDecoratorResolver], offsetQueryArgsDecoratorTypeSDL);
});

it('validate a maxResultsSize for paging.limit', () => {
it('should validate a maxResultsSize for paging.limit', () => {
const queryObj: CursorQueryArgsType<TestDto> = {
paging: { limit: 10 },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('Offset paging strategy QueryArgsType with manual options', (): void =>
return expectSDL([TestOffsetQueryManualOptionsResolver], offsetQueryArgsOptionsTypeSDL);
});

it('validate a maxResultsSize for paging.limit', () => {
it('should validate a maxResultsSize for paging.limit', () => {
const queryObj: CursorQueryArgsType<TestDto> = {
paging: { limit: 10 },
};
Expand All @@ -188,5 +188,16 @@ describe('Offset paging strategy QueryArgsType with manual options', (): void =>
},
]);
});

it('should ignore a maxResultsSize for paging.limit if maxResultSize === -1', () => {
class NoMaxQueryArgsTpe extends QueryArgsType(TestDto, {
pagingStrategy: PagingStrategies.OFFSET,
maxResultsSize: -1,
}) {}
const queryObj: NoMaxQueryArgsTpe = {
paging: { limit: 1000 },
};
expect(validateSync(plainToClass(NoMaxQueryArgsTpe, queryObj))).toEqual([]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments
export class PropertyMax implements ValidatorConstraintInterface {
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types
validate(value: any, args: ValidationArguments): boolean {
const object = args.object as Record<string, Record<string, number>>;
const field = args.constraints[0] as string;
const maxVal = args.constraints[1] as number;
if (maxVal === -1) {
return true;
}
const field = args.constraints[0] as string;
const object = args.object as Record<string, Record<string, number>>;
const prop = object[args.property] ?? {};
return prop[field] !== undefined ? prop[field] <= maxVal : true;
if (prop[field] === undefined) {
return true;
}
return prop[field] <= maxVal;
}

defaultMessage(args: ValidationArguments): string {
Expand Down

0 comments on commit a3cd664

Please sign in to comment.