apollo-magic-refetch
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -8,10 +8,16 @@ "use strict"; | ||
}); | ||
exports.default = doesQueryContain; | ||
exports["default"] = doesQueryContain; | ||
var _getPotentialAncestors = _interopRequireDefault(require("./getPotentialAncestors")); | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function doesQueryContain(document, types, typename, data, predicate) { | ||
var targetType = types[typename]; | ||
if (!targetType) throw new Error("type not found: ".concat(typename)); | ||
var potentialAncestors = (0, _getPotentialAncestors.default)(targetType); | ||
var potentialAncestors = (0, _getPotentialAncestors["default"])(targetType); | ||
@@ -29,8 +35,7 @@ function doesNodeContain(node, data, type) { | ||
if (selectionSet) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
var _iterator = _createForOfIteratorHelper(selectionSet.selections), | ||
_step; | ||
try { | ||
for (var _iterator = selectionSet.selections[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var selection = _step.value; | ||
@@ -58,8 +63,8 @@ var alias = selection.alias; | ||
if (!Array.isArray(innerData)) continue; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
var _iterator2 = _createForOfIteratorHelper(innerData), | ||
_step2; | ||
try { | ||
for (var _iterator2 = innerData[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { | ||
var element = _step2.value; | ||
@@ -72,14 +77,5 @@ | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
_iterator2.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
_iterator2.f(); | ||
} | ||
@@ -93,14 +89,5 @@ } else { | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
_iterator.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
_iterator.f(); | ||
} | ||
@@ -112,8 +99,7 @@ } | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
var _iterator3 = _createForOfIteratorHelper(document.definitions), | ||
_step3; | ||
try { | ||
for (var _iterator3 = document.definitions[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { | ||
var node = _step3.value; | ||
@@ -126,14 +112,5 @@ | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
_iterator3.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return != null) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
_iterator3.f(); | ||
} | ||
@@ -140,0 +117,0 @@ |
@@ -70,2 +70,3 @@ "use strict"; | ||
if (!observableQuery) continue; | ||
if (observableQuery.options.fetchPolicy === 'cache-only') continue; | ||
let data; | ||
@@ -72,0 +73,0 @@ const currentResult = observableQuery.currentResult(); |
@@ -12,3 +12,3 @@ "use strict"; | ||
var _default = _graphqlTag.default` | ||
const typesQuery = (0, _graphqlTag.default)` | ||
{ | ||
@@ -40,3 +40,3 @@ __schema { | ||
`; | ||
var _default = typesQuery; | ||
exports.default = _default; |
@@ -6,3 +6,10 @@ "use strict"; | ||
}); | ||
exports.default = getPotentialAncestors; | ||
exports["default"] = getPotentialAncestors; | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
var cache = new Map(); | ||
@@ -22,8 +29,7 @@ | ||
if (parents) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
var _iterator = _createForOfIteratorHelper(parents), | ||
_step; | ||
try { | ||
for (var _iterator = parents[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var field = _step.value; | ||
@@ -45,14 +51,5 @@ var name = field.name, | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
_iterator.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
_iterator.f(); | ||
} | ||
@@ -59,0 +56,0 @@ } |
@@ -9,3 +9,3 @@ "use strict"; | ||
exports.linkTypes = linkTypes; | ||
exports.default = getSchemaTypes; | ||
exports["default"] = getSchemaTypes; | ||
@@ -16,2 +16,8 @@ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function convertRawField(_ref) { | ||
@@ -28,8 +34,8 @@ var name = _ref.name, | ||
var convertedFields = {}; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
var _iterator = _createForOfIteratorHelper(fields), | ||
_step; | ||
try { | ||
for (var _iterator = fields[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var field = _step.value; | ||
@@ -39,14 +45,5 @@ convertedFields[field.name] = convertRawField(field); | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
_iterator.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
_iterator.f(); | ||
} | ||
@@ -72,8 +69,8 @@ | ||
var types = {}; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
var _iterator2 = _createForOfIteratorHelper(rawTypes), | ||
_step2; | ||
try { | ||
for (var _iterator2 = rawTypes[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { | ||
var rawType = _step2.value; | ||
@@ -87,14 +84,5 @@ var _name3 = rawType.name; | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
_iterator2.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
_iterator2.f(); | ||
} | ||
@@ -140,8 +128,6 @@ | ||
function _getSchemaTypes() { | ||
_getSchemaTypes = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee(fetchTypeMetadata) { | ||
var _ref3, types; | ||
_getSchemaTypes = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(fetchTypeMetadata) { | ||
var _yield$fetchTypeMetad, types; | ||
return _regenerator.default.wrap(function _callee$(_context) { | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
@@ -154,4 +140,4 @@ switch (_context.prev = _context.next) { | ||
case 2: | ||
_ref3 = _context.sent; | ||
types = _ref3.data.__schema.types; | ||
_yield$fetchTypeMetad = _context.sent; | ||
types = _yield$fetchTypeMetad.data.__schema.types; | ||
return _context.abrupt("return", linkTypes(types)); | ||
@@ -164,5 +150,5 @@ | ||
} | ||
}, _callee, this); | ||
}, _callee); | ||
})); | ||
return _getSchemaTypes.apply(this, arguments); | ||
} |
139
index.js
@@ -8,14 +8,14 @@ "use strict"; | ||
}); | ||
exports.default = refetch; | ||
exports["default"] = refetch; | ||
Object.defineProperty(exports, "typesQuery", { | ||
enumerable: true, | ||
get: function get() { | ||
return _typesQuery.default; | ||
return _typesQuery["default"]; | ||
} | ||
}); | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
@@ -31,2 +31,8 @@ | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function normalizePredicate(predicate, idField) { | ||
@@ -42,8 +48,7 @@ if (typeof predicate === 'function') return predicate; | ||
function every(array, predicate) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
var _iterator = _createForOfIteratorHelper(array), | ||
_step; | ||
try { | ||
for (var _iterator = array[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var _elem = _step.value; | ||
@@ -53,14 +58,5 @@ if (!predicate(_elem)) return false; | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
_iterator.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
_iterator.f(); | ||
} | ||
@@ -78,8 +74,6 @@ | ||
function _refetch() { | ||
_refetch = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee3(client, typenameOrTerms, predicate, idField) { | ||
var types, terms, queries, promises, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _loop, _iterator2, _step2, _ret; | ||
_refetch = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(client, typenameOrTerms, predicate, idField) { | ||
var types, terms, queries, promises, _iterator2, _step2, _loop, _ret; | ||
return _regenerator.default.wrap(function _callee3$(_context3) { | ||
return _regenerator["default"].wrap(function _callee3$(_context3) { | ||
while (1) { | ||
@@ -127,6 +121,4 @@ switch (_context3.prev = _context3.next) { | ||
promises = []; | ||
_iteratorNormalCompletion2 = true; | ||
_didIteratorError2 = false; | ||
_iteratorError2 = undefined; | ||
_context3.prev = 19; | ||
_iterator2 = _createForOfIteratorHelper(queries.values()); | ||
_context3.prev = 17; | ||
@@ -138,2 +130,3 @@ _loop = function _loop() { | ||
if (!observableQuery) return "continue"; | ||
if (observableQuery.options.fetchPolicy === 'cache-only') return "continue"; | ||
var data = void 0; | ||
@@ -144,3 +137,3 @@ var currentResult = observableQuery.currentResult(); | ||
if (every(terms, function (_ref3) { | ||
var _ref4 = (0, _slicedToArray2.default)(_ref3, 3), | ||
var _ref4 = (0, _slicedToArray2["default"])(_ref3, 3), | ||
typename = _ref4[0], | ||
@@ -150,3 +143,3 @@ predicate = _ref4[1], | ||
return (0, _doesQueryContain.default)(document, types, typename, data, predicate != null ? normalizePredicate(predicate, idField || 'id') : null); | ||
return (0, _doesQueryContain["default"])(document, types, typename, data, predicate != null ? normalizePredicate(predicate, idField || 'id') : null); | ||
})) { | ||
@@ -157,7 +150,7 @@ promises.push(observableQuery.refetch()); | ||
_iterator2 = queries.values()[Symbol.iterator](); | ||
_iterator2.s(); | ||
case 22: | ||
if (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) { | ||
_context3.next = 29; | ||
case 20: | ||
if ((_step2 = _iterator2.n()).done) { | ||
_context3.next = 26; | ||
break; | ||
@@ -169,52 +162,34 @@ } | ||
if (!(_ret === "continue")) { | ||
_context3.next = 26; | ||
_context3.next = 24; | ||
break; | ||
} | ||
return _context3.abrupt("continue", 26); | ||
return _context3.abrupt("continue", 24); | ||
case 26: | ||
_iteratorNormalCompletion2 = true; | ||
_context3.next = 22; | ||
case 24: | ||
_context3.next = 20; | ||
break; | ||
case 29: | ||
_context3.next = 35; | ||
case 26: | ||
_context3.next = 31; | ||
break; | ||
case 28: | ||
_context3.prev = 28; | ||
_context3.t0 = _context3["catch"](17); | ||
_iterator2.e(_context3.t0); | ||
case 31: | ||
_context3.prev = 31; | ||
_context3.t0 = _context3["catch"](19); | ||
_didIteratorError2 = true; | ||
_iteratorError2 = _context3.t0; | ||
case 35: | ||
_context3.prev = 35; | ||
_context3.prev = 36; | ||
_iterator2.f(); | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
} | ||
return _context3.finish(31); | ||
case 38: | ||
_context3.prev = 38; | ||
if (!_didIteratorError2) { | ||
_context3.next = 41; | ||
break; | ||
} | ||
throw _iteratorError2; | ||
case 41: | ||
return _context3.finish(38); | ||
case 42: | ||
return _context3.finish(35); | ||
case 43: | ||
_context3.next = 45; | ||
case 34: | ||
_context3.next = 36; | ||
return promises; | ||
case 45: | ||
case 36: | ||
case "end": | ||
@@ -224,3 +199,3 @@ return _context3.stop(); | ||
} | ||
}, _callee3, this, [[19, 31, 35, 43], [36,, 38, 42]]); | ||
}, _callee3, null, [[17, 28, 31, 34]]); | ||
})); | ||
@@ -230,9 +205,5 @@ return _refetch.apply(this, arguments); | ||
refetch.fetchTypeMetadata = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee(client) { | ||
return _regenerator.default.wrap(function _callee$(_context) { | ||
refetch.fetchTypeMetadata = /*#__PURE__*/function () { | ||
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(client) { | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
@@ -243,3 +214,3 @@ switch (_context.prev = _context.next) { | ||
refetch.setTypeMetadata(client.query({ | ||
query: _typesQuery.default | ||
query: _typesQuery["default"] | ||
})); | ||
@@ -268,3 +239,3 @@ } // istanbul ignore next | ||
} | ||
}, _callee, this); | ||
}, _callee); | ||
})); | ||
@@ -278,8 +249,4 @@ | ||
refetch.setTypeMetadata = function (metadata) { | ||
typesPromise = (0, _getSchemaTypes.default)( | ||
/*#__PURE__*/ | ||
(0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee2() { | ||
return _regenerator.default.wrap(function _callee2$(_context2) { | ||
typesPromise = (0, _getSchemaTypes["default"])( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() { | ||
return _regenerator["default"].wrap(function _callee2$(_context2) { | ||
while (1) { | ||
@@ -299,4 +266,4 @@ switch (_context2.prev = _context2.next) { | ||
} | ||
}, _callee2, this); | ||
}, _callee2); | ||
}))); | ||
}; |
@@ -22,2 +22,1 @@ The MIT License (MIT) | ||
SOFTWARE. | ||
120
package.json
{ | ||
"name": "apollo-magic-refetch", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "magically refetches relevant queries after creates and deletes", | ||
"main": "index.js", | ||
"module": "es/index.js", | ||
"modules.root": "es", | ||
"sideEffects": false, | ||
"scripts": { | ||
"lint": "eslint src test --cache", | ||
"lint:fix": "eslint --fix src test --cache", | ||
"lint:watch": "esw --watch src test --cache", | ||
"prettier": "prettier --write src/**/*.js test/**/*.js", | ||
"prettier:check": "prettier --list-different src/**/*.js test/**/*.js", | ||
"lint": "eslint $npm_package_config_eslint", | ||
"lint:fix": "eslint --fix $npm_package_config_eslint", | ||
"prettier": "prettier --write $npm_package_config_prettier", | ||
"prettier:check": "prettier --list-different $npm_package_config_prettier", | ||
"flow": "flow", | ||
"flow:coverage": "for file in src/**.js test/**.js; do echo $file; flow coverage $file; done", | ||
"flow:watch": "flow-watch -e js,js.flow,flowconfig --ignore es/ --ignore node_modules/ --watch .flowconfig --watch src/ --watch test/", | ||
"clean": "rimraf es lib $(cd src; ls) *.js.flow", | ||
"build": "npm run clean && babel src --out-dir es && flow-copy-source -v src/ es && cross-env BABEL_ENV=es5 babel src --out-dir . && flow-copy-source -v src/ .", | ||
"test": "cross-env NODE_ENV=production BABEL_ENV=es5 mocha $npm_package_config_mocha && cross-env NODE_ENV=production BABEL_ENV=coverage nyc --reporter=lcov --reporter=text mocha $npm_package_config_mocha", | ||
"test:watch": "cross-env NODE_ENV=production BABEL_ENV=test mocha --watch $npm_package_config_mocha", | ||
"test:debug": "cross-env NODE_ENV=production BABEL_ENV=test mocha --inspect-brk $npm_package_config_mocha", | ||
"clean": "rimraf es lib $(cd src; ls) *.js.flow *.d.ts", | ||
"build": "npm run clean && babel src --out-dir es && flow-copy-source -v src/ es && copy src/**/*.d.ts es && cross-env BABEL_ENV=es5 babel src --out-dir . && flow-copy-source -v src/ . && copy src/**/*.d.ts .", | ||
"test": "cross-env NODE_ENV=test BABEL_ENV=es5 mocha $npm_package_config_mocha && cross-env NODE_ENV=test BABEL_ENV=coverage nyc --reporter=lcov --reporter=text mocha $npm_package_config_mocha", | ||
"test:watch": "cross-env NODE_ENV=test BABEL_ENV=test mocha --watch $npm_package_config_mocha", | ||
"test:debug": "cross-env NODE_ENV=test BABEL_ENV=test mocha --inspect-brk $npm_package_config_mocha", | ||
"codecov": "nyc report --reporter=text-lcov > coverage.lcov; codecov", | ||
"prepublishOnly": "npm run clean && npm run prettier:check && npm run lint && flow && npm test && npm run build", | ||
"open:coverage": "open coverage/lcov-report/index.html", | ||
"semantic-release": "semantic-release", | ||
"travis-deploy-once": "travis-deploy-once" | ||
"semantic-release": "semantic-release" | ||
}, | ||
@@ -32,3 +27,3 @@ "husky": { | ||
"pre-commit": "lint-staged && npm run lint && flow", | ||
"commit-msg": "commitlint -e $GIT_PARAMS", | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS", | ||
"pre-push": "npm test" | ||
@@ -38,9 +33,20 @@ } | ||
"lint-staged": { | ||
"*.{js,json,css,md}": [ | ||
"prettier --write", | ||
"git add" | ||
"*.{js,json,css,md,ts}": [ | ||
"prettier --write" | ||
] | ||
}, | ||
"commitlint": { | ||
"extends": [ | ||
"@jedwards1211/commitlint-config" | ||
] | ||
}, | ||
"prettier": { | ||
"semi": false, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
}, | ||
"config": { | ||
"mocha": "-r @babel/register ./test/**/*.js", | ||
"mocha": "-r @babel/register test/configure.js 'test/**/*.js'", | ||
"eslint": "src test --cache", | ||
"prettier": "*.{json,md,js} {src,test}/**/*.{js,ts}", | ||
"commitizen": { | ||
@@ -79,46 +85,40 @@ "path": "cz-conventional-changelog" | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.5", | ||
"@babel/core": "^7.1.6", | ||
"@babel/plugin-proposal-class-properties": "^7.1.0", | ||
"@babel/plugin-proposal-export-default-from": "^7.0.0", | ||
"@babel/plugin-proposal-export-namespace-from": "^7.0.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", | ||
"@babel/plugin-syntax-dynamic-import": "^7.0.0", | ||
"@babel/plugin-transform-runtime": "^7.1.0", | ||
"@babel/preset-env": "^7.1.6", | ||
"@babel/preset-flow": "^7.0.0", | ||
"@babel/register": "^7.0.0", | ||
"@commitlint/cli": "^6.0.2", | ||
"@commitlint/config-conventional": "^6.0.2", | ||
"@jedwards1211/commitlint-config": "^1.0.0", | ||
"@jedwards1211/eslint-config": "^2.0.0", | ||
"@jedwards1211/eslint-config-flow": "^1.0.2", | ||
"@babel/cli": "^7.15.7", | ||
"@babel/core": "^7.15.5", | ||
"@babel/eslint-parser": "^7.15.7", | ||
"@babel/plugin-proposal-export-default-from": "^7.14.5", | ||
"@babel/plugin-proposal-export-namespace-from": "^7.14.5", | ||
"@babel/plugin-transform-runtime": "^7.15.0", | ||
"@babel/preset-env": "^7.15.6", | ||
"@babel/preset-flow": "^7.14.5", | ||
"@babel/register": "^7.15.3", | ||
"@commitlint/cli": "^13.2.0", | ||
"@commitlint/config-conventional": "^13.2.0", | ||
"@jedwards1211/commitlint-config": "^1.0.2", | ||
"@jedwards1211/eslint-config": "^2.0.2", | ||
"@jedwards1211/eslint-config-flow": "^3.0.1", | ||
"apollo-cache-inmemory": "^1.3.12", | ||
"apollo-client": "^2.4.8", | ||
"apollo-link-schema": "^1.1.4", | ||
"babel-eslint": "^10.0.1", | ||
"babel-plugin-istanbul": "^5.1.0", | ||
"chai": "^4.2.0", | ||
"codecov": "^3.1.0", | ||
"babel-plugin-istanbul": "^6.0.0", | ||
"chai": "^4.3.4", | ||
"codecov": "^3.8.3", | ||
"copy": "^0.3.2", | ||
"cross-env": "^5.2.0", | ||
"eslint": "^5.9.0", | ||
"eslint-config-prettier": "^3.3.0", | ||
"eslint-plugin-flowtype": "^3.2.0", | ||
"eslint-watch": "^4.0.2", | ||
"flow-bin": "^0.84.0", | ||
"flow-copy-source": "^2.0.2", | ||
"flow-watch": "^1.1.4", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^7.32.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-flowtype": "^6.1.0", | ||
"flow-bin": "^0.161.0", | ||
"flow-copy-source": "https://github.com/jedwards1211/flow-copy-source#no-watch", | ||
"graphql": "^14.0.2", | ||
"graphql-tools": "^4.0.3", | ||
"husky": "^1.1.4", | ||
"istanbul": "^0.4.5", | ||
"lint-staged": "^8.0.4", | ||
"mocha": "^5.2.0", | ||
"nyc": "^13.1.0", | ||
"prettier": "^1.15.2", | ||
"prettier-eslint": "^8.8.2", | ||
"rimraf": "^2.6.0", | ||
"semantic-release": "^15.8.0", | ||
"travis-deploy-once": "^5.0.9" | ||
"husky": "^7.0.2", | ||
"lint-staged": "^11.1.2", | ||
"mocha": "^9.1.2", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.4.1", | ||
"prettier-eslint": "^13.0.0", | ||
"rimraf": "^3.0.2", | ||
"semantic-release": "^18.0.0", | ||
"typescript": "^4.4.3" | ||
}, | ||
@@ -133,5 +133,5 @@ "optionalDependencies": { | ||
"dependencies": { | ||
"@babel/runtime": "^7.1.5", | ||
"@babel/runtime": "^7.15.4", | ||
"graphql-tag": "^2.9.2" | ||
} | ||
} |
115
README.md
# apollo-magic-refetch | ||
[![Build Status](https://travis-ci.org/jcoreio/apollo-magic-refetch.svg?branch=master)](https://travis-ci.org/jcoreio/apollo-magic-refetch) | ||
[![CircleCI](https://circleci.com/gh/jcoreio/apollo-magic-refetch.svg?style=svg)](https://circleci.com/gh/jcoreio/apollo-magic-refetch) | ||
[![Coverage Status](https://codecov.io/gh/jcoreio/apollo-magic-refetch/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/apollo-magic-refetch) | ||
@@ -32,3 +32,6 @@ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) | ||
- [API](#api) | ||
- [`refetch(client, typenameOrTerms, [ids], [idField])`](#refetchclient-typenameorterms-ids-idfield) | ||
- [`refetch(client, typenameOrTerms, [predicate, [idField]])`](#refetchclient-typenameorterms-predicate-idfield) | ||
- [`refetch.fetchTypeMetadata(client)`](#refetchfetchtypemetadataclient) | ||
- [`refetch.setTypeMetadata(typeMetadataPromise)`](#refetchsettypemetadatatypemetadatapromise) | ||
- [`typesQuery`](#typesquery) | ||
@@ -53,8 +56,6 @@ ## How it works | ||
Interfaces and union types are not supported yet. This means if they are | ||
anywhere in your results, this library may fail to refetch when it should. | ||
- Interfaces and union types are not supported yet. This means if they are | ||
anywhere in your results, this library may fail to refetch when it should. | ||
- Lists of lists are not supported yet. | ||
Also, lists of lists are not supported, if for whatever reason you are using | ||
lists of lists in your schema (I haven't even checked if this is possible). | ||
## ES environment requirements | ||
@@ -65,3 +66,3 @@ | ||
If you are not using a bundler that supports the `modules.root` property in | ||
If you are not using a bundler that supports the `module` property in | ||
`package.json`, make sure to install `babel-runtime`. | ||
@@ -71,3 +72,4 @@ | ||
`apollo-magic-refetch` uses type metadata that it must fetch from GraphQL. | ||
`apollo-magic-refetch` uses type metadata from GraphQL determine which queries | ||
need to be refetched; the client must get this metadata from the server. | ||
If your schema is large enough it may be a prohibitive amount of metadata. | ||
@@ -85,6 +87,7 @@ `refetch` operations will be delayed until this metadata is fetched. | ||
If your server forbids introspection queries, you will have to fetch it by | ||
other means. For instance, you could set up the following route on your server: | ||
If your server forbids client introspection queries, you will have to fetch the | ||
metadata by other means. For instance, you could execute the required introspection | ||
query on the server, and serve the result on a custom REST route: | ||
````js | ||
```js | ||
import { execute } from 'graphql' | ||
@@ -100,4 +103,5 @@ import schema from './path/to/your/graphql/schema' | ||
app.get('/graphql/refetchTypeMetadata', (req, res) => { | ||
typeMetadataPromise.then(data => res.json(data)) | ||
})``` | ||
typeMetadataPromise.then((data) => res.json(data)) | ||
}) | ||
``` | ||
@@ -112,13 +116,15 @@ And then pass this data to `refetch.setTypeMetadata` **before you ever call | ||
refetch.setTypeMetadata( | ||
fetch('/graphql/refetchTypeMetadata').then(res => res.json()) | ||
fetch('/graphql/refetchTypeMetadata').then((res) => res.json()) | ||
) | ||
```` | ||
``` | ||
## Handling Deletions | ||
In this example, the `__typename` of the object being deleted is `Device` and it | ||
uses the standard `id` field. If instead the field were called `tag`, for | ||
instance, you would pass `'tag'` after `deviceId` in the call to `refetch`. | ||
Typically you call `refetch` within the `update` callback of your `Mutation` | ||
that deletes objects. You just have to call `refetch` with the `__typename` | ||
that was deleted (in this case, `Device`) and the `id` of the deleted object. | ||
This refetches any active queries that contain the deleted object in cached data. | ||
You may pass an array or `Set` of ids in place of a single `deviceId`. | ||
For mutations that delete multiple things at once, you may pass an array or `Set` | ||
of ids to `refetch`, or make multiple calls to `refetch` in your `update` method. | ||
@@ -155,6 +161,13 @@ ```js | ||
When you omit the id parameter, `refetch` refetches all active queries that | ||
contain the requested `__typename` in their selections, regardless of what ids | ||
are actually in their results. This can be used after creating an object. | ||
Typically you call `refetch` within the `update` callback of your `Mutation` | ||
that creates objects. You just have to call `refetch` with the `__typename` | ||
that was created. | ||
Unlike deletions, you don't pass the `id` of the created | ||
object. Without a specific `id` to search for, it simply refetches all active | ||
queries that contain any object of the requested `__typename` in their cached | ||
data, in case the created object belongs in the new results. This is less | ||
efficient than refetching queries containing a specific `id`, but far easier | ||
than manually inserting the created object into each relevant query. | ||
In this example, the `__typename` of the object being created is `Device`. | ||
@@ -202,3 +215,6 @@ | ||
```js | ||
refetch(client, [['User', userIds], ['Organization', organizationId]]) | ||
refetch(client, [ | ||
['User', userIds], | ||
['Organization', organizationId], | ||
]) | ||
``` | ||
@@ -319,3 +335,3 @@ | ||
<ApolloConsumer> | ||
{client => ( | ||
{(client) => ( | ||
<Mutation | ||
@@ -327,5 +343,5 @@ mutation={mutation} | ||
> | ||
{createUser => ( | ||
{(createUser) => ( | ||
<CreateUserForm | ||
onSubmit={values => | ||
onSubmit={(values) => | ||
createUser({ | ||
@@ -345,4 +361,11 @@ variables: { organizationId, values }, | ||
### `refetch(client, typenameOrTerms, [predicate], [idField])` | ||
### `refetch(client, typenameOrTerms, [predicate, [idField]])` | ||
```js | ||
import refetch from 'apollo-magic-refetch' | ||
``` | ||
Scans active queries in the given `ApolloClient` and refetches any that contain | ||
data matching the given type(s)/id(s). | ||
#### Arguments | ||
@@ -373,1 +396,37 @@ | ||
`predicate` is not an id, array, or `Set` of ids, rather than a `function`. | ||
### `refetch.fetchTypeMetadata(client)` | ||
Prefetches type metadata by running an introspection query on the given on | ||
`ApolloClient`. The server must support client introspection queries; | ||
otherwise use [`refetch.setTypeMetadata`](#refetchsettypemetadatatypemetadatapromise). | ||
#### Arguments | ||
##### `client: ApolloClient` | ||
The client to fetch type metadata from. | ||
### `refetch.setTypeMetadata(typeMetadataPromise)` | ||
Sets the type metadata to use for determing which queries to refetch. | ||
Use this method if your server forbids client introspection queries. | ||
#### Arguments | ||
##### `typeMetadataPromise: TypeMetadata | Promise<TypeMetadata>` | ||
The result of executing the [`typesQuery`](#typesquery) GraphQL query | ||
or a `Promise` that will resolve to the result. | ||
### `typesQuery` | ||
```js | ||
import { typesQuery } from 'apollo-magic-refetch' | ||
``` | ||
The parsed GraphQL introspection query that gets all of the type metadata | ||
needed to determine which queries to refetch. Use this if your server forbids | ||
client introspection queries; execute this query on the server side and send | ||
the result to the client code that calls | ||
[`refetch.setTypeMetadata`](#refetchsettypemetadatatypemetadatapromise). |
@@ -8,3 +8,3 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
exports["default"] = void 0; | ||
@@ -15,14 +15,6 @@ var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral")); | ||
function _templateObject() { | ||
var data = (0, _taggedTemplateLiteral2.default)(["\n {\n __schema {\n types {\n name\n fields {\n name\n type {\n name\n kind\n ofType {\n name\n kind\n ofType {\n name\n kind\n ofType {\n name\n }\n }\n }\n }\n }\n }\n }\n }\n"]); | ||
var _templateObject; | ||
_templateObject = function _templateObject() { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
var _default = (0, _graphqlTag.default)(_templateObject()); | ||
exports.default = _default; | ||
var typesQuery = (0, _graphqlTag["default"])(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["\n {\n __schema {\n types {\n name\n fields {\n name\n type {\n name\n kind\n ofType {\n name\n kind\n ofType {\n name\n kind\n ofType {\n name\n }\n }\n }\n }\n }\n }\n }\n }\n"]))); | ||
var _default = typesQuery; | ||
exports["default"] = _default; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38
420
67759
23
769
Updated@babel/runtime@^7.15.4