UNPKG

38.7 kBJavaScriptView Raw
1exports.__esModule=!0,exports.isApolloError=h,exports.ObservableQuery=exports.NetworkStatus=exports.FetchType=exports.ApolloError=exports.ApolloClient=exports.default=void 0;var e,t,r=require("tslib"),o=require("apollo-utilities"),n=require("apollo-link"),i=(e=require("symbol-observable"))&&e.__esModule?e:{default:e},s=require("ts-invariant"),a=require("graphql/language/visitor");function u(e){return e<7}exports.NetworkStatus=t,function(e){e[e.loading=1]="loading",e[e.setVariables=2]="setVariables",e[e.fetchMore=3]="fetchMore",e[e.refetch=4]="refetch",e[e.poll=6]="poll",e[e.ready=7]="ready",e[e.error=8]="error"}(t||(exports.NetworkStatus=t={}));var c=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return(0,r.__extends)(t,e),t.prototype[i.default]=function(){return this},t.prototype["@@observable"]=function(){return this},t}(n.Observable);function l(e){return Array.isArray(e)&&e.length>0}function h(e){return e.hasOwnProperty("graphQLErrors")}var p,y=function(e){var t="";return l(e.graphQLErrors)&&e.graphQLErrors.forEach(function(e){var r=e?e.message:"Error message not found.";t+="GraphQL error: "+r+"\n"}),e.networkError&&(t+="Network error: "+e.networkError.message+"\n"),t=t.replace(/\n$/,"")},f=function(e){function t(r){var o=r.graphQLErrors,n=r.networkError,i=r.errorMessage,s=r.extraInfo,a=e.call(this,i)||this;return a.graphQLErrors=o||[],a.networkError=n||null,a.message=i||y(a),a.extraInfo=s,a.__proto__=t.prototype,a}return(0,r.__extends)(t,e),t}(Error);exports.ApolloError=f,exports.FetchType=p,function(e){e[e.normal=1]="normal",e[e.refetch=2]="refetch",e[e.poll=3]="poll"}(p||(exports.FetchType=p={}));var v=function(e){function n(t){var r=t.queryManager,n=t.options,i=t.shouldSubscribe,s=void 0===i||i,a=e.call(this,function(e){return a.onSubscribe(e)})||this;a.observers=new Set,a.subscriptions=new Set,a.isTornDown=!1,a.options=n,a.variables=n.variables||{},a.queryId=r.generateQueryId(),a.shouldSubscribe=s;var u=(0,o.getOperationDefinition)(n.query);return a.queryName=u&&u.name&&u.name.value,a.queryManager=r,a}return(0,r.__extends)(n,e),n.prototype.result=function(){var e=this;return new Promise(function(t,r){var o={next:function(r){t(r),e.observers.delete(o),e.observers.size||e.queryManager.removeQuery(e.queryId),setTimeout(function(){n.unsubscribe()},0)},error:r},n=e.subscribe(o)})},n.prototype.currentResult=function(){var e=this.getCurrentResult();return void 0===e.data&&(e.data={}),e},n.prototype.getCurrentResult=function(){if(this.isTornDown){var e=this.lastResult;return{data:!this.lastError&&e&&e.data||void 0,error:this.lastError,loading:!1,networkStatus:t.error}}var o,n,i,s=this.queryManager.getCurrentQueryResult(this),a=s.data,c=s.partial,h=this.queryManager.queryStore.get(this.queryId),p=this.options.fetchPolicy,y="network-only"===p||"no-cache"===p;if(h){var v=h.networkStatus;if(n=h,void 0===(i=this.options.errorPolicy)&&(i="none"),n&&(n.networkError||"none"===i&&l(n.graphQLErrors)))return{data:void 0,loading:!1,networkStatus:v,error:new f({graphQLErrors:h.graphQLErrors,networkError:h.networkError})};h.variables&&(this.options.variables=(0,r.__assign)({},this.options.variables,h.variables),this.variables=this.options.variables),o={data:a,loading:u(v),networkStatus:v},h.graphQLErrors&&"all"===this.options.errorPolicy&&(o.errors=h.graphQLErrors)}else{var d=y||c&&"cache-only"!==p;o={data:a,loading:d,networkStatus:d?t.loading:t.ready}}return c||this.updateLastResult((0,r.__assign)({},o,{stale:!1})),(0,r.__assign)({},o,{partial:c})},n.prototype.isDifferentFromLastResult=function(e){var t=this.lastResultSnapshot;return!(t&&e&&t.networkStatus===e.networkStatus&&t.stale===e.stale&&(0,o.isEqual)(t.data,e.data))},n.prototype.getLastResult=function(){return this.lastResult},n.prototype.getLastError=function(){return this.lastError},n.prototype.resetLastResults=function(){delete this.lastResult,delete this.lastResultSnapshot,delete this.lastError,this.isTornDown=!1},n.prototype.resetQueryStoreErrors=function(){var e=this.queryManager.queryStore.get(this.queryId);e&&(e.networkError=null,e.graphQLErrors=[])},n.prototype.refetch=function(e){var t=this.options.fetchPolicy;return"cache-only"===t?Promise.reject(new s.InvariantError(3)):("no-cache"!==t&&"cache-and-network"!==t&&(t="network-only"),(0,o.isEqual)(this.variables,e)||(this.variables=(0,r.__assign)({},this.variables,e)),(0,o.isEqual)(this.options.variables,this.variables)||(this.options.variables=(0,r.__assign)({},this.options.variables,this.variables)),this.queryManager.fetchQuery(this.queryId,(0,r.__assign)({},this.options,{fetchPolicy:t}),p.refetch))},n.prototype.fetchMore=function(e){var t=this;(0,s.invariant)(e.updateQuery,4);var o=(0,r.__assign)({},e.query?e:(0,r.__assign)({},this.options,e,{variables:(0,r.__assign)({},this.variables,e.variables)}),{fetchPolicy:"network-only"}),n=this.queryManager.generateQueryId();return this.queryManager.fetchQuery(n,o,p.normal,this.queryId).then(function(r){return t.updateQuery(function(t){return e.updateQuery(t,{fetchMoreResult:r.data,variables:o.variables})}),t.queryManager.stopQuery(n),r},function(e){throw t.queryManager.stopQuery(n),e})},n.prototype.subscribeToMore=function(e){var t=this,r=this.queryManager.startGraphQLSubscription({query:e.document,variables:e.variables}).subscribe({next:function(r){var o=e.updateQuery;o&&t.updateQuery(function(e,t){var n=t.variables;return o(e,{subscriptionData:r,variables:n})})},error:function(t){e.onError&&e.onError(t)}});return this.subscriptions.add(r),function(){t.subscriptions.delete(r)&&r.unsubscribe()}},n.prototype.setOptions=function(e){var t=this.options.fetchPolicy;this.options=(0,r.__assign)({},this.options,e),e.pollInterval?this.startPolling(e.pollInterval):0===e.pollInterval&&this.stopPolling();var o=e.fetchPolicy;return this.setVariables(this.options.variables,t!==o&&("cache-only"===t||"standby"===t||"network-only"===o),e.fetchResults)},n.prototype.setVariables=function(e,t,r){return void 0===t&&(t=!1),void 0===r&&(r=!0),this.isTornDown=!1,e=e||this.variables,!t&&(0,o.isEqual)(e,this.variables)?this.observers.size&&r?this.result():Promise.resolve():(this.variables=this.options.variables=e,this.observers.size?this.queryManager.fetchQuery(this.queryId,this.options):Promise.resolve())},n.prototype.updateQuery=function(e){var t=this.queryManager,r=t.getQueryWithPreviousResult(this.queryId),n=r.previousResult,i=r.variables,s=r.document,a=(0,o.tryFunctionOrLogError)(function(){return e(n,{variables:i})});a&&(t.dataStore.markUpdateQueryResult(s,i,a),t.broadcastQueries())},n.prototype.stopPolling=function(){this.queryManager.stopPollingQuery(this.queryId),this.options.pollInterval=void 0},n.prototype.startPolling=function(e){m(this),this.options.pollInterval=e,this.queryManager.startPollingQuery(this.options,this.queryId)},n.prototype.updateLastResult=function(e){var t=this.lastResult;return this.lastResult=e,this.lastResultSnapshot=this.queryManager.assumeImmutableResults?e:(0,o.cloneDeep)(e),t},n.prototype.onSubscribe=function(e){var t=this;try{var r=e._subscription._observer;r&&!r.error&&(r.error=d)}catch(e){}var o=!this.observers.size;return this.observers.add(e),e.next&&this.lastResult&&e.next(this.lastResult),e.error&&this.lastError&&e.error(this.lastError),o&&this.setUpQuery(),function(){t.observers.delete(e)&&!t.observers.size&&t.tearDownQuery()}},n.prototype.setUpQuery=function(){var e=this,t=this.queryManager,r=this.queryId;this.shouldSubscribe&&t.addObservableQuery(r,this),this.options.pollInterval&&(m(this),t.startPollingQuery(this.options,r));var n=function(t){b(e.observers,"error",e.lastError=t)};t.observeQuery(r,this.options,{next:function(r){if(e.lastError||e.isDifferentFromLastResult(r)){var n=e.updateLastResult(r),i=e.options,s=i.query,a=i.variables,u=i.fetchPolicy;t.transform(s).hasClientExports?t.getLocalState().addExportedVariables(s,a).then(function(i){var a=e.variables;e.variables=e.options.variables=i,!r.loading&&n&&"cache-only"!==u&&t.transform(s).serverQuery&&!(0,o.isEqual)(a,i)?e.refetch():b(e.observers,"next",r)}):b(e.observers,"next",r)}},error:n}).catch(n)},n.prototype.tearDownQuery=function(){var e=this.queryManager;this.isTornDown=!0,e.stopPollingQuery(this.queryId),this.subscriptions.forEach(function(e){return e.unsubscribe()}),this.subscriptions.clear(),e.removeObservableQuery(this.queryId),e.stopQuery(this.queryId),this.observers.clear()},n}(c);function d(e){}function b(e,t,r){var o=[];e.forEach(function(e){return e[t]&&o.push(e)}),o.forEach(function(e){return e[t](r)})}function m(e){var t=e.options.fetchPolicy;(0,s.invariant)("cache-first"!==t&&"cache-only"!==t,5)}exports.ObservableQuery=v;var g=function(){function e(){this.store={}}return e.prototype.getStore=function(){return this.store},e.prototype.get=function(e){return this.store[e]},e.prototype.initMutation=function(e,t,r){this.store[e]={mutation:t,variables:r||{},loading:!0,error:null}},e.prototype.markMutationError=function(e,t){var r=this.store[e];r&&(r.loading=!1,r.error=t)},e.prototype.markMutationResult=function(e){var t=this.store[e];t&&(t.loading=!1,t.error=null)},e.prototype.reset=function(){this.store={}},e}(),Q=function(){function e(){this.store={}}return e.prototype.getStore=function(){return this.store},e.prototype.get=function(e){return this.store[e]},e.prototype.initQuery=function(e){var r=this.store[e.queryId];(0,s.invariant)(!r||r.document===e.document||(0,o.isEqual)(r.document,e.document),19);var n,i=!1,a=null;e.storePreviousVariables&&r&&r.networkStatus!==t.loading&&((0,o.isEqual)(r.variables,e.variables)||(i=!0,a=r.variables)),n=i?t.setVariables:e.isPoll?t.poll:e.isRefetch?t.refetch:t.loading;var u=[];r&&r.graphQLErrors&&(u=r.graphQLErrors),this.store[e.queryId]={document:e.document,variables:e.variables,previousVariables:a,networkError:null,graphQLErrors:u,networkStatus:n,metadata:e.metadata},"string"==typeof e.fetchMoreForQueryId&&this.store[e.fetchMoreForQueryId]&&(this.store[e.fetchMoreForQueryId].networkStatus=t.fetchMore)},e.prototype.markQueryResult=function(e,r,o){this.store&&this.store[e]&&(this.store[e].networkError=null,this.store[e].graphQLErrors=l(r.errors)?r.errors:[],this.store[e].previousVariables=null,this.store[e].networkStatus=t.ready,"string"==typeof o&&this.store[o]&&(this.store[o].networkStatus=t.ready))},e.prototype.markQueryError=function(e,r,o){this.store&&this.store[e]&&(this.store[e].networkError=r,this.store[e].networkStatus=t.error,"string"==typeof o&&this.markQueryResultClient(o,!0))},e.prototype.markQueryResultClient=function(e,r){var o=this.store&&this.store[e];o&&(o.networkError=null,o.previousVariables=null,r&&(o.networkStatus=t.ready))},e.prototype.stopQuery=function(e){delete this.store[e]},e.prototype.reset=function(e){var r=this;Object.keys(this.store).forEach(function(o){e.indexOf(o)<0?r.stopQuery(o):r.store[o].networkStatus=t.loading})},e}();var w=function(){function e(e){var t=e.cache,r=e.client,o=e.resolvers,n=e.fragmentMatcher;this.cache=t,r&&(this.client=r),o&&this.addResolvers(o),n&&this.setFragmentMatcher(n)}return e.prototype.addResolvers=function(e){var t=this;this.resolvers=this.resolvers||{},Array.isArray(e)?e.forEach(function(e){t.resolvers=(0,o.mergeDeep)(t.resolvers,e)}):this.resolvers=(0,o.mergeDeep)(this.resolvers,e)},e.prototype.setResolvers=function(e){this.resolvers={},this.addResolvers(e)},e.prototype.getResolvers=function(){return this.resolvers||{}},e.prototype.runResolvers=function(e){var t=e.document,o=e.remoteResult,n=e.context,i=e.variables,s=e.onlyRunForcedResolvers,a=void 0!==s&&s;return(0,r.__awaiter)(this,void 0,void 0,function(){return(0,r.__generator)(this,function(e){return t?[2,this.resolveDocument(t,o.data,n,i,this.fragmentMatcher,a).then(function(e){return(0,r.__assign)({},o,{data:e.result})})]:[2,o]})})},e.prototype.setFragmentMatcher=function(e){this.fragmentMatcher=e},e.prototype.getFragmentMatcher=function(){return this.fragmentMatcher},e.prototype.clientQuery=function(e){return(0,o.hasDirectives)(["client"],e)&&this.resolvers?e:null},e.prototype.serverQuery=function(e){return this.resolvers?(0,o.removeClientSetsFromDocument)(e):e},e.prototype.prepareContext=function(e){void 0===e&&(e={});var t=this.cache;return(0,r.__assign)({},e,{cache:t,getCacheKey:function(e){if(t.config)return t.config.dataIdFromObject(e);(0,s.invariant)(!1,17)}})},e.prototype.addExportedVariables=function(e,t,o){return void 0===t&&(t={}),void 0===o&&(o={}),(0,r.__awaiter)(this,void 0,void 0,function(){return(0,r.__generator)(this,function(n){return e?[2,this.resolveDocument(e,this.buildRootValueFromCache(e,t)||{},this.prepareContext(o),t).then(function(e){return(0,r.__assign)({},t,e.exportedVariables)})]:[2,(0,r.__assign)({},t)]})})},e.prototype.shouldForceResolvers=function(e){var t=!1;return(0,a.visit)(e,{Directive:{enter:function(e){if("client"===e.name.value&&e.arguments&&(t=e.arguments.some(function(e){return"always"===e.name.value&&"BooleanValue"===e.value.kind&&!0===e.value.value})))return a.BREAK}}}),t},e.prototype.buildRootValueFromCache=function(e,t){return this.cache.diff({query:(0,o.buildQueryFromSelectionSet)(e),variables:t,returnPartialData:!0,optimistic:!1}).result},e.prototype.resolveDocument=function(e,t,n,i,s,a){return void 0===n&&(n={}),void 0===i&&(i={}),void 0===s&&(s=function(){return!0}),void 0===a&&(a=!1),(0,r.__awaiter)(this,void 0,void 0,function(){var u,c,l,h,p,y,f,v,d;return(0,r.__generator)(this,function(b){var m;return u=(0,o.getMainDefinition)(e),c=(0,o.getFragmentDefinitions)(e),l=(0,o.createFragmentMap)(c),h=u.operation,p=h?(m=h).charAt(0).toUpperCase()+m.slice(1):"Query",f=(y=this).cache,v=y.client,d={fragmentMap:l,context:(0,r.__assign)({},n,{cache:f,client:v}),variables:i,fragmentMatcher:s,defaultOperationType:p,exportedVariables:{},onlyRunForcedResolvers:a},[2,this.resolveSelectionSet(u.selectionSet,t,d).then(function(e){return{result:e,exportedVariables:d.exportedVariables}})]})})},e.prototype.resolveSelectionSet=function(e,t,n){return(0,r.__awaiter)(this,void 0,void 0,function(){var i,a,u,c,l,h=this;return(0,r.__generator)(this,function(p){return i=n.fragmentMap,a=n.context,u=n.variables,c=[t],l=function(e){return(0,r.__awaiter)(h,void 0,void 0,function(){var l,h;return(0,r.__generator)(this,function(r){return(0,o.shouldInclude)(e,u)?(0,o.isField)(e)?[2,this.resolveField(e,t,n).then(function(t){var r;void 0!==t&&c.push(((r={})[(0,o.resultKeyNameFromField)(e)]=t,r))})]:((0,o.isInlineFragment)(e)?l=e:(l=i[e.name.value],(0,s.invariant)(l,18)),l&&l.typeCondition&&(h=l.typeCondition.name.value,n.fragmentMatcher(t,h,a))?[2,this.resolveSelectionSet(l.selectionSet,t,n).then(function(e){c.push(e)})]:[2]):[2]})})},[2,Promise.all(e.selections.map(l)).then(function(){return(0,o.mergeDeepArray)(c)})]})})},e.prototype.resolveField=function(e,t,n){return(0,r.__awaiter)(this,void 0,void 0,function(){var i,s,a,u,c,l,h,p,y,f=this;return(0,r.__generator)(this,function(r){return i=n.variables,s=e.name.value,a=(0,o.resultKeyNameFromField)(e),u=s!==a,c=t[a]||t[s],l=Promise.resolve(c),n.onlyRunForcedResolvers&&!this.shouldForceResolvers(e)||(h=t.__typename||n.defaultOperationType,(p=this.resolvers&&this.resolvers[h])&&(y=p[u?s:a])&&(l=Promise.resolve(y(t,(0,o.argumentsObjectFromField)(e,i),n.context,{field:e})))),[2,l.then(function(t){return void 0===t&&(t=c),e.directives&&e.directives.forEach(function(e){"export"===e.name.value&&e.arguments&&e.arguments.forEach(function(e){"as"===e.name.value&&"StringValue"===e.value.kind&&(n.exportedVariables[e.value.value]=t)})}),e.selectionSet?null==t?t:Array.isArray(t)?f.resolveSubSelectedArray(e,t,n):e.selectionSet?f.resolveSelectionSet(e.selectionSet,t,n):void 0:t})]})})},e.prototype.resolveSubSelectedArray=function(e,t,r){var o=this;return Promise.all(t.map(function(t){return null===t?null:Array.isArray(t)?o.resolveSubSelectedArray(e,t,r):e.selectionSet?o.resolveSelectionSet(e.selectionSet,t,r):void 0}))},e}();function S(e){var t=new Set,r=null;return new c(function(o){return t.add(o),r=r||e.subscribe({next:function(e){t.forEach(function(t){return t.next&&t.next(e)})},error:function(e){t.forEach(function(t){return t.error&&t.error(e)})},complete:function(){t.forEach(function(e){return e.complete&&e.complete()})}}),function(){t.delete(o)&&!t.size&&r&&(r.unsubscribe(),r=null)}})}var q=Object.prototype.hasOwnProperty,R=function(){function e(e){var t=e.link,r=e.queryDeduplication,n=void 0!==r&&r,i=e.store,s=e.onBroadcast,a=void 0===s?function(){}:s,u=e.ssrMode,c=void 0!==u&&u,l=e.clientAwareness,h=void 0===l?{}:l,p=e.localState,y=e.assumeImmutableResults;this.mutationStore=new g,this.queryStore=new Q,this.clientAwareness={},this.idCounter=1,this.queries=new Map,this.fetchQueryRejectFns=new Map,this.transformCache=new(o.canUseWeakMap?WeakMap:Map),this.inFlightLinkObservables=new Map,this.pollingInfoByQueryId=new Map,this.link=t,this.queryDeduplication=n,this.dataStore=i,this.onBroadcast=a,this.clientAwareness=h,this.localState=p||new w({cache:i.getCache()}),this.ssrMode=c,this.assumeImmutableResults=!!y}return e.prototype.stop=function(){var e=this;this.queries.forEach(function(t,r){e.stopQueryNoBroadcast(r)}),this.fetchQueryRejectFns.forEach(function(e){e(new s.InvariantError(6))})},e.prototype.mutate=function(e){var t=e.mutation,n=e.variables,i=e.optimisticResponse,a=e.updateQueries,u=e.refetchQueries,c=void 0===u?[]:u,h=e.awaitRefetchQueries,p=void 0!==h&&h,y=e.update,v=e.errorPolicy,d=void 0===v?"none":v,b=e.fetchPolicy,m=e.context,g=void 0===m?{}:m;return(0,r.__awaiter)(this,void 0,void 0,function(){var e,u,h,v=this;return(0,r.__generator)(this,function(m){switch(m.label){case 0:return(0,s.invariant)(t,7),(0,s.invariant)(!b||"no-cache"===b,8),e=this.generateQueryId(),t=this.transform(t).document,this.setQuery(e,function(){return{document:t}}),n=this.getVariables(t,n),this.transform(t).hasClientExports?[4,this.localState.addExportedVariables(t,n,g)]:[3,2];case 1:n=m.sent(),m.label=2;case 2:return u=function(){var e={};return a&&v.queries.forEach(function(t,r){var o=t.observableQuery;if(o){var n=o.queryName;n&&q.call(a,n)&&(e[r]={updater:a[n],query:v.queryStore.get(r)})}}),e},this.mutationStore.initMutation(e,t,n),this.dataStore.markMutationInit({mutationId:e,document:t,variables:n,updateQueries:u(),update:y,optimisticResponse:i}),this.broadcastQueries(),h=this,[2,new Promise(function(s,a){var v,m;h.getObservableFromLink(t,(0,r.__assign)({},g,{optimisticResponse:i}),n,!1).subscribe({next:function(r){(0,o.graphQLResultHasError)(r)&&"none"===d?m=new f({graphQLErrors:r.errors}):(h.mutationStore.markMutationResult(e),"no-cache"!==b&&h.dataStore.markMutationResult({mutationId:e,result:r,document:t,variables:n,updateQueries:u(),update:y}),v=r)},error:function(t){h.mutationStore.markMutationError(e,t),h.dataStore.markMutationComplete({mutationId:e,optimisticResponse:i}),h.broadcastQueries(),h.setQuery(e,function(){return{document:null}}),a(new f({networkError:t}))},complete:function(){if(m&&h.mutationStore.markMutationError(e,m),h.dataStore.markMutationComplete({mutationId:e,optimisticResponse:i}),h.broadcastQueries(),m)a(m);else{"function"==typeof c&&(c=c(v));var t=[];l(c)&&c.forEach(function(e){if("string"==typeof e)h.queries.forEach(function(r){var o=r.observableQuery;o&&o.queryName===e&&t.push(o.refetch())});else{var r={query:e.query,variables:e.variables,fetchPolicy:"network-only"};e.context&&(r.context=e.context),t.push(h.query(r))}}),Promise.all(p?t:[]).then(function(){h.setQuery(e,function(){return{document:null}}),"ignore"===d&&v&&(0,o.graphQLResultHasError)(v)&&delete v.errors,s(v)})}}})})]}})})},e.prototype.fetchQuery=function(e,t,n,i){return(0,r.__awaiter)(this,void 0,void 0,function(){var s,a,u,c,l,y,v,d,b,m,g,Q,w,S,q,R,k,E,_=this;return(0,r.__generator)(this,function(I){switch(I.label){case 0:return s=t.metadata,a=void 0===s?null:s,u=t.fetchPolicy,c=void 0===u?"cache-first":u,l=t.context,y=void 0===l?{}:l,v=this.transform(t.query).document,d=this.getVariables(v,t.variables),this.transform(v).hasClientExports?[4,this.localState.addExportedVariables(v,d,y)]:[3,2];case 1:d=I.sent(),I.label=2;case 2:if(t=(0,r.__assign)({},t,{variables:d}),g=m="network-only"===c||"no-cache"===c,m||(Q=this.dataStore.getCache().diff({query:v,variables:d,returnPartialData:!0,optimistic:!1}),w=Q.complete,S=Q.result,g=!w||"cache-and-network"===c,b=S),q=g&&"cache-only"!==c&&"standby"!==c,(0,o.hasDirectives)(["live"],v)&&(q=!0),R=this.idCounter++,k="no-cache"!==c?this.updateQueryWatch(e,v,t):void 0,this.setQuery(e,function(){return{document:v,lastRequestId:R,invalidated:!0,cancel:k}}),this.invalidate(i),this.queryStore.initQuery({queryId:e,document:v,storePreviousVariables:q,variables:d,isPoll:n===p.poll,isRefetch:n===p.refetch,metadata:a,fetchMoreForQueryId:i}),this.broadcastQueries(),q){if(E=this.fetchRequest({requestId:R,queryId:e,document:v,options:t,fetchMoreForQueryId:i}).catch(function(t){throw h(t)?t:(R>=_.getQuery(e).lastRequestId&&(_.queryStore.markQueryError(e,t,i),_.invalidate(e),_.invalidate(i),_.broadcastQueries()),new f({networkError:t}))}),"cache-and-network"!==c)return[2,E];E.catch(function(){})}return this.queryStore.markQueryResultClient(e,!q),this.invalidate(e),this.invalidate(i),this.transform(v).hasForcedResolvers?[2,this.localState.runResolvers({document:v,remoteResult:{data:b},context:y,variables:d,onlyRunForcedResolvers:!0}).then(function(r){return _.markQueryResult(e,r,t,i),_.broadcastQueries(),r})]:(this.broadcastQueries(),[2,{data:b}])}})})},e.prototype.markQueryResult=function(e,t,r,o){var n=r.fetchPolicy,i=r.variables,s=r.errorPolicy;"no-cache"===n?this.setQuery(e,function(){return{newData:{result:t.data,complete:!0}}}):this.dataStore.markQueryResult(t,this.getQuery(e).document,i,o,"ignore"===s||"all"===s)},e.prototype.queryListenerForObserver=function(e,t,r){var o=this;function n(e,t){if(r[e])try{r[e](t)}catch(e){}}return function(r,i){if(o.invalidate(e,!1),r){var s=o.getQuery(e),a=s.observableQuery,c=s.document,h=a?a.options.fetchPolicy:t.fetchPolicy;if("standby"!==h){var p=u(r.networkStatus),y=a&&a.getLastResult(),v=!(!y||y.networkStatus===r.networkStatus),d=t.returnPartialData||!i&&r.previousVariables||v&&t.notifyOnNetworkStatusChange||"cache-only"===h||"cache-and-network"===h;if(!p||d){var b=l(r.graphQLErrors),m=a&&a.options.errorPolicy||t.errorPolicy||"none";if("none"===m&&b||r.networkError)return n("error",new f({graphQLErrors:r.graphQLErrors,networkError:r.networkError}));try{var g=void 0,Q=void 0;if(i)"no-cache"!==h&&"network-only"!==h&&o.setQuery(e,function(){return{newData:null}}),g=i.result,Q=!i.complete;else{var w=a&&a.getLastError(),S="none"!==m&&(w&&w.graphQLErrors)!==r.graphQLErrors;if(y&&y.data&&!S)g=y.data,Q=!1;else{var q=o.dataStore.getCache().diff({query:c,variables:r.previousVariables||r.variables,returnPartialData:!0,optimistic:!0});g=q.result,Q=!q.complete}}var R=Q&&!(t.returnPartialData||"cache-only"===h),k={data:R?y&&y.data:g,loading:p,networkStatus:r.networkStatus,stale:R};"all"===m&&b&&(k.errors=r.graphQLErrors),n("next",k)}catch(e){n("error",new f({networkError:e}))}}}}}},e.prototype.transform=function(e){var t=this.transformCache;if(!t.has(e)){var r=this.dataStore.getCache(),n=r.transformDocument(e),i=(0,o.removeConnectionDirectiveFromDocument)(r.transformForLink(n)),s=this.localState.clientQuery(n),a=this.localState.serverQuery(i),u={document:n,hasClientExports:(0,o.hasClientExports)(n),hasForcedResolvers:this.localState.shouldForceResolvers(n),clientQuery:s,serverQuery:a,defaultVars:(0,o.getDefaultValues)((0,o.getOperationDefinition)(n))},c=function(e){e&&!t.has(e)&&t.set(e,u)};c(e),c(n),c(s),c(a)}return t.get(e)},e.prototype.getVariables=function(e,t){return(0,r.__assign)({},this.transform(e).defaultVars,t)},e.prototype.watchQuery=function(e,t){void 0===t&&(t=!0),(0,s.invariant)("standby"!==e.fetchPolicy,9),e.variables=this.getVariables(e.query,e.variables),void 0===e.notifyOnNetworkStatusChange&&(e.notifyOnNetworkStatusChange=!1);var o=(0,r.__assign)({},e);return new v({queryManager:this,options:o,shouldSubscribe:t})},e.prototype.query=function(e){var t=this;return(0,s.invariant)(e.query,10),(0,s.invariant)("Document"===e.query.kind,11),(0,s.invariant)(!e.returnPartialData,12),(0,s.invariant)(!e.pollInterval,13),new Promise(function(r,o){var n=t.watchQuery(e,!1);t.fetchQueryRejectFns.set("query:"+n.queryId,o),n.result().then(r,o).then(function(){return t.fetchQueryRejectFns.delete("query:"+n.queryId)})})},e.prototype.generateQueryId=function(){return String(this.idCounter++)},e.prototype.stopQueryInStore=function(e){this.stopQueryInStoreNoBroadcast(e),this.broadcastQueries()},e.prototype.stopQueryInStoreNoBroadcast=function(e){this.stopPollingQuery(e),this.queryStore.stopQuery(e),this.invalidate(e)},e.prototype.addQueryListener=function(e,t){this.setQuery(e,function(e){return e.listeners.add(t),{invalidated:!1}})},e.prototype.updateQueryWatch=function(e,t,r){var o=this,n=this.getQuery(e).cancel;n&&n();return this.dataStore.getCache().watch({query:t,variables:r.variables,optimistic:!0,previousResult:function(){var t=null,r=o.getQuery(e).observableQuery;if(r){var n=r.getLastResult();n&&(t=n.data)}return t},callback:function(t){o.setQuery(e,function(){return{invalidated:!0,newData:t}})}})},e.prototype.addObservableQuery=function(e,t){this.setQuery(e,function(){return{observableQuery:t}})},e.prototype.removeObservableQuery=function(e){var t=this.getQuery(e).cancel;this.setQuery(e,function(){return{observableQuery:null}}),t&&t()},e.prototype.clearStore=function(){this.fetchQueryRejectFns.forEach(function(e){e(new s.InvariantError(14))});var e=[];return this.queries.forEach(function(t,r){t.observableQuery&&e.push(r)}),this.queryStore.reset(e),this.mutationStore.reset(),this.dataStore.reset()},e.prototype.resetStore=function(){var e=this;return this.clearStore().then(function(){return e.reFetchObservableQueries()})},e.prototype.reFetchObservableQueries=function(e){var t=this;void 0===e&&(e=!1);var r=[];return this.queries.forEach(function(o,n){var i=o.observableQuery;if(i){var s=i.options.fetchPolicy;i.resetLastResults(),"cache-only"===s||!e&&"standby"===s||r.push(i.refetch()),t.setQuery(n,function(){return{newData:null}}),t.invalidate(n)}}),this.broadcastQueries(),Promise.all(r)},e.prototype.observeQuery=function(e,t,r){return this.addQueryListener(e,this.queryListenerForObserver(e,t,r)),this.fetchQuery(e,t)},e.prototype.startQuery=function(e,t,r){return this.addQueryListener(e,r),this.fetchQuery(e,t).catch(function(){}),e},e.prototype.startGraphQLSubscription=function(e){var t=this,r=e.query,n=e.fetchPolicy,i=e.variables;r=this.transform(r).document,i=this.getVariables(r,i);var s=function(e){return t.getObservableFromLink(r,{},e,!1).map(function(i){if(n&&"no-cache"===n||(t.dataStore.markSubscriptionResult(i,r,e),t.broadcastQueries()),(0,o.graphQLResultHasError)(i))throw new f({graphQLErrors:i.errors});return i})};if(this.transform(r).hasClientExports){var a=this.localState.addExportedVariables(r,i).then(s);return new c(function(e){var t=null;return a.then(function(r){return t=r.subscribe(e)},e.error),function(){return t&&t.unsubscribe()}})}return s(i)},e.prototype.stopQuery=function(e){this.stopQueryNoBroadcast(e),this.broadcastQueries()},e.prototype.stopQueryNoBroadcast=function(e){this.stopQueryInStoreNoBroadcast(e),this.removeQuery(e)},e.prototype.removeQuery=function(e){this.fetchQueryRejectFns.delete("query:"+e),this.fetchQueryRejectFns.delete("fetchRequest:"+e),this.getQuery(e).subscriptions.forEach(function(e){return e.unsubscribe()}),this.queries.delete(e)},e.prototype.getCurrentQueryResult=function(e,t){void 0===t&&(t=!0);var r=e.options,o=r.variables,n=r.query,i=r.fetchPolicy,s=r.returnPartialData,a=e.getLastResult(),u=this.getQuery(e.queryId).newData;if(u&&u.complete)return{data:u.result,partial:!1};if("no-cache"===i||"network-only"===i)return{data:void 0,partial:!1};var c=this.dataStore.getCache().diff({query:n,variables:o,previousResult:a?a.data:void 0,returnPartialData:!0,optimistic:t}),l=c.result,h=c.complete;return{data:h||s?l:void 0,partial:!h}},e.prototype.getQueryWithPreviousResult=function(e){var t;if("string"==typeof e){var r=this.getQuery(e).observableQuery;(0,s.invariant)(r,15),t=r}else t=e;var o=t.options,n=o.variables,i=o.query;return{previousResult:this.getCurrentQueryResult(t,!1).data,variables:n,document:i}},e.prototype.broadcastQueries=function(){var e=this;this.onBroadcast(),this.queries.forEach(function(t,r){t.invalidated&&t.listeners.forEach(function(o){o&&o(e.queryStore.get(r),t.newData)})})},e.prototype.getLocalState=function(){return this.localState},e.prototype.getObservableFromLink=function(e,t,i,s){var a,u=this;void 0===s&&(s=this.queryDeduplication);var l=this.transform(e).serverQuery;if(l){var h=this.inFlightLinkObservables,p=this.link,y={query:l,variables:i,operationName:(0,o.getOperationName)(l)||void 0,context:this.prepareContext((0,r.__assign)({},t,{forceFetch:!s}))};if(t=y.context,s){var f=h.get(l)||new Map;h.set(l,f);var v=JSON.stringify(i);if(!(a=f.get(v))){f.set(v,a=S((0,n.execute)(p,y)));var d=function(){f.delete(v),f.size||h.delete(l),b.unsubscribe()},b=a.subscribe({next:d,error:d,complete:d})}}else a=S((0,n.execute)(p,y))}else a=c.of({data:{}}),t=this.prepareContext(t);var m=this.transform(e).clientQuery;return m&&(a=function(e,t){return new c(function(r){var o=r.next,n=r.error,i=r.complete,s=0,a=!1,u={next:function(e){++s,new Promise(function(r){r(t(e))}).then(function(e){--s,o&&o.call(r,e),a&&u.complete()},function(e){--s,n&&n.call(r,e)})},error:function(e){n&&n.call(r,e)},complete:function(){a=!0,s||i&&i.call(r)}},c=e.subscribe(u);return function(){return c.unsubscribe()}})}(a,function(e){return u.localState.runResolvers({document:m,remoteResult:e,context:t,variables:i})})),a},e.prototype.fetchRequest=function(e){var r,o,n=this,i=e.requestId,s=e.queryId,a=e.document,u=e.options,c=e.fetchMoreForQueryId,h=u.variables,p=u.errorPolicy,y=void 0===p?"none":p,v=u.fetchPolicy;return new Promise(function(e,p){var d=n.getObservableFromLink(a,u.context,h),b="fetchRequest:"+s;n.fetchQueryRejectFns.set(b,p);var m=function(){n.fetchQueryRejectFns.delete(b),n.setQuery(s,function(e){e.subscriptions.delete(g)})},g=d.map(function(e){if(i>=n.getQuery(s).lastRequestId&&(n.markQueryResult(s,e,u,c),n.queryStore.markQueryResult(s,e,c),n.invalidate(s),n.invalidate(c),n.broadcastQueries()),"none"===y&&l(e.errors))return p(new f({graphQLErrors:e.errors}));if("all"===y&&(o=e.errors),c||"no-cache"===v)r=e.data;else{var t=n.dataStore.getCache().diff({variables:h,query:a,optimistic:!1,returnPartialData:!0}),d=t.result;(t.complete||u.returnPartialData)&&(r=d)}}).subscribe({error:function(e){m(),p(e)},complete:function(){m(),e({data:r,errors:o,loading:!1,networkStatus:t.ready,stale:!1})}});n.setQuery(s,function(e){e.subscriptions.add(g)})})},e.prototype.getQuery=function(e){return this.queries.get(e)||{listeners:new Set,invalidated:!1,document:null,newData:null,lastRequestId:1,observableQuery:null,subscriptions:new Set}},e.prototype.setQuery=function(e,t){var o=this.getQuery(e),n=(0,r.__assign)({},o,t(o));this.queries.set(e,n)},e.prototype.invalidate=function(e,t){void 0===t&&(t=!0),e&&this.setQuery(e,function(){return{invalidated:t}})},e.prototype.prepareContext=function(e){void 0===e&&(e={});var t=this.localState.prepareContext(e);return(0,r.__assign)({},t,{clientAwareness:this.clientAwareness})},e.prototype.checkInFlight=function(e){var r=this.queryStore.get(e);return r&&r.networkStatus!==t.ready&&r.networkStatus!==t.error},e.prototype.startPollingQuery=function(e,t,o){var n=this,i=e.pollInterval;if((0,s.invariant)(i,16),!this.ssrMode){var a=this.pollingInfoByQueryId.get(t);a||this.pollingInfoByQueryId.set(t,a={}),a.interval=i,a.options=(0,r.__assign)({},e,{fetchPolicy:"network-only"});var u=function(){var e=n.pollingInfoByQueryId.get(t);e&&(n.checkInFlight(t)?c():n.fetchQuery(t,e.options,p.poll).then(c,c))},c=function(){var e=n.pollingInfoByQueryId.get(t);e&&(clearTimeout(e.timeout),e.timeout=setTimeout(u,e.interval))};o&&this.addQueryListener(t,o),c()}return t},e.prototype.stopPollingQuery=function(e){this.pollingInfoByQueryId.delete(e)},e}(),k=function(){function e(e){this.cache=e}return e.prototype.getCache=function(){return this.cache},e.prototype.markQueryResult=function(e,t,r,n,i){void 0===i&&(i=!1);var s=!(0,o.graphQLResultHasError)(e);i&&(0,o.graphQLResultHasError)(e)&&e.data&&(s=!0),!n&&s&&this.cache.write({result:e.data,dataId:"ROOT_QUERY",query:t,variables:r})},e.prototype.markSubscriptionResult=function(e,t,r){(0,o.graphQLResultHasError)(e)||this.cache.write({result:e.data,dataId:"ROOT_SUBSCRIPTION",query:t,variables:r})},e.prototype.markMutationInit=function(e){var t,r=this;e.optimisticResponse&&(t="function"==typeof e.optimisticResponse?e.optimisticResponse(e.variables):e.optimisticResponse,this.cache.recordOptimisticTransaction(function(o){var n=r.cache;r.cache=o;try{r.markMutationResult({mutationId:e.mutationId,result:{data:t},document:e.document,variables:e.variables,updateQueries:e.updateQueries,update:e.update})}finally{r.cache=n}},e.mutationId))},e.prototype.markMutationResult=function(e){var t=this;if(!(0,o.graphQLResultHasError)(e.result)){var r=[{result:e.result.data,dataId:"ROOT_MUTATION",query:e.document,variables:e.variables}],n=e.updateQueries;n&&Object.keys(n).forEach(function(i){var s=n[i],a=s.query,u=s.updater,c=t.cache.diff({query:a.document,variables:a.variables,returnPartialData:!0,optimistic:!1}),l=c.result;if(c.complete){var h=(0,o.tryFunctionOrLogError)(function(){return u(l,{mutationResult:e.result,queryName:(0,o.getOperationName)(a.document)||void 0,queryVariables:a.variables})});h&&r.push({result:h,dataId:"ROOT_QUERY",query:a.document,variables:a.variables})}}),this.cache.performTransaction(function(t){r.forEach(function(e){return t.write(e)});var n=e.update;n&&(0,o.tryFunctionOrLogError)(function(){return n(t,e.result)})})}},e.prototype.markMutationComplete=function(e){var t=e.mutationId;e.optimisticResponse&&this.cache.removeOptimistic(t)},e.prototype.markUpdateQueryResult=function(e,t,r){this.cache.write({result:r,dataId:"ROOT_QUERY",variables:t,query:e})},e.prototype.reset=function(){return this.cache.reset()},e}(),E="2.6.3",_=function(){function e(e){var t=this;this.defaultOptions={},this.resetStoreCallbacks=[],this.clearStoreCallbacks=[];var r=e.cache,o=e.ssrMode,i=void 0!==o&&o,a=e.ssrForceFetchDelay,u=void 0===a?0:a,c=e.connectToDevTools,l=e.queryDeduplication,h=void 0===l||l,p=e.defaultOptions,y=e.assumeImmutableResults,f=void 0!==y&&y,v=e.resolvers,d=e.typeDefs,b=e.fragmentMatcher,m=e.name,g=e.version,Q=e.link;if(!Q&&v&&(Q=n.ApolloLink.empty()),!Q||!r)throw new s.InvariantError(1);this.link=Q,this.cache=r,this.store=new k(r),this.disableNetworkFetches=i||u>0,this.queryDeduplication=h,this.defaultOptions=p||{},this.typeDefs=d,u&&setTimeout(function(){return t.disableNetworkFetches=!1},u),this.watchQuery=this.watchQuery.bind(this),this.query=this.query.bind(this),this.mutate=this.mutate.bind(this),this.resetStore=this.resetStore.bind(this),this.reFetchObservableQueries=this.reFetchObservableQueries.bind(this);void 0!==c&&(c&&"undefined"!=typeof window)&&(window.__APOLLO_CLIENT__=this),this.version=E,this.localState=new w({cache:r,client:this,resolvers:v,fragmentMatcher:b}),this.queryManager=new R({link:this.link,store:this.store,queryDeduplication:h,ssrMode:i,clientAwareness:{name:m,version:g},localState:this.localState,assumeImmutableResults:f,onBroadcast:function(){t.devToolsHookCb&&t.devToolsHookCb({action:{},state:{queries:t.queryManager.queryStore.getStore(),mutations:t.queryManager.mutationStore.getStore()},dataWithOptimisticResults:t.cache.extract(!0)})}})}return e.prototype.stop=function(){this.queryManager.stop()},e.prototype.watchQuery=function(e){return this.defaultOptions.watchQuery&&(e=(0,r.__assign)({},this.defaultOptions.watchQuery,e)),!this.disableNetworkFetches||"network-only"!==e.fetchPolicy&&"cache-and-network"!==e.fetchPolicy||(e=(0,r.__assign)({},e,{fetchPolicy:"cache-first"})),this.queryManager.watchQuery(e)},e.prototype.query=function(e){return this.defaultOptions.query&&(e=(0,r.__assign)({},this.defaultOptions.query,e)),(0,s.invariant)("cache-and-network"!==e.fetchPolicy,2),this.disableNetworkFetches&&"network-only"===e.fetchPolicy&&(e=(0,r.__assign)({},e,{fetchPolicy:"cache-first"})),this.queryManager.query(e)},e.prototype.mutate=function(e){return this.defaultOptions.mutate&&(e=(0,r.__assign)({},this.defaultOptions.mutate,e)),this.queryManager.mutate(e)},e.prototype.subscribe=function(e){return this.queryManager.startGraphQLSubscription(e)},e.prototype.readQuery=function(e,t){return void 0===t&&(t=!1),this.cache.readQuery(e,t)},e.prototype.readFragment=function(e,t){return void 0===t&&(t=!1),this.cache.readFragment(e,t)},e.prototype.writeQuery=function(e){var t=this.cache.writeQuery(e);return this.queryManager.broadcastQueries(),t},e.prototype.writeFragment=function(e){var t=this.cache.writeFragment(e);return this.queryManager.broadcastQueries(),t},e.prototype.writeData=function(e){var t=this.cache.writeData(e);return this.queryManager.broadcastQueries(),t},e.prototype.__actionHookForDevTools=function(e){this.devToolsHookCb=e},e.prototype.__requestRaw=function(e){return(0,n.execute)(this.link,e)},e.prototype.initQueryManager=function(){return this.queryManager},e.prototype.resetStore=function(){var e=this;return Promise.resolve().then(function(){return e.queryManager.clearStore()}).then(function(){return Promise.all(e.resetStoreCallbacks.map(function(e){return e()}))}).then(function(){return e.reFetchObservableQueries()})},e.prototype.clearStore=function(){var e=this;return Promise.resolve().then(function(){return e.queryManager.clearStore()}).then(function(){return Promise.all(e.clearStoreCallbacks.map(function(e){return e()}))})},e.prototype.onResetStore=function(e){var t=this;return this.resetStoreCallbacks.push(e),function(){t.resetStoreCallbacks=t.resetStoreCallbacks.filter(function(t){return t!==e})}},e.prototype.onClearStore=function(e){var t=this;return this.clearStoreCallbacks.push(e),function(){t.clearStoreCallbacks=t.clearStoreCallbacks.filter(function(t){return t!==e})}},e.prototype.reFetchObservableQueries=function(e){return this.queryManager.reFetchObservableQueries(e)},e.prototype.extract=function(e){return this.cache.extract(e)},e.prototype.restore=function(e){return this.cache.restore(e)},e.prototype.addResolvers=function(e){this.localState.addResolvers(e)},e.prototype.setResolvers=function(e){this.localState.setResolvers(e)},e.prototype.getResolvers=function(){return this.localState.getResolvers()},e.prototype.setLocalStateFragmentMatcher=function(e){this.localState.setFragmentMatcher(e)},e}();exports.ApolloClient=_;var I=_;exports.default=I;