UNPKG

9.67 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.isSpecifiedScalarType = isSpecifiedScalarType;
7exports.specifiedScalarTypes = exports.GraphQLID = exports.GraphQLBoolean = exports.GraphQLString = exports.GraphQLFloat = exports.GraphQLInt = void 0;
8
9var _isFinite = _interopRequireDefault(require("../polyfills/isFinite.js"));
10
11var _isInteger = _interopRequireDefault(require("../polyfills/isInteger.js"));
12
13var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
14
15var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
16
17var _kinds = require("../language/kinds.js");
18
19var _printer = require("../language/printer.js");
20
21var _GraphQLError = require("../error/GraphQLError.js");
22
23var _definition = require("./definition.js");
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27// As per the GraphQL Spec, Integers are only treated as valid when a valid
28// 32-bit signed integer, providing the broadest support across platforms.
29//
30// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because
31// they are internally represented as IEEE 754 doubles.
32var MAX_INT = 2147483647;
33var MIN_INT = -2147483648;
34
35function serializeInt(outputValue) {
36 var coercedValue = serializeObject(outputValue);
37
38 if (typeof coercedValue === 'boolean') {
39 return coercedValue ? 1 : 0;
40 }
41
42 var num = coercedValue;
43
44 if (typeof coercedValue === 'string' && coercedValue !== '') {
45 num = Number(coercedValue);
46 }
47
48 if (!(0, _isInteger.default)(num)) {
49 throw new _GraphQLError.GraphQLError("Int cannot represent non-integer value: ".concat((0, _inspect.default)(coercedValue)));
50 }
51
52 if (num > MAX_INT || num < MIN_INT) {
53 throw new _GraphQLError.GraphQLError('Int cannot represent non 32-bit signed integer value: ' + (0, _inspect.default)(coercedValue));
54 }
55
56 return num;
57}
58
59function coerceInt(inputValue) {
60 if (!(0, _isInteger.default)(inputValue)) {
61 throw new _GraphQLError.GraphQLError("Int cannot represent non-integer value: ".concat((0, _inspect.default)(inputValue)));
62 }
63
64 if (inputValue > MAX_INT || inputValue < MIN_INT) {
65 throw new _GraphQLError.GraphQLError("Int cannot represent non 32-bit signed integer value: ".concat(inputValue));
66 }
67
68 return inputValue;
69}
70
71var GraphQLInt = new _definition.GraphQLScalarType({
72 name: 'Int',
73 description: 'The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.',
74 serialize: serializeInt,
75 parseValue: coerceInt,
76 parseLiteral: function parseLiteral(valueNode) {
77 if (valueNode.kind !== _kinds.Kind.INT) {
78 throw new _GraphQLError.GraphQLError("Int cannot represent non-integer value: ".concat((0, _printer.print)(valueNode)), valueNode);
79 }
80
81 var num = parseInt(valueNode.value, 10);
82
83 if (num > MAX_INT || num < MIN_INT) {
84 throw new _GraphQLError.GraphQLError("Int cannot represent non 32-bit signed integer value: ".concat(valueNode.value), valueNode);
85 }
86
87 return num;
88 }
89});
90exports.GraphQLInt = GraphQLInt;
91
92function serializeFloat(outputValue) {
93 var coercedValue = serializeObject(outputValue);
94
95 if (typeof coercedValue === 'boolean') {
96 return coercedValue ? 1 : 0;
97 }
98
99 var num = coercedValue;
100
101 if (typeof coercedValue === 'string' && coercedValue !== '') {
102 num = Number(coercedValue);
103 }
104
105 if (!(0, _isFinite.default)(num)) {
106 throw new _GraphQLError.GraphQLError("Float cannot represent non numeric value: ".concat((0, _inspect.default)(coercedValue)));
107 }
108
109 return num;
110}
111
112function coerceFloat(inputValue) {
113 if (!(0, _isFinite.default)(inputValue)) {
114 throw new _GraphQLError.GraphQLError("Float cannot represent non numeric value: ".concat((0, _inspect.default)(inputValue)));
115 }
116
117 return inputValue;
118}
119
120var GraphQLFloat = new _definition.GraphQLScalarType({
121 name: 'Float',
122 description: 'The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).',
123 serialize: serializeFloat,
124 parseValue: coerceFloat,
125 parseLiteral: function parseLiteral(valueNode) {
126 if (valueNode.kind !== _kinds.Kind.FLOAT && valueNode.kind !== _kinds.Kind.INT) {
127 throw new _GraphQLError.GraphQLError("Float cannot represent non numeric value: ".concat((0, _printer.print)(valueNode)), valueNode);
128 }
129
130 return parseFloat(valueNode.value);
131 }
132}); // Support serializing objects with custom valueOf() or toJSON() functions -
133// a common way to represent a complex value which can be represented as
134// a string (ex: MongoDB id objects).
135
136exports.GraphQLFloat = GraphQLFloat;
137
138function serializeObject(outputValue) {
139 if ((0, _isObjectLike.default)(outputValue)) {
140 if (typeof outputValue.valueOf === 'function') {
141 var valueOfResult = outputValue.valueOf();
142
143 if (!(0, _isObjectLike.default)(valueOfResult)) {
144 return valueOfResult;
145 }
146 }
147
148 if (typeof outputValue.toJSON === 'function') {
149 // $FlowFixMe[incompatible-use]
150 return outputValue.toJSON();
151 }
152 }
153
154 return outputValue;
155}
156
157function serializeString(outputValue) {
158 var coercedValue = serializeObject(outputValue); // Serialize string, boolean and number values to a string, but do not
159 // attempt to coerce object, function, symbol, or other types as strings.
160
161 if (typeof coercedValue === 'string') {
162 return coercedValue;
163 }
164
165 if (typeof coercedValue === 'boolean') {
166 return coercedValue ? 'true' : 'false';
167 }
168
169 if ((0, _isFinite.default)(coercedValue)) {
170 return coercedValue.toString();
171 }
172
173 throw new _GraphQLError.GraphQLError("String cannot represent value: ".concat((0, _inspect.default)(outputValue)));
174}
175
176function coerceString(inputValue) {
177 if (typeof inputValue !== 'string') {
178 throw new _GraphQLError.GraphQLError("String cannot represent a non string value: ".concat((0, _inspect.default)(inputValue)));
179 }
180
181 return inputValue;
182}
183
184var GraphQLString = new _definition.GraphQLScalarType({
185 name: 'String',
186 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.',
187 serialize: serializeString,
188 parseValue: coerceString,
189 parseLiteral: function parseLiteral(valueNode) {
190 if (valueNode.kind !== _kinds.Kind.STRING) {
191 throw new _GraphQLError.GraphQLError("String cannot represent a non string value: ".concat((0, _printer.print)(valueNode)), valueNode);
192 }
193
194 return valueNode.value;
195 }
196});
197exports.GraphQLString = GraphQLString;
198
199function serializeBoolean(outputValue) {
200 var coercedValue = serializeObject(outputValue);
201
202 if (typeof coercedValue === 'boolean') {
203 return coercedValue;
204 }
205
206 if ((0, _isFinite.default)(coercedValue)) {
207 return coercedValue !== 0;
208 }
209
210 throw new _GraphQLError.GraphQLError("Boolean cannot represent a non boolean value: ".concat((0, _inspect.default)(coercedValue)));
211}
212
213function coerceBoolean(inputValue) {
214 if (typeof inputValue !== 'boolean') {
215 throw new _GraphQLError.GraphQLError("Boolean cannot represent a non boolean value: ".concat((0, _inspect.default)(inputValue)));
216 }
217
218 return inputValue;
219}
220
221var GraphQLBoolean = new _definition.GraphQLScalarType({
222 name: 'Boolean',
223 description: 'The `Boolean` scalar type represents `true` or `false`.',
224 serialize: serializeBoolean,
225 parseValue: coerceBoolean,
226 parseLiteral: function parseLiteral(valueNode) {
227 if (valueNode.kind !== _kinds.Kind.BOOLEAN) {
228 throw new _GraphQLError.GraphQLError("Boolean cannot represent a non boolean value: ".concat((0, _printer.print)(valueNode)), valueNode);
229 }
230
231 return valueNode.value;
232 }
233});
234exports.GraphQLBoolean = GraphQLBoolean;
235
236function serializeID(outputValue) {
237 var coercedValue = serializeObject(outputValue);
238
239 if (typeof coercedValue === 'string') {
240 return coercedValue;
241 }
242
243 if ((0, _isInteger.default)(coercedValue)) {
244 return String(coercedValue);
245 }
246
247 throw new _GraphQLError.GraphQLError("ID cannot represent value: ".concat((0, _inspect.default)(outputValue)));
248}
249
250function coerceID(inputValue) {
251 if (typeof inputValue === 'string') {
252 return inputValue;
253 }
254
255 if ((0, _isInteger.default)(inputValue)) {
256 return inputValue.toString();
257 }
258
259 throw new _GraphQLError.GraphQLError("ID cannot represent value: ".concat((0, _inspect.default)(inputValue)));
260}
261
262var GraphQLID = new _definition.GraphQLScalarType({
263 name: 'ID',
264 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.',
265 serialize: serializeID,
266 parseValue: coerceID,
267 parseLiteral: function parseLiteral(valueNode) {
268 if (valueNode.kind !== _kinds.Kind.STRING && valueNode.kind !== _kinds.Kind.INT) {
269 throw new _GraphQLError.GraphQLError('ID cannot represent a non-string and non-integer value: ' + (0, _printer.print)(valueNode), valueNode);
270 }
271
272 return valueNode.value;
273 }
274});
275exports.GraphQLID = GraphQLID;
276var specifiedScalarTypes = Object.freeze([GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID]);
277exports.specifiedScalarTypes = specifiedScalarTypes;
278
279function isSpecifiedScalarType(type) {
280 return specifiedScalarTypes.some(function (_ref) {
281 var name = _ref.name;
282 return type.name === name;
283 });
284}