1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.specifiedScalarTypes = exports.GraphQLID = exports.GraphQLBoolean = exports.GraphQLString = exports.GraphQLFloat = exports.GraphQLInt = undefined;
|
7 | exports.isSpecifiedScalarType = isSpecifiedScalarType;
|
8 |
|
9 | var _definition = require('./definition');
|
10 |
|
11 | var _kinds = require('../language/kinds');
|
12 |
|
13 | var Kind = _interopRequireWildcard(_kinds);
|
14 |
|
15 | function _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 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | var MAX_INT = 2147483647;
|
32 | var MIN_INT = -2147483648;
|
33 |
|
34 | function 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 |
|
49 | var 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 |
|
65 | function 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 |
|
76 | var 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 |
|
86 | function 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 |
|
93 | var 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 |
|
103 | var 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 |
|
113 | var 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 |
|
123 | var specifiedScalarTypes = exports.specifiedScalarTypes = [GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID];
|
124 |
|
125 | function isSpecifiedScalarType(type) {
|
126 | return (0, _definition.isNamedType)(type) && (
|
127 |
|
128 |
|
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 |