123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- var tslib_1 = require("tslib");
- var fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify"));
- var ts_invariant_1 = require("ts-invariant");
- function isScalarValue(value) {
- return ['StringValue', 'BooleanValue', 'EnumValue'].indexOf(value.kind) > -1;
- }
- exports.isScalarValue = isScalarValue;
- function isNumberValue(value) {
- return ['IntValue', 'FloatValue'].indexOf(value.kind) > -1;
- }
- exports.isNumberValue = isNumberValue;
- function isStringValue(value) {
- return value.kind === 'StringValue';
- }
- function isBooleanValue(value) {
- return value.kind === 'BooleanValue';
- }
- function isIntValue(value) {
- return value.kind === 'IntValue';
- }
- function isFloatValue(value) {
- return value.kind === 'FloatValue';
- }
- function isVariable(value) {
- return value.kind === 'Variable';
- }
- function isObjectValue(value) {
- return value.kind === 'ObjectValue';
- }
- function isListValue(value) {
- return value.kind === 'ListValue';
- }
- function isEnumValue(value) {
- return value.kind === 'EnumValue';
- }
- function isNullValue(value) {
- return value.kind === 'NullValue';
- }
- function valueToObjectRepresentation(argObj, name, value, variables) {
- if (isIntValue(value) || isFloatValue(value)) {
- argObj[name.value] = Number(value.value);
- }
- else if (isBooleanValue(value) || isStringValue(value)) {
- argObj[name.value] = value.value;
- }
- else if (isObjectValue(value)) {
- var nestedArgObj_1 = {};
- value.fields.map(function (obj) {
- return valueToObjectRepresentation(nestedArgObj_1, obj.name, obj.value, variables);
- });
- argObj[name.value] = nestedArgObj_1;
- }
- else if (isVariable(value)) {
- var variableValue = (variables || {})[value.name.value];
- argObj[name.value] = variableValue;
- }
- else if (isListValue(value)) {
- argObj[name.value] = value.values.map(function (listValue) {
- var nestedArgArrayObj = {};
- valueToObjectRepresentation(nestedArgArrayObj, name, listValue, variables);
- return nestedArgArrayObj[name.value];
- });
- }
- else if (isEnumValue(value)) {
- argObj[name.value] = value.value;
- }
- else if (isNullValue(value)) {
- argObj[name.value] = null;
- }
- else {
- throw new ts_invariant_1.InvariantError("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\"" +
- 'is not supported. Use variables instead of inline arguments to ' +
- 'overcome this limitation.');
- }
- }
- exports.valueToObjectRepresentation = valueToObjectRepresentation;
- function storeKeyNameFromField(field, variables) {
- var directivesObj = null;
- if (field.directives) {
- directivesObj = {};
- field.directives.forEach(function (directive) {
- directivesObj[directive.name.value] = {};
- if (directive.arguments) {
- directive.arguments.forEach(function (_a) {
- var name = _a.name, value = _a.value;
- return valueToObjectRepresentation(directivesObj[directive.name.value], name, value, variables);
- });
- }
- });
- }
- var argObj = null;
- if (field.arguments && field.arguments.length) {
- argObj = {};
- field.arguments.forEach(function (_a) {
- var name = _a.name, value = _a.value;
- return valueToObjectRepresentation(argObj, name, value, variables);
- });
- }
- return getStoreKeyName(field.name.value, argObj, directivesObj);
- }
- exports.storeKeyNameFromField = storeKeyNameFromField;
- var KNOWN_DIRECTIVES = [
- 'connection',
- 'include',
- 'skip',
- 'client',
- 'rest',
- 'export',
- ];
- function getStoreKeyName(fieldName, args, directives) {
- if (directives &&
- directives['connection'] &&
- directives['connection']['key']) {
- if (directives['connection']['filter'] &&
- directives['connection']['filter'].length > 0) {
- var filterKeys = directives['connection']['filter']
- ? directives['connection']['filter']
- : [];
- filterKeys.sort();
- var queryArgs_1 = args;
- var filteredArgs_1 = {};
- filterKeys.forEach(function (key) {
- filteredArgs_1[key] = queryArgs_1[key];
- });
- return directives['connection']['key'] + "(" + JSON.stringify(filteredArgs_1) + ")";
- }
- else {
- return directives['connection']['key'];
- }
- }
- var completeFieldName = fieldName;
- if (args) {
- var stringifiedArgs = fast_json_stable_stringify_1.default(args);
- completeFieldName += "(" + stringifiedArgs + ")";
- }
- if (directives) {
- Object.keys(directives).forEach(function (key) {
- if (KNOWN_DIRECTIVES.indexOf(key) !== -1)
- return;
- if (directives[key] && Object.keys(directives[key]).length) {
- completeFieldName += "@" + key + "(" + JSON.stringify(directives[key]) + ")";
- }
- else {
- completeFieldName += "@" + key;
- }
- });
- }
- return completeFieldName;
- }
- exports.getStoreKeyName = getStoreKeyName;
- function argumentsObjectFromField(field, variables) {
- if (field.arguments && field.arguments.length) {
- var argObj_1 = {};
- field.arguments.forEach(function (_a) {
- var name = _a.name, value = _a.value;
- return valueToObjectRepresentation(argObj_1, name, value, variables);
- });
- return argObj_1;
- }
- return null;
- }
- exports.argumentsObjectFromField = argumentsObjectFromField;
- function resultKeyNameFromField(field) {
- return field.alias ? field.alias.value : field.name.value;
- }
- exports.resultKeyNameFromField = resultKeyNameFromField;
- function isField(selection) {
- return selection.kind === 'Field';
- }
- exports.isField = isField;
- function isInlineFragment(selection) {
- return selection.kind === 'InlineFragment';
- }
- exports.isInlineFragment = isInlineFragment;
- function isIdValue(idObject) {
- return idObject &&
- idObject.type === 'id' &&
- typeof idObject.generated === 'boolean';
- }
- exports.isIdValue = isIdValue;
- function toIdValue(idConfig, generated) {
- if (generated === void 0) { generated = false; }
- return tslib_1.__assign({ type: 'id', generated: generated }, (typeof idConfig === 'string'
- ? { id: idConfig, typename: undefined }
- : idConfig));
- }
- exports.toIdValue = toIdValue;
- function isJsonValue(jsonObject) {
- return (jsonObject != null &&
- typeof jsonObject === 'object' &&
- jsonObject.type === 'json');
- }
- exports.isJsonValue = isJsonValue;
- function defaultValueFromVariable(node) {
- throw new ts_invariant_1.InvariantError("Variable nodes are not supported by valueFromNode");
- }
- function valueFromNode(node, onVariable) {
- if (onVariable === void 0) { onVariable = defaultValueFromVariable; }
- switch (node.kind) {
- case 'Variable':
- return onVariable(node);
- case 'NullValue':
- return null;
- case 'IntValue':
- return parseInt(node.value, 10);
- case 'FloatValue':
- return parseFloat(node.value);
- case 'ListValue':
- return node.values.map(function (v) { return valueFromNode(v, onVariable); });
- case 'ObjectValue': {
- var value = {};
- for (var _i = 0, _a = node.fields; _i < _a.length; _i++) {
- var field = _a[_i];
- value[field.name.value] = valueFromNode(field.value, onVariable);
- }
- return value;
- }
- default:
- return node.value;
- }
- }
- exports.valueFromNode = valueFromNode;
- //# sourceMappingURL=storeUtils.js.map
|