1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify"));
|
5 | var ts_invariant_1 = require("ts-invariant");
|
6 | function isScalarValue(value) {
|
7 | return ['StringValue', 'BooleanValue', 'EnumValue'].indexOf(value.kind) > -1;
|
8 | }
|
9 | exports.isScalarValue = isScalarValue;
|
10 | function isNumberValue(value) {
|
11 | return ['IntValue', 'FloatValue'].indexOf(value.kind) > -1;
|
12 | }
|
13 | exports.isNumberValue = isNumberValue;
|
14 | function isStringValue(value) {
|
15 | return value.kind === 'StringValue';
|
16 | }
|
17 | function isBooleanValue(value) {
|
18 | return value.kind === 'BooleanValue';
|
19 | }
|
20 | function isIntValue(value) {
|
21 | return value.kind === 'IntValue';
|
22 | }
|
23 | function isFloatValue(value) {
|
24 | return value.kind === 'FloatValue';
|
25 | }
|
26 | function isVariable(value) {
|
27 | return value.kind === 'Variable';
|
28 | }
|
29 | function isObjectValue(value) {
|
30 | return value.kind === 'ObjectValue';
|
31 | }
|
32 | function isListValue(value) {
|
33 | return value.kind === 'ListValue';
|
34 | }
|
35 | function isEnumValue(value) {
|
36 | return value.kind === 'EnumValue';
|
37 | }
|
38 | function isNullValue(value) {
|
39 | return value.kind === 'NullValue';
|
40 | }
|
41 | function valueToObjectRepresentation(argObj, name, value, variables) {
|
42 | if (isIntValue(value) || isFloatValue(value)) {
|
43 | argObj[name.value] = Number(value.value);
|
44 | }
|
45 | else if (isBooleanValue(value) || isStringValue(value)) {
|
46 | argObj[name.value] = value.value;
|
47 | }
|
48 | else if (isObjectValue(value)) {
|
49 | var nestedArgObj_1 = {};
|
50 | value.fields.map(function (obj) {
|
51 | return valueToObjectRepresentation(nestedArgObj_1, obj.name, obj.value, variables);
|
52 | });
|
53 | argObj[name.value] = nestedArgObj_1;
|
54 | }
|
55 | else if (isVariable(value)) {
|
56 | var variableValue = (variables || {})[value.name.value];
|
57 | argObj[name.value] = variableValue;
|
58 | }
|
59 | else if (isListValue(value)) {
|
60 | argObj[name.value] = value.values.map(function (listValue) {
|
61 | var nestedArgArrayObj = {};
|
62 | valueToObjectRepresentation(nestedArgArrayObj, name, listValue, variables);
|
63 | return nestedArgArrayObj[name.value];
|
64 | });
|
65 | }
|
66 | else if (isEnumValue(value)) {
|
67 | argObj[name.value] = value.value;
|
68 | }
|
69 | else if (isNullValue(value)) {
|
70 | argObj[name.value] = null;
|
71 | }
|
72 | else {
|
73 | throw new ts_invariant_1.InvariantError("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\"" +
|
74 | 'is not supported. Use variables instead of inline arguments to ' +
|
75 | 'overcome this limitation.');
|
76 | }
|
77 | }
|
78 | exports.valueToObjectRepresentation = valueToObjectRepresentation;
|
79 | function storeKeyNameFromField(field, variables) {
|
80 | var directivesObj = null;
|
81 | if (field.directives) {
|
82 | directivesObj = {};
|
83 | field.directives.forEach(function (directive) {
|
84 | directivesObj[directive.name.value] = {};
|
85 | if (directive.arguments) {
|
86 | directive.arguments.forEach(function (_a) {
|
87 | var name = _a.name, value = _a.value;
|
88 | return valueToObjectRepresentation(directivesObj[directive.name.value], name, value, variables);
|
89 | });
|
90 | }
|
91 | });
|
92 | }
|
93 | var argObj = null;
|
94 | if (field.arguments && field.arguments.length) {
|
95 | argObj = {};
|
96 | field.arguments.forEach(function (_a) {
|
97 | var name = _a.name, value = _a.value;
|
98 | return valueToObjectRepresentation(argObj, name, value, variables);
|
99 | });
|
100 | }
|
101 | return getStoreKeyName(field.name.value, argObj, directivesObj);
|
102 | }
|
103 | exports.storeKeyNameFromField = storeKeyNameFromField;
|
104 | var KNOWN_DIRECTIVES = [
|
105 | 'connection',
|
106 | 'include',
|
107 | 'skip',
|
108 | 'client',
|
109 | 'rest',
|
110 | 'export',
|
111 | ];
|
112 | function getStoreKeyName(fieldName, args, directives) {
|
113 | if (directives &&
|
114 | directives['connection'] &&
|
115 | directives['connection']['key']) {
|
116 | if (directives['connection']['filter'] &&
|
117 | directives['connection']['filter'].length > 0) {
|
118 | var filterKeys = directives['connection']['filter']
|
119 | ? directives['connection']['filter']
|
120 | : [];
|
121 | filterKeys.sort();
|
122 | var queryArgs_1 = args;
|
123 | var filteredArgs_1 = {};
|
124 | filterKeys.forEach(function (key) {
|
125 | filteredArgs_1[key] = queryArgs_1[key];
|
126 | });
|
127 | return directives['connection']['key'] + "(" + JSON.stringify(filteredArgs_1) + ")";
|
128 | }
|
129 | else {
|
130 | return directives['connection']['key'];
|
131 | }
|
132 | }
|
133 | var completeFieldName = fieldName;
|
134 | if (args) {
|
135 | var stringifiedArgs = fast_json_stable_stringify_1.default(args);
|
136 | completeFieldName += "(" + stringifiedArgs + ")";
|
137 | }
|
138 | if (directives) {
|
139 | Object.keys(directives).forEach(function (key) {
|
140 | if (KNOWN_DIRECTIVES.indexOf(key) !== -1)
|
141 | return;
|
142 | if (directives[key] && Object.keys(directives[key]).length) {
|
143 | completeFieldName += "@" + key + "(" + JSON.stringify(directives[key]) + ")";
|
144 | }
|
145 | else {
|
146 | completeFieldName += "@" + key;
|
147 | }
|
148 | });
|
149 | }
|
150 | return completeFieldName;
|
151 | }
|
152 | exports.getStoreKeyName = getStoreKeyName;
|
153 | function argumentsObjectFromField(field, variables) {
|
154 | if (field.arguments && field.arguments.length) {
|
155 | var argObj_1 = {};
|
156 | field.arguments.forEach(function (_a) {
|
157 | var name = _a.name, value = _a.value;
|
158 | return valueToObjectRepresentation(argObj_1, name, value, variables);
|
159 | });
|
160 | return argObj_1;
|
161 | }
|
162 | return null;
|
163 | }
|
164 | exports.argumentsObjectFromField = argumentsObjectFromField;
|
165 | function resultKeyNameFromField(field) {
|
166 | return field.alias ? field.alias.value : field.name.value;
|
167 | }
|
168 | exports.resultKeyNameFromField = resultKeyNameFromField;
|
169 | function isField(selection) {
|
170 | return selection.kind === 'Field';
|
171 | }
|
172 | exports.isField = isField;
|
173 | function isInlineFragment(selection) {
|
174 | return selection.kind === 'InlineFragment';
|
175 | }
|
176 | exports.isInlineFragment = isInlineFragment;
|
177 | function isIdValue(idObject) {
|
178 | return idObject &&
|
179 | idObject.type === 'id' &&
|
180 | typeof idObject.generated === 'boolean';
|
181 | }
|
182 | exports.isIdValue = isIdValue;
|
183 | function toIdValue(idConfig, generated) {
|
184 | if (generated === void 0) { generated = false; }
|
185 | return tslib_1.__assign({ type: 'id', generated: generated }, (typeof idConfig === 'string'
|
186 | ? { id: idConfig, typename: undefined }
|
187 | : idConfig));
|
188 | }
|
189 | exports.toIdValue = toIdValue;
|
190 | function isJsonValue(jsonObject) {
|
191 | return (jsonObject != null &&
|
192 | typeof jsonObject === 'object' &&
|
193 | jsonObject.type === 'json');
|
194 | }
|
195 | exports.isJsonValue = isJsonValue;
|
196 | function defaultValueFromVariable(node) {
|
197 | throw new ts_invariant_1.InvariantError("Variable nodes are not supported by valueFromNode");
|
198 | }
|
199 | function valueFromNode(node, onVariable) {
|
200 | if (onVariable === void 0) { onVariable = defaultValueFromVariable; }
|
201 | switch (node.kind) {
|
202 | case 'Variable':
|
203 | return onVariable(node);
|
204 | case 'NullValue':
|
205 | return null;
|
206 | case 'IntValue':
|
207 | return parseInt(node.value, 10);
|
208 | case 'FloatValue':
|
209 | return parseFloat(node.value);
|
210 | case 'ListValue':
|
211 | return node.values.map(function (v) { return valueFromNode(v, onVariable); });
|
212 | case 'ObjectValue': {
|
213 | var value = {};
|
214 | for (var _i = 0, _a = node.fields; _i < _a.length; _i++) {
|
215 | var field = _a[_i];
|
216 | value[field.name.value] = valueFromNode(field.value, onVariable);
|
217 | }
|
218 | return value;
|
219 | }
|
220 | default:
|
221 | return node.value;
|
222 | }
|
223 | }
|
224 | exports.valueFromNode = valueFromNode;
|
225 |
|
\ | No newline at end of file |