graphql
Version:
A Query Language and Runtime which can target any service.
255 lines (198 loc) • 8.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isSpecifiedScalarType = isSpecifiedScalarType;
exports.specifiedScalarTypes = exports.GraphQLID = exports.GraphQLBoolean = exports.GraphQLString = exports.GraphQLFloat = exports.GraphQLInt = void 0;
var _isFinite = _interopRequireDefault(require("../polyfills/isFinite"));
var _isInteger = _interopRequireDefault(require("../polyfills/isInteger"));
var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
var _definition = require("./definition");
var _kinds = require("../language/kinds");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
// As per the GraphQL Spec, Integers are only treated as valid when a valid
// 32-bit signed integer, providing the broadest support across platforms.
//
// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because
// they are internally represented as IEEE 754 doubles.
var MAX_INT = 2147483647;
var MIN_INT = -2147483648;
function serializeInt(value) {
if (typeof value === 'boolean') {
return value ? 1 : 0;
}
var num = value;
if (typeof value === 'string' && value !== '') {
num = Number(value);
}
if (!(0, _isInteger.default)(num)) {
throw new TypeError("Int cannot represent non-integer value: ".concat((0, _inspect.default)(value)));
}
if (num > MAX_INT || num < MIN_INT) {
throw new TypeError("Int cannot represent non 32-bit signed integer value: ".concat((0, _inspect.default)(value)));
}
return num;
}
function coerceInt(value) {
if (!(0, _isInteger.default)(value)) {
throw new TypeError("Int cannot represent non-integer value: ".concat((0, _inspect.default)(value)));
}
if (value > MAX_INT || value < MIN_INT) {
throw new TypeError("Int cannot represent non 32-bit signed integer value: ".concat((0, _inspect.default)(value)));
}
return value;
}
var GraphQLInt = new _definition.GraphQLScalarType({
name: 'Int',
description: 'The `Int` scalar type represents non-fractional signed whole numeric ' + 'values. Int can represent values between -(2^31) and 2^31 - 1. ',
serialize: serializeInt,
parseValue: coerceInt,
parseLiteral: function parseLiteral(ast) {
if (ast.kind === _kinds.Kind.INT) {
var num = parseInt(ast.value, 10);
if (num <= MAX_INT && num >= MIN_INT) {
return num;
}
}
return undefined;
}
});
exports.GraphQLInt = GraphQLInt;
function serializeFloat(value) {
if (typeof value === 'boolean') {
return value ? 1 : 0;
}
var num = value;
if (typeof value === 'string' && value !== '') {
num = Number(value);
}
if (!(0, _isFinite.default)(num)) {
throw new TypeError("Float cannot represent non numeric value: ".concat((0, _inspect.default)(value)));
}
return num;
}
function coerceFloat(value) {
if (!(0, _isFinite.default)(value)) {
throw new TypeError("Float cannot represent non numeric value: ".concat((0, _inspect.default)(value)));
}
return value;
}
var GraphQLFloat = new _definition.GraphQLScalarType({
name: 'Float',
description: 'The `Float` scalar type represents signed double-precision fractional ' + 'values as specified by ' + '[IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point). ',
serialize: serializeFloat,
parseValue: coerceFloat,
parseLiteral: function parseLiteral(ast) {
return ast.kind === _kinds.Kind.FLOAT || ast.kind === _kinds.Kind.INT ? parseFloat(ast.value) : undefined;
}
}); // Support serializing objects with custom valueOf() or toJSON() functions -
// a common way to represent a complex value which can be represented as
// a string (ex: MongoDB id objects).
exports.GraphQLFloat = GraphQLFloat;
function serializeObject(value) {
if (_typeof(value) === 'object' && value !== null) {
if (typeof value.valueOf === 'function') {
var valueOfResult = value.valueOf();
if (_typeof(valueOfResult) !== 'object') {
return valueOfResult;
}
}
if (typeof value.toJSON === 'function') {
// $FlowFixMe(>=0.90.0)
return value.toJSON();
}
}
return value;
}
function serializeString(rawValue) {
var value = serializeObject(rawValue); // Serialize string, boolean and number values to a string, but do not
// attempt to coerce object, function, symbol, or other types as strings.
if (typeof value === 'string') {
return value;
}
if (typeof value === 'boolean') {
return value ? 'true' : 'false';
}
if ((0, _isFinite.default)(value)) {
return value.toString();
}
throw new TypeError("String cannot represent value: ".concat((0, _inspect.default)(rawValue)));
}
function coerceString(value) {
if (typeof value !== 'string') {
throw new TypeError("String cannot represent a non string value: ".concat((0, _inspect.default)(value)));
}
return value;
}
var GraphQLString = new _definition.GraphQLScalarType({
name: 'String',
description: 'The `String` scalar type represents textual data, represented as UTF-8 ' + 'character sequences. The String type is most often used by GraphQL to ' + 'represent free-form human-readable text.',
serialize: serializeString,
parseValue: coerceString,
parseLiteral: function parseLiteral(ast) {
return ast.kind === _kinds.Kind.STRING ? ast.value : undefined;
}
});
exports.GraphQLString = GraphQLString;
function serializeBoolean(value) {
if (typeof value === 'boolean') {
return value;
}
if ((0, _isFinite.default)(value)) {
return value !== 0;
}
throw new TypeError("Boolean cannot represent a non boolean value: ".concat((0, _inspect.default)(value)));
}
function coerceBoolean(value) {
if (typeof value !== 'boolean') {
throw new TypeError("Boolean cannot represent a non boolean value: ".concat((0, _inspect.default)(value)));
}
return value;
}
var GraphQLBoolean = new _definition.GraphQLScalarType({
name: 'Boolean',
description: 'The `Boolean` scalar type represents `true` or `false`.',
serialize: serializeBoolean,
parseValue: coerceBoolean,
parseLiteral: function parseLiteral(ast) {
return ast.kind === _kinds.Kind.BOOLEAN ? ast.value : undefined;
}
});
exports.GraphQLBoolean = GraphQLBoolean;
function serializeID(rawValue) {
var value = serializeObject(rawValue);
if (typeof value === 'string') {
return value;
}
if ((0, _isInteger.default)(value)) {
return String(value);
}
throw new TypeError("ID cannot represent value: ".concat((0, _inspect.default)(rawValue)));
}
function coerceID(value) {
if (typeof value === 'string') {
return value;
}
if ((0, _isInteger.default)(value)) {
return value.toString();
}
throw new TypeError("ID cannot represent value: ".concat((0, _inspect.default)(value)));
}
var GraphQLID = new _definition.GraphQLScalarType({
name: 'ID',
description: 'The `ID` scalar type represents a unique identifier, often used to ' + 'refetch an object or as key for a cache. The ID type appears in a JSON ' + 'response as a String; however, it is not intended to be human-readable. ' + 'When expected as an input type, any string (such as `"4"`) or integer ' + '(such as `4`) input value will be accepted as an ID.',
serialize: serializeID,
parseValue: coerceID,
parseLiteral: function parseLiteral(ast) {
return ast.kind === _kinds.Kind.STRING || ast.kind === _kinds.Kind.INT ? ast.value : undefined;
}
});
exports.GraphQLID = GraphQLID;
var specifiedScalarTypes = [GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID];
exports.specifiedScalarTypes = specifiedScalarTypes;
function isSpecifiedScalarType(type) {
return (0, _definition.isNamedType)(type) && ( // Would prefer to use specifiedScalarTypes.some(), however %checks needs
// a simple expression.
type.name === GraphQLString.name || type.name === GraphQLInt.name || type.name === GraphQLFloat.name || type.name === GraphQLBoolean.name || type.name === GraphQLID.name);
}