UNPKG

@ephys/graphql-joi-directives

Version:

Adds Joi-powered constraint directive for GraphQL

42 lines (41 loc) 1.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildJoiIntDirectiveTypedef = exports.buildJoiIntDirective = void 0; const graphql_1 = require("graphql"); const joi_1 = __importDefault(require("joi")); const _internal_1 = require("./_internal"); class JoiConstrainedInt extends _internal_1.JoiConstrainedScalar { buildJoi(fieldName, args) { let schema = joi_1.default.number().integer().label(fieldName); for (const argKey of Object.keys(args)) { const argVal = args[argKey]; switch (argKey) { case 'min': schema = schema.min(argVal); break; case 'max': schema = schema.max(argVal); break; default: throw new Error(`Unsupported argument ${argKey}.`); } } return schema; } } function buildJoiIntDirective(tag) { return _internal_1.buildJoiDirective(tag, graphql_1.GraphQLInt, JoiConstrainedInt); } exports.buildJoiIntDirective = buildJoiIntDirective; function buildJoiIntDirectiveTypedef(directiveName) { return ` directive @${directiveName}( min: Int, max: Int ) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION `; } exports.buildJoiIntDirectiveTypedef = buildJoiIntDirectiveTypedef;