apollo-progressive-fragment-matcher
Version:
A smart alternative to the introspection fragment matcher.
128 lines (101 loc) • 5.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PossibleTypesExtension = void 0;
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _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); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var unique = function unique(item, index, array) {
return array.indexOf(item) === index;
};
var PossibleTypesExtension =
/*#__PURE__*/
function () {
function PossibleTypesExtension() {
_classCallCheck(this, PossibleTypesExtension);
this.enabled = false;
this.schema = null;
}
_createClass(PossibleTypesExtension, [{
key: "requestDidStart",
value: function requestDidStart(_ref) {
var _ref$extensions = _ref.extensions;
_ref$extensions = _ref$extensions === void 0 ? {} : _ref$extensions;
var possibleTypes = _ref$extensions.possibleTypes,
rest = _objectWithoutProperties(_ref, ["extensions"]);
this.enabled = !!possibleTypes;
}
}, {
key: "willResolveField",
value: function willResolveField(root, args, context, _ref2) {
var schema = _ref2.schema;
this.schema = schema;
}
}, {
key: "willSendResponse",
value: function willSendResponse(_ref3) {
var graphqlResponse = _ref3.graphqlResponse,
context = _ref3.context;
if (this.enabled && this.schema) {
var types = this.extractTypes(graphqlResponse.data).filter(unique);
var possibleTypes = {};
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = types[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var type = _step.value;
possibleTypes[type] = this.schema.getType(type).getInterfaces().map(function (_ref4) {
var name = _ref4.name;
return name;
});
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
graphqlResponse.extensions = graphqlResponse.extensions || {};
graphqlResponse.extensions.possibleTypes = possibleTypes;
}
return {
graphqlResponse: graphqlResponse,
context: context
};
}
}, {
key: "extractTypes",
value: function extractTypes(obj) {
var types = [];
if (!obj) return types;
var _arr = Object.keys(obj);
for (var _i = 0; _i < _arr.length; _i++) {
var field = _arr[_i];
if (field === '__typename') types.push(obj[field]);
if (_typeof(obj[field]) === 'object') {
types.push.apply(types, _toConsumableArray(this.extractTypes(obj[field])));
}
}
return types;
}
}]);
return PossibleTypesExtension;
}();
exports.PossibleTypesExtension = PossibleTypesExtension;