UNPKG

graphql-scalars

Version:

A collection of scalar types not included in base GraphQL.

35 lines 1.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const definition_1 = require("graphql/type/definition"); const error_1 = require("graphql/error"); const language_1 = require("graphql/language"); const validate = (value) => { const parsed = typeof value === 'string' ? parseInt(value, 10) : value; if (typeof parsed !== 'number' || Number.isNaN(parsed)) { throw new TypeError(`Value is not a number: ${value}`); } if (parsed === Infinity || parsed === -Infinity) { throw new TypeError(`Value is not a finite number: ${value}`); } if (parsed <= 0 || parsed > 65535) { throw new TypeError(`Value is not a valid TCP port: ${value}`); } return parsed; }; exports.default = new definition_1.GraphQLScalarType({ name: `Port`, description: `A field whose value is a valid TCP port within the range of 0 to 65535: https://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_ports`, serialize(value) { return validate(value); }, parseValue(value) { return validate(value); }, parseLiteral(ast) { if (ast.kind !== language_1.Kind.INT) { throw new error_1.GraphQLError(`Can only validate integers as TCP ports but got a: ${ast.kind}`); } return validate(ast.value); } }); //# sourceMappingURL=Port.js.map