graphql
Version:
A Query Language and Runtime which can target any service.
89 lines (78 loc) • 2.6 kB
JavaScript
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
;
var _Object$defineProperty = require('babel-runtime/core-js/object/define-property')['default'];
_Object$defineProperty(exports, '__esModule', {
value: true
});
var _definition = require('./definition');
var _language = require('../language');
// Integers are only safe when between -(2^53 - 1) and 2^53 - 1 due to being
// encoded in JavaScript and represented in JSON as double-precision floating
// point numbers, as specified by IEEE 754.
var MAX_INT = 9007199254740991;
var MIN_INT = -9007199254740991;
var GraphQLInt = new _definition.GraphQLScalarType({
name: 'Int',
coerce: function coerce(value) {
var num = +value;
return num === num && num <= MAX_INT && num >= MIN_INT ? num | 0 : null;
},
coerceLiteral: function coerceLiteral(ast) {
if (ast.kind === _language.Kind.INT) {
var num = parseInt(ast.value, 10);
if (num <= MAX_INT && num >= MIN_INT) {
return num;
}
}
}
});
exports.GraphQLInt = GraphQLInt;
var GraphQLFloat = new _definition.GraphQLScalarType({
name: 'Float',
coerce: function coerce(value) {
var num = +value;
return num === num ? num : null;
},
coerceLiteral: function coerceLiteral(ast) {
return ast.kind === _language.Kind.FLOAT || ast.kind === _language.Kind.INT ? parseFloat(ast.value) : null;
}
});
exports.GraphQLFloat = GraphQLFloat;
var GraphQLString = new _definition.GraphQLScalarType({
name: 'String',
coerce: function coerce(value) {
return '' + value;
},
coerceLiteral: function coerceLiteral(ast) {
return ast.kind === _language.Kind.STRING ? ast.value : null;
}
});
exports.GraphQLString = GraphQLString;
var GraphQLBoolean = new _definition.GraphQLScalarType({
name: 'Boolean',
coerce: function coerce(value) {
return !!value;
},
coerceLiteral: function coerceLiteral(ast) {
return ast.kind === _language.Kind.BOOLEAN ? ast.value : null;
}
});
exports.GraphQLBoolean = GraphQLBoolean;
var GraphQLID = new _definition.GraphQLScalarType({
name: 'ID',
coerce: function coerce(value) {
return '' + value;
},
coerceLiteral: function coerceLiteral(ast) {
return ast.kind === _language.Kind.STRING || ast.kind === _language.Kind.INT ? ast.value : null;
}
});
exports.GraphQLID = GraphQLID;