UNPKG

7.12 kBJavaScriptView Raw
1import { InvariantError } from "../globals/index.js";
2import { isNonNullObject } from "../common/objects.js";
3import { getFragmentFromSelection } from "./fragments.js";
4export function makeReference(id) {
5 return { __ref: String(id) };
6}
7export function isReference(obj) {
8 return Boolean(obj && typeof obj === 'object' && typeof obj.__ref === 'string');
9}
10export function isDocumentNode(value) {
11 return (isNonNullObject(value) &&
12 value.kind === "Document" &&
13 Array.isArray(value.definitions));
14}
15function isStringValue(value) {
16 return value.kind === 'StringValue';
17}
18function isBooleanValue(value) {
19 return value.kind === 'BooleanValue';
20}
21function isIntValue(value) {
22 return value.kind === 'IntValue';
23}
24function isFloatValue(value) {
25 return value.kind === 'FloatValue';
26}
27function isVariable(value) {
28 return value.kind === 'Variable';
29}
30function isObjectValue(value) {
31 return value.kind === 'ObjectValue';
32}
33function isListValue(value) {
34 return value.kind === 'ListValue';
35}
36function isEnumValue(value) {
37 return value.kind === 'EnumValue';
38}
39function isNullValue(value) {
40 return value.kind === 'NullValue';
41}
42export function valueToObjectRepresentation(argObj, name, value, variables) {
43 if (isIntValue(value) || isFloatValue(value)) {
44 argObj[name.value] = Number(value.value);
45 }
46 else if (isBooleanValue(value) || isStringValue(value)) {
47 argObj[name.value] = value.value;
48 }
49 else if (isObjectValue(value)) {
50 var nestedArgObj_1 = {};
51 value.fields.map(function (obj) {
52 return valueToObjectRepresentation(nestedArgObj_1, obj.name, obj.value, variables);
53 });
54 argObj[name.value] = nestedArgObj_1;
55 }
56 else if (isVariable(value)) {
57 var variableValue = (variables || {})[value.name.value];
58 argObj[name.value] = variableValue;
59 }
60 else if (isListValue(value)) {
61 argObj[name.value] = value.values.map(function (listValue) {
62 var nestedArgArrayObj = {};
63 valueToObjectRepresentation(nestedArgArrayObj, name, listValue, variables);
64 return nestedArgArrayObj[name.value];
65 });
66 }
67 else if (isEnumValue(value)) {
68 argObj[name.value] = value.value;
69 }
70 else if (isNullValue(value)) {
71 argObj[name.value] = null;
72 }
73 else {
74 throw __DEV__ ? new InvariantError("The inline argument \"".concat(name.value, "\" of kind \"").concat(value.kind, "\"") +
75 'is not supported. Use variables instead of inline arguments to ' +
76 'overcome this limitation.') : new InvariantError(54);
77 }
78}
79export 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}
103var KNOWN_DIRECTIVES = [
104 'connection',
105 'include',
106 'skip',
107 'client',
108 'rest',
109 'export',
110];
111export var getStoreKeyName = Object.assign(function (fieldName, args, directives) {
112 if (args &&
113 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 filteredArgs_1 = {};
123 filterKeys.forEach(function (key) {
124 filteredArgs_1[key] = args[key];
125 });
126 return "".concat(directives['connection']['key'], "(").concat(stringify(filteredArgs_1), ")");
127 }
128 else {
129 return directives['connection']['key'];
130 }
131 }
132 var completeFieldName = fieldName;
133 if (args) {
134 var stringifiedArgs = stringify(args);
135 completeFieldName += "(".concat(stringifiedArgs, ")");
136 }
137 if (directives) {
138 Object.keys(directives).forEach(function (key) {
139 if (KNOWN_DIRECTIVES.indexOf(key) !== -1)
140 return;
141 if (directives[key] && Object.keys(directives[key]).length) {
142 completeFieldName += "@".concat(key, "(").concat(stringify(directives[key]), ")");
143 }
144 else {
145 completeFieldName += "@".concat(key);
146 }
147 });
148 }
149 return completeFieldName;
150}, {
151 setStringify: function (s) {
152 var previous = stringify;
153 stringify = s;
154 return previous;
155 },
156});
157var stringify = function defaultStringify(value) {
158 return JSON.stringify(value, stringifyReplacer);
159};
160function stringifyReplacer(_key, value) {
161 if (isNonNullObject(value) && !Array.isArray(value)) {
162 value = Object.keys(value).sort().reduce(function (copy, key) {
163 copy[key] = value[key];
164 return copy;
165 }, {});
166 }
167 return value;
168}
169export function argumentsObjectFromField(field, variables) {
170 if (field.arguments && field.arguments.length) {
171 var argObj_1 = {};
172 field.arguments.forEach(function (_a) {
173 var name = _a.name, value = _a.value;
174 return valueToObjectRepresentation(argObj_1, name, value, variables);
175 });
176 return argObj_1;
177 }
178 return null;
179}
180export function resultKeyNameFromField(field) {
181 return field.alias ? field.alias.value : field.name.value;
182}
183export function getTypenameFromResult(result, selectionSet, fragmentMap) {
184 if (typeof result.__typename === 'string') {
185 return result.__typename;
186 }
187 for (var _i = 0, _a = selectionSet.selections; _i < _a.length; _i++) {
188 var selection = _a[_i];
189 if (isField(selection)) {
190 if (selection.name.value === '__typename') {
191 return result[resultKeyNameFromField(selection)];
192 }
193 }
194 else {
195 var typename = getTypenameFromResult(result, getFragmentFromSelection(selection, fragmentMap).selectionSet, fragmentMap);
196 if (typeof typename === 'string') {
197 return typename;
198 }
199 }
200 }
201}
202export function isField(selection) {
203 return selection.kind === 'Field';
204}
205export function isInlineFragment(selection) {
206 return selection.kind === 'InlineFragment';
207}
208//# sourceMappingURL=storeUtils.js.map
\No newline at end of file