UNPKG

7.34 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /**
8 * Copyright (c) 2015-present, Facebook, Inc.
9 *
10 * This source code is licensed under the MIT license found in the
11 * LICENSE file in the root directory of this source tree.
12 *
13 *
14 */
15
16exports.astFromValue = astFromValue;
17
18var _iterall = require('iterall');
19
20var _isNullish = require('../jsutils/isNullish');
21
22var _isNullish2 = _interopRequireDefault(_isNullish);
23
24var _isInvalid = require('../jsutils/isInvalid');
25
26var _isInvalid2 = _interopRequireDefault(_isInvalid);
27
28var _kinds = require('../language/kinds');
29
30var Kind = _interopRequireWildcard(_kinds);
31
32var _definition = require('../type/definition');
33
34var _scalars = require('../type/scalars');
35
36function _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; } }
37
38function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
40/**
41 * Produces a GraphQL Value AST given a JavaScript value.
42 *
43 * A GraphQL type must be provided, which will be used to interpret different
44 * JavaScript values.
45 *
46 * | JSON Value | GraphQL Value |
47 * | ------------- | -------------------- |
48 * | Object | Input Object |
49 * | Array | List |
50 * | Boolean | Boolean |
51 * | String | String / Enum Value |
52 * | Number | Int / Float |
53 * | Mixed | Enum Value |
54 * | null | NullValue |
55 *
56 */
57function astFromValue(value, type) {
58 // Ensure flow knows that we treat function params as const.
59 var _value = value;
60
61 if ((0, _definition.isNonNullType)(type)) {
62 var astValue = astFromValue(_value, type.ofType);
63 if (astValue && astValue.kind === Kind.NULL) {
64 return null;
65 }
66 return astValue;
67 }
68
69 // only explicit null, not undefined, NaN
70 if (_value === null) {
71 return { kind: Kind.NULL };
72 }
73
74 // undefined, NaN
75 if ((0, _isInvalid2.default)(_value)) {
76 return null;
77 }
78
79 // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
80 // the value is not an array, convert the value using the list's item type.
81 if ((0, _definition.isListType)(type)) {
82 var itemType = type.ofType;
83 if ((0, _iterall.isCollection)(_value)) {
84 var valuesNodes = [];
85 (0, _iterall.forEach)(_value, function (item) {
86 var itemNode = astFromValue(item, itemType);
87 if (itemNode) {
88 valuesNodes.push(itemNode);
89 }
90 });
91 return { kind: Kind.LIST, values: valuesNodes };
92 }
93 return astFromValue(_value, itemType);
94 }
95
96 // Populate the fields of the input object by creating ASTs from each value
97 // in the JavaScript object according to the fields in the input type.
98 if ((0, _definition.isInputObjectType)(type)) {
99 if (_value === null || (typeof _value === 'undefined' ? 'undefined' : _typeof(_value)) !== 'object') {
100 return null;
101 }
102 var fields = type.getFields();
103 var fieldNodes = [];
104 Object.keys(fields).forEach(function (fieldName) {
105 var fieldType = fields[fieldName].type;
106 var fieldValue = astFromValue(_value[fieldName], fieldType);
107 if (fieldValue) {
108 fieldNodes.push({
109 kind: Kind.OBJECT_FIELD,
110 name: { kind: Kind.NAME, value: fieldName },
111 value: fieldValue
112 });
113 }
114 });
115 return { kind: Kind.OBJECT, fields: fieldNodes };
116 }
117
118 if ((0, _definition.isScalarType)(type) || (0, _definition.isEnumType)(type)) {
119 // Since value is an internally represented value, it must be serialized
120 // to an externally represented value before converting into an AST.
121 var serialized = type.serialize(_value);
122 if ((0, _isNullish2.default)(serialized)) {
123 return null;
124 }
125
126 // Others serialize based on their corresponding JavaScript scalar types.
127 if (typeof serialized === 'boolean') {
128 return { kind: Kind.BOOLEAN, value: serialized };
129 }
130
131 // JavaScript numbers can be Int or Float values.
132 if (typeof serialized === 'number') {
133 var stringNum = String(serialized);
134 return (/^[0-9]+$/.test(stringNum) ? { kind: Kind.INT, value: stringNum } : { kind: Kind.FLOAT, value: stringNum }
135 );
136 }
137
138 if (typeof serialized === 'string') {
139 // Enum types use Enum literals.
140 if ((0, _definition.isEnumType)(type)) {
141 return { kind: Kind.ENUM, value: serialized };
142 }
143
144 // ID types can use Int literals.
145 if (type === _scalars.GraphQLID && /^[0-9]+$/.test(serialized)) {
146 return { kind: Kind.INT, value: serialized };
147 }
148
149 // Use JSON stringify, which uses the same string encoding as GraphQL,
150 // then remove the quotes.
151 return {
152 kind: Kind.STRING,
153 value: JSON.stringify(serialized).slice(1, -1)
154 };
155 }
156
157 throw new TypeError('Cannot convert value to AST: ' + String(serialized));
158 }
159
160 /* istanbul ignore next */
161 throw new Error('Unknown type: ' + type + '.');
162}
\No newline at end of file