UNPKG

16.6 kBJavaScriptView Raw
1exports.__esModule=!0,exports.assertIdValue=function(e){(0,a.invariant)((0,r.isIdValue)(e),11)},exports.defaultDataIdFromObject=R,exports.defaultNormalizedCacheFactory=function(e){return new m(e)},exports.enhanceErrorWithDocument=v,exports.WriteError=exports.StoreWriter=exports.StoreReader=exports.ObjectCache=exports.IntrospectionFragmentMatcher=exports.InMemoryCache=exports.HeuristicFragmentMatcher=void 0;var e=require("tslib"),t=require("apollo-cache"),r=require("apollo-utilities"),i=require("optimism"),a=require("ts-invariant"),n=!1;function o(){var e=!n;return(0,r.isTest)()||(n=!0),e}var s=function(){function e(){}return e.prototype.ensureReady=function(){return Promise.resolve()},e.prototype.canBypassInit=function(){return!0},e.prototype.match=function(e,t,r){var i=r.store.get(e.id),a="ROOT_QUERY"===e.id;if(!i)return a;var n=i.__typename,s=void 0===n?a&&"Query":n;return s&&s===t||(o(),"heuristic")},e}();exports.HeuristicFragmentMatcher=s;var c=function(){function e(e){e&&e.introspectionQueryResultData?(this.possibleTypesMap=this.parseIntrospectionResult(e.introspectionQueryResultData),this.isReady=!0):this.isReady=!1,this.match=this.match.bind(this)}return e.prototype.match=function(e,t,r){(0,a.invariant)(this.isReady,1);var i=r.store.get(e.id),n="ROOT_QUERY"===e.id;if(!i)return n;var o=i.__typename,s=void 0===o?n&&"Query":o;if((0,a.invariant)(s,2),s===t)return!0;var c=this.possibleTypesMap[t];return!!(s&&c&&c.indexOf(s)>-1)},e.prototype.parseIntrospectionResult=function(e){var t={};return e.__schema.types.forEach(function(e){"UNION"!==e.kind&&"INTERFACE"!==e.kind||(t[e.name]=e.possibleTypes.map(function(e){return e.name}))}),t},e}();exports.IntrospectionFragmentMatcher=c;var u=Object.prototype.hasOwnProperty,l=function(){function e(e){var t=this;void 0===e&&(e=Object.create(null)),this.data=e,this.depend=(0,i.wrap)(function(e){return t.data[e]},{disposable:!0,makeCacheKey:function(e){return e}})}return e.prototype.toObject=function(){return this.data},e.prototype.get=function(e){return this.depend(e),this.data[e]},e.prototype.set=function(e,t){t!==this.data[e]&&(this.data[e]=t,this.depend.dirty(e))},e.prototype.delete=function(e){u.call(this.data,e)&&(delete this.data[e],this.depend.dirty(e))},e.prototype.clear=function(){this.replace(null)},e.prototype.replace=function(e){var t=this;e?(Object.keys(e).forEach(function(r){t.set(r,e[r])}),Object.keys(this.data).forEach(function(r){u.call(e,r)||t.delete(r)})):Object.keys(this.data).forEach(function(e){t.delete(e)})},e}();function d(e){return new l(e)}var p=function(){function t(e){var t=this,a=void 0===e?{}:e,n=a.cacheKeyRoot,o=void 0===n?new i.KeyTrie(r.canUseWeakMap):n,s=a.freezeResults,c=void 0!==s&&s,u=this.executeStoreQuery,d=this.executeSelectionSet,p=this.executeSubSelectedArray;this.freezeResults=c,this.executeStoreQuery=(0,i.wrap)(function(e){return u.call(t,e)},{makeCacheKey:function(e){var t=e.query,r=e.rootValue,i=e.contextValue,a=e.variableValues,n=e.fragmentMatcher;if(i.store instanceof l)return o.lookup(i.store,t,n,JSON.stringify(a),r.id)}}),this.executeSelectionSet=(0,i.wrap)(function(e){return d.call(t,e)},{makeCacheKey:function(e){var t=e.selectionSet,r=e.rootValue,i=e.execContext;if(i.contextValue.store instanceof l)return o.lookup(i.contextValue.store,t,i.fragmentMatcher,JSON.stringify(i.variableValues),r.id)}}),this.executeSubSelectedArray=(0,i.wrap)(function(e){return p.call(t,e)},{makeCacheKey:function(e){var t=e.field,r=e.array,i=e.execContext;if(i.contextValue.store instanceof l)return o.lookup(i.contextValue.store,t,r,JSON.stringify(i.variableValues))}})}return t.prototype.readQueryFromStore=function(t){return this.diffQueryAgainstStore((0,e.__assign)((0,e.__assign)({},t),{returnPartialData:!1})).result},t.prototype.diffQueryAgainstStore=function(e){var t=e.store,i=e.query,n=e.variables,o=e.previousResult,s=e.returnPartialData,c=void 0===s||s,u=e.rootId,l=void 0===u?"ROOT_QUERY":u,d=e.fragmentMatcherFunction,p=e.config,f=(0,r.getQueryDefinition)(i);n=(0,r.assign)({},(0,r.getDefaultValues)(f),n);var h={store:t,dataIdFromObject:p&&p.dataIdFromObject,cacheRedirects:p&&p.cacheRedirects||{}},m=this.executeStoreQuery({query:i,rootValue:{type:"id",id:l,generated:!0,typename:"Query"},contextValue:h,variableValues:n,fragmentMatcher:d}),y=m.missing&&m.missing.length>0;return y&&!c&&m.missing.forEach(function(e){if(!e.tolerable)throw new a.InvariantError(8)}),o&&(0,r.isEqual)(o,m.result)&&(m.result=o),{result:m.result,complete:!y}},t.prototype.executeStoreQuery=function(e){var t=e.query,i=e.rootValue,a=e.contextValue,n=e.variableValues,o=e.fragmentMatcher,s=void 0===o?h:o,c=(0,r.getMainDefinition)(t),u=(0,r.getFragmentDefinitions)(t),l={query:t,fragmentMap:(0,r.createFragmentMap)(u),contextValue:a,variableValues:n,fragmentMatcher:s};return this.executeSelectionSet({selectionSet:c.selectionSet,rootValue:i,execContext:l})},t.prototype.executeSelectionSet=function(t){var i=this,n=t.selectionSet,o=t.rootValue,s=t.execContext,c=s.fragmentMap,u=s.contextValue,l=s.variableValues,d={result:null},p=[],f=u.store.get(o.id),h=f&&f.__typename||"ROOT_QUERY"===o.id&&"Query"||void 0;function m(e){var t;return e.missing&&(d.missing=d.missing||[],(t=d.missing).push.apply(t,e.missing)),e.result}return n.selections.forEach(function(t){var n;if((0,r.shouldInclude)(t,l))if((0,r.isField)(t)){var d=m(i.executeField(f,h,t,s));void 0!==d&&p.push(((n={})[(0,r.resultKeyNameFromField)(t)]=d,n))}else{var y=void 0;if((0,r.isInlineFragment)(t))y=t;else if(!(y=c[t.name.value]))throw new a.InvariantError(9);var v=y.typeCondition&&y.typeCondition.name.value,g=!v||s.fragmentMatcher(o,v,u);if(g){var S=i.executeSelectionSet({selectionSet:y.selectionSet,rootValue:o,execContext:s});"heuristic"===g&&S.missing&&(S=(0,e.__assign)((0,e.__assign)({},S),{missing:S.missing.map(function(t){return(0,e.__assign)((0,e.__assign)({},t),{tolerable:!0})})})),p.push(m(S))}}}),d.result=(0,r.mergeDeepArray)(p),this.freezeResults,d},t.prototype.executeField=function(e,t,i,a){var n=a.variableValues,o=a.contextValue,s=function(e,t,i,a,n,o){o.resultKey;var s=o.directives,c=i;(a||s)&&(c=(0,r.getStoreKeyName)(c,a,s));var u=void 0;if(e&&void 0===(u=e[c])&&n.cacheRedirects&&"string"==typeof t){var l=n.cacheRedirects[t];if(l){var d=l[i];d&&(u=d(e,a,{getCacheKey:function(e){var t=n.dataIdFromObject(e);return t&&(0,r.toIdValue)({id:t,typename:e.__typename})}}))}}if(void 0===u)return{result:u,missing:[{object:e,fieldName:c,tolerable:!1}]};(0,r.isJsonValue)(u)&&(u=u.json);return{result:u}}(e,t,i.name.value,(0,r.argumentsObjectFromField)(i,n),o,{resultKey:(0,r.resultKeyNameFromField)(i),directives:(0,r.getDirectiveInfoFromField)(i,n)});return Array.isArray(s.result)?this.combineExecResults(s,this.executeSubSelectedArray({field:i,array:s.result,execContext:a})):i.selectionSet?null==s.result?s:this.combineExecResults(s,this.executeSelectionSet({selectionSet:i.selectionSet,rootValue:s.result,execContext:a})):(f(i,s.result),this.freezeResults,s)},t.prototype.combineExecResults=function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return t.forEach(function(t){t.missing&&(e=e||[]).push.apply(e,t.missing)}),{result:t.pop().result,missing:e}},t.prototype.executeSubSelectedArray=function(e){var t,r=this,i=e.field,a=e.array,n=e.execContext;function o(e){return e.missing&&(t=t||[]).push.apply(t,e.missing),e.result}return a=a.map(function(e){return null===e?null:Array.isArray(e)?o(r.executeSubSelectedArray({field:i,array:e,execContext:n})):i.selectionSet?o(r.executeSelectionSet({selectionSet:i.selectionSet,rootValue:e,execContext:n})):(f(i,e),e)}),this.freezeResults,{result:a,missing:t}},t}();function f(e,t){if(!e.selectionSet&&(0,r.isIdValue)(t))throw new a.InvariantError(10)}function h(){return!0}exports.StoreReader=p;var m=function(){function e(e){void 0===e&&(e=Object.create(null)),this.data=e}return e.prototype.toObject=function(){return this.data},e.prototype.get=function(e){return this.data[e]},e.prototype.set=function(e,t){this.data[e]=t},e.prototype.delete=function(e){this.data[e]=void 0},e.prototype.clear=function(){this.data=Object.create(null)},e.prototype.replace=function(e){this.data=e||Object.create(null)},e}();exports.ObjectCache=m;var y=function(t){function r(){var e=null!==t&&t.apply(this,arguments)||this;return e.type="WriteError",e}return(0,e.__extends)(r,t),r}(Error);function v(e,t){var r=new y("Error writing result to store for query:\n "+JSON.stringify(t));return r.message+="\n"+e.message,r.stack=e.stack,r}exports.WriteError=y;var g=function(){function t(){}return t.prototype.writeQueryToStore=function(e){var t=e.query,r=e.result,i=e.store,a=void 0===i?d():i,n=e.variables,o=e.dataIdFromObject,s=e.fragmentMatcherFunction;return this.writeResultToStore({dataId:"ROOT_QUERY",result:r,document:t,store:a,variables:n,dataIdFromObject:o,fragmentMatcherFunction:s})},t.prototype.writeResultToStore=function(e){var t=e.dataId,i=e.result,a=e.document,n=e.store,o=void 0===n?d():n,s=e.variables,c=e.dataIdFromObject,u=e.fragmentMatcherFunction,l=(0,r.getOperationDefinition)(a);try{return this.writeSelectionSetToStore({result:i,dataId:t,selectionSet:l.selectionSet,context:{store:o,processedData:{},variables:(0,r.assign)({},(0,r.getDefaultValues)(l),s),dataIdFromObject:c,fragmentMap:(0,r.createFragmentMap)((0,r.getFragmentDefinitions)(a)),fragmentMatcherFunction:u}})}catch(e){throw v(e,a)}},t.prototype.writeSelectionSetToStore=function(e){var t=this,i=e.result,n=e.dataId,o=e.selectionSet,s=e.context,c=s.variables,u=s.store,l=s.fragmentMap;return o.selections.forEach(function(e){var o;if((0,r.shouldInclude)(e,c))if((0,r.isField)(e)){var u=(0,r.resultKeyNameFromField)(e),d=i[u];if(void 0!==d)t.writeFieldToStore({dataId:n,value:d,field:e,context:s});else{var p=!1,f=!1;e.directives&&e.directives.length&&(p=e.directives.some(function(e){return e.name&&"defer"===e.name.value}),f=e.directives.some(function(e){return e.name&&"client"===e.name.value})),!p&&!f&&s.fragmentMatcherFunction}}else{var h=void 0;(0,r.isInlineFragment)(e)?h=e:(h=(l||{})[e.name.value],(0,a.invariant)(h,3));var y=!0;if(s.fragmentMatcherFunction&&h.typeCondition){var v=n||"self",g=(0,r.toIdValue)({id:v,typename:void 0}),S={store:new m((o={},o[v]=i,o)),cacheRedirects:{}},b=s.fragmentMatcherFunction(g,h.typeCondition.name.value,S);(0,r.isProduction)(),y=!!b}y&&t.writeSelectionSetToStore({result:i,selectionSet:h.selectionSet,dataId:n,context:s})}}),u},t.prototype.writeFieldToStore=function(t){var i,n,o,s=t.field,c=t.value,u=t.dataId,l=t.context,d=l.variables,p=l.dataIdFromObject,f=l.store,h=(0,r.storeKeyNameFromField)(s,d);if(s.selectionSet&&null!==c)if(Array.isArray(c)){var m=u+"."+h;n=this.processArrayValue(c,m,s.selectionSet,l)}else{var y=u+"."+h,v=!0;if(S(y)||(y="$"+y),p){var g=p(c);(0,a.invariant)(!g||!S(g),4),(g||"number"==typeof g&&0===g)&&(y=g,v=!1)}b(y,s,l.processedData)||this.writeSelectionSetToStore({dataId:y,result:c,selectionSet:s.selectionSet,context:l});var x=c.__typename;n=(0,r.toIdValue)({id:y,typename:x},v);var R=(o=f.get(u))&&o[h];if(R!==n&&(0,r.isIdValue)(R)){var _=void 0!==R.typename,I=void 0!==x,F=_&&I&&R.typename!==x;(0,a.invariant)(!v||R.generated||F,5),(0,a.invariant)(!_||I,6),R.generated&&(F?v||f.delete(R.id):function t(i,a,n){if(i===a)return!1;var o=n.get(i);var s=n.get(a);var c=!1;Object.keys(o).forEach(function(e){var i=o[e],a=s[e];(0,r.isIdValue)(i)&&S(i.id)&&(0,r.isIdValue)(a)&&!(0,r.isEqual)(i,a)&&t(i.id,a.id,n)&&(c=!0)});n.delete(i);var u=(0,e.__assign)((0,e.__assign)({},o),s);if((0,r.isEqual)(u,s))return c;n.set(a,u);return!0}(R.id,n.id,f))}}else n=null!=c&&"object"==typeof c?{type:"json",json:c}:c;(o=f.get(u))&&(0,r.isEqual)(n,o[h])||f.set(u,(0,e.__assign)((0,e.__assign)({},o),((i={})[h]=n,i)))},t.prototype.processArrayValue=function(e,t,i,a){var n=this;return e.map(function(e,o){if(null===e)return null;var s=t+"."+o;if(Array.isArray(e))return n.processArrayValue(e,s,i,a);var c=!0;if(a.dataIdFromObject){var u=a.dataIdFromObject(e);u&&(s=u,c=!1)}return b(s,i,a.processedData)||n.writeSelectionSetToStore({dataId:s,result:e,selectionSet:i,context:a}),(0,r.toIdValue)({id:s,typename:e.__typename},c)})},t}();function S(e){return"$"===e[0]}function b(e,t,r){if(!r)return!1;if(r[e]){if(r[e].indexOf(t)>=0)return!0;r[e].push(t)}else r[e]=[t];return!1}exports.StoreWriter=g;var x={fragmentMatcher:new s,dataIdFromObject:R,addTypename:!0,resultCaching:!0,freezeResults:!1};function R(e){if(e.__typename){if(void 0!==e.id)return e.__typename+":"+e.id;if(void 0!==e._id)return e.__typename+":"+e._id}return null}var _=Object.prototype.hasOwnProperty,I=function(t){function r(e,r,i){var a=t.call(this,Object.create(null))||this;return a.optimisticId=e,a.parent=r,a.transaction=i,a}return(0,e.__extends)(r,t),r.prototype.toObject=function(){return(0,e.__assign)((0,e.__assign)({},this.parent.toObject()),this.data)},r.prototype.get=function(e){return _.call(this.data,e)?this.data[e]:this.parent.get(e)},r}(m),F=function(t){function n(a){void 0===a&&(a={});var n=t.call(this)||this;n.watches=new Set,n.typenameDocumentCache=new Map,n.cacheKeyRoot=new i.KeyTrie(r.canUseWeakMap),n.silenceBroadcast=!1,n.config=(0,e.__assign)((0,e.__assign)({},x),a),n.config.customResolvers&&(n.config.cacheRedirects=n.config.customResolvers),n.config.cacheResolvers&&(n.config.cacheRedirects=n.config.cacheResolvers),n.addTypename=!!n.config.addTypename,n.data=n.config.resultCaching?new l:new m,n.optimisticData=n.data,n.storeWriter=new g,n.storeReader=new p({cacheKeyRoot:n.cacheKeyRoot,freezeResults:a.freezeResults});var o=n,s=o.maybeBroadcastWatch;return n.maybeBroadcastWatch=(0,i.wrap)(function(e){return s.call(n,e)},{makeCacheKey:function(e){if(!e.optimistic&&!e.previousResult)return o.data instanceof l?o.cacheKeyRoot.lookup(e.query,JSON.stringify(e.variables)):void 0}}),n}return(0,e.__extends)(n,t),n.prototype.restore=function(e){return e&&this.data.replace(e),this},n.prototype.extract=function(e){return void 0===e&&(e=!1),(e?this.optimisticData:this.data).toObject()},n.prototype.read=function(e){if("string"==typeof e.rootId&&void 0===this.data.get(e.rootId))return null;var t=this.config.fragmentMatcher,r=t&&t.match;return this.storeReader.readQueryFromStore({store:e.optimistic?this.optimisticData:this.data,query:this.transformDocument(e.query),variables:e.variables,rootId:e.rootId,fragmentMatcherFunction:r,previousResult:e.previousResult,config:this.config})||null},n.prototype.write=function(e){var t=this.config.fragmentMatcher,r=t&&t.match;this.storeWriter.writeResultToStore({dataId:e.dataId,result:e.result,variables:e.variables,document:this.transformDocument(e.query),store:this.data,dataIdFromObject:this.config.dataIdFromObject,fragmentMatcherFunction:r}),this.broadcastWatches()},n.prototype.diff=function(e){var t=this.config.fragmentMatcher,r=t&&t.match;return this.storeReader.diffQueryAgainstStore({store:e.optimistic?this.optimisticData:this.data,query:this.transformDocument(e.query),variables:e.variables,returnPartialData:e.returnPartialData,previousResult:e.previousResult,fragmentMatcherFunction:r,config:this.config})},n.prototype.watch=function(e){var t=this;return this.watches.add(e),function(){t.watches.delete(e)}},n.prototype.evict=function(e){throw new a.InvariantError(7)},n.prototype.reset=function(){return this.data.clear(),this.broadcastWatches(),Promise.resolve()},n.prototype.removeOptimistic=function(e){for(var t=[],r=0,i=this.optimisticData;i instanceof I;)i.optimisticId===e?++r:t.push(i),i=i.parent;if(r>0){for(this.optimisticData=i;t.length>0;){var a=t.pop();this.performTransaction(a.transaction,a.optimisticId)}this.broadcastWatches()}},n.prototype.performTransaction=function(e,t){var r=this.data,i=this.silenceBroadcast;this.silenceBroadcast=!0,"string"==typeof t&&(this.data=this.optimisticData=new I(t,this.optimisticData,e));try{e(this)}finally{this.silenceBroadcast=i,this.data=r}this.broadcastWatches()},n.prototype.recordOptimisticTransaction=function(e,t){return this.performTransaction(e,t)},n.prototype.transformDocument=function(e){if(this.addTypename){var t=this.typenameDocumentCache.get(e);return t||(t=(0,r.addTypenameToDocument)(e),this.typenameDocumentCache.set(e,t),this.typenameDocumentCache.set(t,t)),t}return e},n.prototype.broadcastWatches=function(){var e=this;this.silenceBroadcast||this.watches.forEach(function(t){return e.maybeBroadcastWatch(t)})},n.prototype.maybeBroadcastWatch=function(e){e.callback(this.diff({query:e.query,variables:e.variables,previousResult:e.previousResult&&e.previousResult(),optimistic:e.optimistic}))},n}(t.ApolloCache);exports.InMemoryCache=F;