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"));
|
10 |
|
11 | var _isInteger = _interopRequireDefault(require("../polyfills/isInteger"));
|
12 |
|
13 | var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
|
14 |
|
15 | var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
|
16 |
|
17 | var _kinds = require("../language/kinds");
|
18 |
|
19 | var _definition = require("./definition");
|
20 |
|
21 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var MAX_INT = 2147483647;
|
29 | var MIN_INT = -2147483648;
|
30 |
|
31 | function serializeInt(value) {
|
32 | if (typeof value === 'boolean') {
|
33 | return value ? 1 : 0;
|
34 | }
|
35 |
|
36 | var num = value;
|
37 |
|
38 | if (typeof value === 'string' && value !== '') {
|
39 | num = Number(value);
|
40 | }
|
41 |
|
42 | if (!(0, _isInteger.default)(num)) {
|
43 | throw new TypeError("Int cannot represent non-integer value: ".concat((0, _inspect.default)(value)));
|
44 | }
|
45 |
|
46 | if (num > MAX_INT || num < MIN_INT) {
|
47 | throw new TypeError("Int cannot represent non 32-bit signed integer value: ".concat((0, _inspect.default)(value)));
|
48 | }
|
49 |
|
50 | return num;
|
51 | }
|
52 |
|
53 | function coerceInt(value) {
|
54 | if (!(0, _isInteger.default)(value)) {
|
55 | throw new TypeError("Int cannot represent non-integer value: ".concat((0, _inspect.default)(value)));
|
56 | }
|
57 |
|
58 | if (value > MAX_INT || value < MIN_INT) {
|
59 | throw new TypeError("Int cannot represent non 32-bit signed integer value: ".concat((0, _inspect.default)(value)));
|
60 | }
|
61 |
|
62 | return value;
|
63 | }
|
64 |
|
65 | var GraphQLInt = new _definition.GraphQLScalarType({
|
66 | name: 'Int',
|
67 | description: 'The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.',
|
68 | serialize: serializeInt,
|
69 | parseValue: coerceInt,
|
70 | parseLiteral: function parseLiteral(ast) {
|
71 | if (ast.kind === _kinds.Kind.INT) {
|
72 | var num = parseInt(ast.value, 10);
|
73 |
|
74 | if (num <= MAX_INT && num >= MIN_INT) {
|
75 | return num;
|
76 | }
|
77 | }
|
78 |
|
79 | return undefined;
|
80 | }
|
81 | });
|
82 | exports.GraphQLInt = GraphQLInt;
|
83 |
|
84 | function serializeFloat(value) {
|
85 | if (typeof value === 'boolean') {
|
86 | return value ? 1 : 0;
|
87 | }
|
88 |
|
89 | var num = value;
|
90 |
|
91 | if (typeof value === 'string' && value !== '') {
|
92 | num = Number(value);
|
93 | }
|
94 |
|
95 | if (!(0, _isFinite.default)(num)) {
|
96 | throw new TypeError("Float cannot represent non numeric value: ".concat((0, _inspect.default)(value)));
|
97 | }
|
98 |
|
99 | return num;
|
100 | }
|
101 |
|
102 | function coerceFloat(value) {
|
103 | if (!(0, _isFinite.default)(value)) {
|
104 | throw new TypeError("Float cannot represent non numeric value: ".concat((0, _inspect.default)(value)));
|
105 | }
|
106 |
|
107 | return value;
|
108 | }
|
109 |
|
110 | var GraphQLFloat = new _definition.GraphQLScalarType({
|
111 | name: 'Float',
|
112 | description: 'The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).',
|
113 | serialize: serializeFloat,
|
114 | parseValue: coerceFloat,
|
115 | parseLiteral: function parseLiteral(ast) {
|
116 | return ast.kind === _kinds.Kind.FLOAT || ast.kind === _kinds.Kind.INT ? parseFloat(ast.value) : undefined;
|
117 | }
|
118 | });
|
119 |
|
120 |
|
121 |
|
122 | exports.GraphQLFloat = GraphQLFloat;
|
123 |
|
124 | function serializeObject(value) {
|
125 | if ((0, _isObjectLike.default)(value)) {
|
126 | if (typeof value.valueOf === 'function') {
|
127 | var valueOfResult = value.valueOf();
|
128 |
|
129 | if (!(0, _isObjectLike.default)(valueOfResult)) {
|
130 | return valueOfResult;
|
131 | }
|
132 | }
|
133 |
|
134 | if (typeof value.toJSON === 'function') {
|
135 |
|
136 | return value.toJSON();
|
137 | }
|
138 | }
|
139 |
|
140 | return value;
|
141 | }
|
142 |
|
143 | function serializeString(rawValue) {
|
144 | var value = serializeObject(rawValue);
|
145 |
|
146 |
|
147 | if (typeof value === 'string') {
|
148 | return value;
|
149 | }
|
150 |
|
151 | if (typeof value === 'boolean') {
|
152 | return value ? 'true' : 'false';
|
153 | }
|
154 |
|
155 | if ((0, _isFinite.default)(value)) {
|
156 | return value.toString();
|
157 | }
|
158 |
|
159 | throw new TypeError("String cannot represent value: ".concat((0, _inspect.default)(rawValue)));
|
160 | }
|
161 |
|
162 | function coerceString(value) {
|
163 | if (typeof value !== 'string') {
|
164 | throw new TypeError("String cannot represent a non string value: ".concat((0, _inspect.default)(value)));
|
165 | }
|
166 |
|
167 | return value;
|
168 | }
|
169 |
|
170 | var GraphQLString = new _definition.GraphQLScalarType({
|
171 | name: 'String',
|
172 | 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.',
|
173 | serialize: serializeString,
|
174 | parseValue: coerceString,
|
175 | parseLiteral: function parseLiteral(ast) {
|
176 | return ast.kind === _kinds.Kind.STRING ? ast.value : undefined;
|
177 | }
|
178 | });
|
179 | exports.GraphQLString = GraphQLString;
|
180 |
|
181 | function serializeBoolean(value) {
|
182 | if (typeof value === 'boolean') {
|
183 | return value;
|
184 | }
|
185 |
|
186 | if ((0, _isFinite.default)(value)) {
|
187 | return value !== 0;
|
188 | }
|
189 |
|
190 | throw new TypeError("Boolean cannot represent a non boolean value: ".concat((0, _inspect.default)(value)));
|
191 | }
|
192 |
|
193 | function coerceBoolean(value) {
|
194 | if (typeof value !== 'boolean') {
|
195 | throw new TypeError("Boolean cannot represent a non boolean value: ".concat((0, _inspect.default)(value)));
|
196 | }
|
197 |
|
198 | return value;
|
199 | }
|
200 |
|
201 | var GraphQLBoolean = new _definition.GraphQLScalarType({
|
202 | name: 'Boolean',
|
203 | description: 'The `Boolean` scalar type represents `true` or `false`.',
|
204 | serialize: serializeBoolean,
|
205 | parseValue: coerceBoolean,
|
206 | parseLiteral: function parseLiteral(ast) {
|
207 | return ast.kind === _kinds.Kind.BOOLEAN ? ast.value : undefined;
|
208 | }
|
209 | });
|
210 | exports.GraphQLBoolean = GraphQLBoolean;
|
211 |
|
212 | function serializeID(rawValue) {
|
213 | var value = serializeObject(rawValue);
|
214 |
|
215 | if (typeof value === 'string') {
|
216 | return value;
|
217 | }
|
218 |
|
219 | if ((0, _isInteger.default)(value)) {
|
220 | return String(value);
|
221 | }
|
222 |
|
223 | throw new TypeError("ID cannot represent value: ".concat((0, _inspect.default)(rawValue)));
|
224 | }
|
225 |
|
226 | function coerceID(value) {
|
227 | if (typeof value === 'string') {
|
228 | return value;
|
229 | }
|
230 |
|
231 | if ((0, _isInteger.default)(value)) {
|
232 | return value.toString();
|
233 | }
|
234 |
|
235 | throw new TypeError("ID cannot represent value: ".concat((0, _inspect.default)(value)));
|
236 | }
|
237 |
|
238 | var GraphQLID = new _definition.GraphQLScalarType({
|
239 | name: 'ID',
|
240 | 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.',
|
241 | serialize: serializeID,
|
242 | parseValue: coerceID,
|
243 | parseLiteral: function parseLiteral(ast) {
|
244 | return ast.kind === _kinds.Kind.STRING || ast.kind === _kinds.Kind.INT ? ast.value : undefined;
|
245 | }
|
246 | });
|
247 | exports.GraphQLID = GraphQLID;
|
248 | var specifiedScalarTypes = Object.freeze([GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID]);
|
249 | exports.specifiedScalarTypes = specifiedScalarTypes;
|
250 |
|
251 | function isSpecifiedScalarType(type) {
|
252 | return (0, _definition.isScalarType)(type) && specifiedScalarTypes.some(function (_ref) {
|
253 | var name = _ref.name;
|
254 | return type.name === name;
|
255 | });
|
256 | }
|