UNPKG

5.54 kBJavaScriptView Raw
1function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
3/**
4 * Copyright (c) 2015-present, Facebook, Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 *
9 *
10 */
11import { forEach, isCollection } from 'iterall';
12import objectValues from '../polyfills/objectValues';
13import inspect from '../jsutils/inspect';
14import isNullish from '../jsutils/isNullish';
15import isInvalid from '../jsutils/isInvalid';
16import { Kind } from '../language/kinds';
17import { isScalarType, isEnumType, isInputObjectType, isListType, isNonNullType } from '../type/definition';
18import { GraphQLID } from '../type/scalars';
19/**
20 * Produces a GraphQL Value AST given a JavaScript value.
21 *
22 * A GraphQL type must be provided, which will be used to interpret different
23 * JavaScript values.
24 *
25 * | JSON Value | GraphQL Value |
26 * | ------------- | -------------------- |
27 * | Object | Input Object |
28 * | Array | List |
29 * | Boolean | Boolean |
30 * | String | String / Enum Value |
31 * | Number | Int / Float |
32 * | Mixed | Enum Value |
33 * | null | NullValue |
34 *
35 */
36
37export function astFromValue(value, type) {
38 if (isNonNullType(type)) {
39 var astValue = astFromValue(value, type.ofType);
40
41 if (astValue && astValue.kind === Kind.NULL) {
42 return null;
43 }
44
45 return astValue;
46 } // only explicit null, not undefined, NaN
47
48
49 if (value === null) {
50 return {
51 kind: Kind.NULL
52 };
53 } // undefined, NaN
54
55
56 if (isInvalid(value)) {
57 return null;
58 } // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
59 // the value is not an array, convert the value using the list's item type.
60
61
62 if (isListType(type)) {
63 var itemType = type.ofType;
64
65 if (isCollection(value)) {
66 var valuesNodes = [];
67 forEach(value, function (item) {
68 var itemNode = astFromValue(item, itemType);
69
70 if (itemNode) {
71 valuesNodes.push(itemNode);
72 }
73 });
74 return {
75 kind: Kind.LIST,
76 values: valuesNodes
77 };
78 }
79
80 return astFromValue(value, itemType);
81 } // Populate the fields of the input object by creating ASTs from each value
82 // in the JavaScript object according to the fields in the input type.
83
84
85 if (isInputObjectType(type)) {
86 if (value === null || _typeof(value) !== 'object') {
87 return null;
88 }
89
90 var fields = objectValues(type.getFields());
91 var fieldNodes = [];
92 var _iteratorNormalCompletion = true;
93 var _didIteratorError = false;
94 var _iteratorError = undefined;
95
96 try {
97 for (var _iterator = fields[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
98 var field = _step.value;
99 var fieldValue = astFromValue(value[field.name], field.type);
100
101 if (fieldValue) {
102 fieldNodes.push({
103 kind: Kind.OBJECT_FIELD,
104 name: {
105 kind: Kind.NAME,
106 value: field.name
107 },
108 value: fieldValue
109 });
110 }
111 }
112 } catch (err) {
113 _didIteratorError = true;
114 _iteratorError = err;
115 } finally {
116 try {
117 if (!_iteratorNormalCompletion && _iterator.return != null) {
118 _iterator.return();
119 }
120 } finally {
121 if (_didIteratorError) {
122 throw _iteratorError;
123 }
124 }
125 }
126
127 return {
128 kind: Kind.OBJECT,
129 fields: fieldNodes
130 };
131 }
132
133 if (isScalarType(type) || isEnumType(type)) {
134 // Since value is an internally represented value, it must be serialized
135 // to an externally represented value before converting into an AST.
136 var serialized = type.serialize(value);
137
138 if (isNullish(serialized)) {
139 return null;
140 } // Others serialize based on their corresponding JavaScript scalar types.
141
142
143 if (typeof serialized === 'boolean') {
144 return {
145 kind: Kind.BOOLEAN,
146 value: serialized
147 };
148 } // JavaScript numbers can be Int or Float values.
149
150
151 if (typeof serialized === 'number') {
152 var stringNum = String(serialized);
153 return integerStringRegExp.test(stringNum) ? {
154 kind: Kind.INT,
155 value: stringNum
156 } : {
157 kind: Kind.FLOAT,
158 value: stringNum
159 };
160 }
161
162 if (typeof serialized === 'string') {
163 // Enum types use Enum literals.
164 if (isEnumType(type)) {
165 return {
166 kind: Kind.ENUM,
167 value: serialized
168 };
169 } // ID types can use Int literals.
170
171
172 if (type === GraphQLID && integerStringRegExp.test(serialized)) {
173 return {
174 kind: Kind.INT,
175 value: serialized
176 };
177 }
178
179 return {
180 kind: Kind.STRING,
181 value: serialized
182 };
183 }
184
185 throw new TypeError("Cannot convert value to AST: ".concat(inspect(serialized)));
186 }
187 /* istanbul ignore next */
188
189
190 throw new Error("Unknown type: ".concat(type, "."));
191}
192/**
193 * IntValue:
194 * - NegativeSign? 0
195 * - NegativeSign? NonZeroDigit ( Digit+ )?
196 */
197
198var integerStringRegExp = /^-?(0|[1-9][0-9]*)$/;
\No newline at end of file