1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.isSpecifiedScalarType = isSpecifiedScalarType;
|
7 | exports.specifiedScalarTypes = exports.GraphQLID = exports.GraphQLBoolean = exports.GraphQLString = exports.GraphQLFloat = exports.GraphQLInt = void 0;
|
8 |
|
9 | var _isFinite = _interopRequireDefault(require("../polyfills/isFinite.js"));
|
10 |
|
11 | var _isInteger = _interopRequireDefault(require("../polyfills/isInteger.js"));
|
12 |
|
13 | var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
|
14 |
|
15 | var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
|
16 |
|
17 | var _kinds = require("../language/kinds.js");
|
18 |
|
19 | var _printer = require("../language/printer.js");
|
20 |
|
21 | var _GraphQLError = require("../error/GraphQLError.js");
|
22 |
|
23 | var _definition = require("./definition.js");
|
24 |
|
25 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var MAX_INT = 2147483647;
|
33 | var MIN_INT = -2147483648;
|
34 |
|
35 | function 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 |
|
59 | function 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 |
|
71 | var 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 | });
|
90 | exports.GraphQLInt = GraphQLInt;
|
91 |
|
92 | function 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 |
|
112 | function 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 |
|
120 | var 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 | });
|
133 |
|
134 |
|
135 |
|
136 | exports.GraphQLFloat = GraphQLFloat;
|
137 |
|
138 | function 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 |
|
150 | return outputValue.toJSON();
|
151 | }
|
152 | }
|
153 |
|
154 | return outputValue;
|
155 | }
|
156 |
|
157 | function serializeString(outputValue) {
|
158 | var coercedValue = serializeObject(outputValue);
|
159 |
|
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 |
|
176 | function 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 |
|
184 | var 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 | });
|
197 | exports.GraphQLString = GraphQLString;
|
198 |
|
199 | function 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 |
|
213 | function 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 |
|
221 | var 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 | });
|
234 | exports.GraphQLBoolean = GraphQLBoolean;
|
235 |
|
236 | function 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 |
|
250 | function 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 |
|
262 | var 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 | });
|
275 | exports.GraphQLID = GraphQLID;
|
276 | var specifiedScalarTypes = Object.freeze([GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID]);
|
277 | exports.specifiedScalarTypes = specifiedScalarTypes;
|
278 |
|
279 | function isSpecifiedScalarType(type) {
|
280 | return specifiedScalarTypes.some(function (_ref) {
|
281 | var name = _ref.name;
|
282 | return type.name === name;
|
283 | });
|
284 | }
|