nestjs-graphql-relay
Version:
@nestjs/graphql + graphql-relay + typeorm
77 lines (68 loc) • 2.48 kB
text/typescript
import { ArgsType, Field, Int } from "@nestjs/graphql";
import {
Min,
Validate,
ValidateIf,
ValidationArguments,
ValidatorConstraint,
ValidatorConstraintInterface,
} from "class-validator";
import * as Relay from "graphql-relay";
import "reflect-metadata";
({ async: false })
export class CannotUseWithout implements ValidatorConstraintInterface {
validate(_: unknown, args: ValidationArguments) {
const object = args.object as Record<string, unknown>;
const required = args.constraints[0] as string;
return object[required] !== undefined;
}
defaultMessage(args: ValidationArguments) {
return `Cannot be used without \`${args.constraints[0]}\`.`;
}
}
({ async: false })
export class CannotUseWith implements ValidatorConstraintInterface {
validate(_: unknown, args: ValidationArguments) {
const object = args.object as Record<string, unknown>;
const result = args.constraints.every((propertyName) => {
return object[propertyName] === undefined;
});
return result;
}
defaultMessage(args: ValidationArguments) {
return `Cannot be used with \`${args.constraints.join("` , `")}\`.`;
}
}
()
export class ConnectionArgs implements Relay.ConnectionArguments {
(() => String, {
nullable: true,
description: "Paginate before opaque cursor",
})
((o) => o.before !== undefined)
(CannotUseWithout, ["last"])
(CannotUseWith, ["after", "first"])
readonly before?: Relay.ConnectionCursor;
(() => String, {
nullable: true,
description: "Paginate after opaque cursor",
})
((o) => o.after !== undefined)
(CannotUseWithout, ["first"])
(CannotUseWith, ["before", "last"])
readonly after?: Relay.ConnectionCursor;
(() => Int, { nullable: true, description: "Paginate first" })
((o) => o.first !== undefined)
(1)
(CannotUseWith, ["before", "last"])
readonly first?: number;
(() => Int, { nullable: true, description: "Paginate last" })
((o) => o.last !== undefined)
// Required `before`. This is a weird corner case.
// We'd have to invert the ordering of query to get the last few items then re-invert it when emitting the results.
// We'll just ignore it for now.
(CannotUseWithout, ["before"])
(CannotUseWith, ["after", "first"])
(1)
readonly last?: number;
}