UNPKG

5.16 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.specifiedScalarTypes = exports.GraphQLID = exports.GraphQLBoolean = exports.GraphQLString = exports.GraphQLFloat = exports.GraphQLInt = undefined;
7exports.isSpecifiedScalarType = isSpecifiedScalarType;
8
9var _definition = require('./definition');
10
11var _kinds = require('../language/kinds');
12
13var Kind = _interopRequireWildcard(_kinds);
14
15function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
16
17// As per the GraphQL Spec, Integers are only treated as valid when a valid
18// 32-bit signed integer, providing the broadest support across platforms.
19//
20// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because
21// they are internally represented as IEEE 754 doubles.
22/**
23 * Copyright (c) 2015-present, Facebook, Inc.
24 *
25 * This source code is licensed under the MIT license found in the
26 * LICENSE file in the root directory of this source tree.
27 *
28 *
29 */
30
31var MAX_INT = 2147483647;
32var MIN_INT = -2147483648;
33
34function coerceInt(value) {
35 if (value === '') {
36 throw new TypeError('Int cannot represent non 32-bit signed integer value: (empty string)');
37 }
38 var num = Number(value);
39 if (num !== num || num > MAX_INT || num < MIN_INT) {
40 throw new TypeError('Int cannot represent non 32-bit signed integer value: ' + String(value));
41 }
42 var int = Math.floor(num);
43 if (int !== num) {
44 throw new TypeError('Int cannot represent non-integer value: ' + String(value));
45 }
46 return int;
47}
48
49var GraphQLInt = exports.GraphQLInt = new _definition.GraphQLScalarType({
50 name: 'Int',
51 description: 'The `Int` scalar type represents non-fractional signed whole numeric ' + 'values. Int can represent values between -(2^31) and 2^31 - 1. ',
52 serialize: coerceInt,
53 parseValue: coerceInt,
54 parseLiteral: function parseLiteral(ast) {
55 if (ast.kind === Kind.INT) {
56 var num = parseInt(ast.value, 10);
57 if (num <= MAX_INT && num >= MIN_INT) {
58 return num;
59 }
60 }
61 return undefined;
62 }
63});
64
65function coerceFloat(value) {
66 if (value === '') {
67 throw new TypeError('Float cannot represent non numeric value: (empty string)');
68 }
69 var num = Number(value);
70 if (num === num) {
71 return num;
72 }
73 throw new TypeError('Float cannot represent non numeric value: ' + String(value));
74}
75
76var GraphQLFloat = exports.GraphQLFloat = new _definition.GraphQLScalarType({
77 name: 'Float',
78 description: 'The `Float` scalar type represents signed double-precision fractional ' + 'values as specified by ' + '[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ',
79 serialize: coerceFloat,
80 parseValue: coerceFloat,
81 parseLiteral: function parseLiteral(ast) {
82 return ast.kind === Kind.FLOAT || ast.kind === Kind.INT ? parseFloat(ast.value) : undefined;
83 }
84});
85
86function coerceString(value) {
87 if (Array.isArray(value)) {
88 throw new TypeError('String cannot represent an array value: [' + String(value) + ']');
89 }
90 return String(value);
91}
92
93var GraphQLString = exports.GraphQLString = new _definition.GraphQLScalarType({
94 name: 'String',
95 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.',
96 serialize: coerceString,
97 parseValue: coerceString,
98 parseLiteral: function parseLiteral(ast) {
99 return ast.kind === Kind.STRING ? ast.value : undefined;
100 }
101});
102
103var GraphQLBoolean = exports.GraphQLBoolean = new _definition.GraphQLScalarType({
104 name: 'Boolean',
105 description: 'The `Boolean` scalar type represents `true` or `false`.',
106 serialize: Boolean,
107 parseValue: Boolean,
108 parseLiteral: function parseLiteral(ast) {
109 return ast.kind === Kind.BOOLEAN ? ast.value : undefined;
110 }
111});
112
113var GraphQLID = exports.GraphQLID = new _definition.GraphQLScalarType({
114 name: 'ID',
115 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.',
116 serialize: String,
117 parseValue: String,
118 parseLiteral: function parseLiteral(ast) {
119 return ast.kind === Kind.STRING || ast.kind === Kind.INT ? ast.value : undefined;
120 }
121});
122
123var specifiedScalarTypes = exports.specifiedScalarTypes = [GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID];
124
125function isSpecifiedScalarType(type) {
126 return (0, _definition.isNamedType)(type) && (
127 // Would prefer to use specifiedScalarTypes.some(), however %checks needs
128 // a simple expression.
129 type.name === GraphQLString.name || type.name === GraphQLInt.name || type.name === GraphQLFloat.name || type.name === GraphQLBoolean.name || type.name === GraphQLID.name);
130}
\No newline at end of file