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