@apollo/react-hooks
Advanced tools
Comparing version 3.0.1 to 3.1.0-beta.0
@@ -102,3 +102,3 @@ import * as tslib_1 from "tslib"; | ||
called: true, | ||
data: {} | ||
data: undefined | ||
}; | ||
@@ -162,3 +162,3 @@ if (this.context && this.context.renderPromises) { | ||
_this.previousData.result.networkStatus === networkStatus && | ||
isEqual(_this.previousData.result.data, data || {})) { | ||
isEqual(_this.previousData.result.data, data)) { | ||
return; | ||
@@ -191,6 +191,3 @@ } | ||
QueryData.prototype.getQueryResult = function () { | ||
var result = { | ||
data: Object.create(null) | ||
}; | ||
Object.assign(result, this.observableQueryFields(this.currentObservable.query)); | ||
var result = tslib_1.__assign({}, this.observableQueryFields(this.currentObservable.query)); | ||
if (this.getOptions().skip) { | ||
@@ -203,12 +200,13 @@ result = tslib_1.__assign({}, result, { data: undefined, error: undefined, loading: false, called: true }); | ||
var error = currentResult.error, data = currentResult.data; | ||
data = data || Object.create(null); | ||
if (errors && errors.length > 0) { | ||
error = new ApolloError({ graphQLErrors: errors }); | ||
} | ||
Object.assign(result, { loading: loading, networkStatus: networkStatus, error: error, called: true }); | ||
result = tslib_1.__assign({}, result, { loading: loading, | ||
networkStatus: networkStatus, | ||
error: error, called: true }); | ||
if (loading) { | ||
var previousData = this.previousData.result | ||
? this.previousData.result.data | ||
: {}; | ||
Object.assign(result.data, previousData, data); | ||
var previousData = this.previousData.result && this.previousData.result.data; | ||
result.data = | ||
previousData && data | ||
? tslib_1.__assign({}, previousData, data) : previousData || data; | ||
} | ||
@@ -225,3 +223,3 @@ else if (error) { | ||
if (partialRefetch && | ||
Object.keys(data).length === 0 && | ||
!data && | ||
partial && | ||
@@ -236,3 +234,3 @@ fetchPolicy !== 'cache-only') { | ||
} | ||
Object.assign(result.data, data); | ||
result.data = data; | ||
} | ||
@@ -239,0 +237,0 @@ } |
@@ -217,3 +217,3 @@ exports.__esModule = true; | ||
called: true, | ||
data: {} | ||
data: undefined | ||
}; | ||
@@ -298,3 +298,3 @@ | ||
if (_this.previousData.result && _this.previousData.result.loading === loading && _this.previousData.result.networkStatus === networkStatus && (0, _equality.equal)(_this.previousData.result.data, data || {})) { | ||
if (_this.previousData.result && _this.previousData.result.loading === loading && _this.previousData.result.networkStatus === networkStatus && (0, _equality.equal)(_this.previousData.result.data, data)) { | ||
return; | ||
@@ -332,6 +332,3 @@ } | ||
QueryData.prototype.getQueryResult = function () { | ||
var result = { | ||
data: Object.create(null) | ||
}; | ||
Object.assign(result, this.observableQueryFields(this.currentObservable.query)); | ||
var result = (0, _tslib.__assign)({}, this.observableQueryFields(this.currentObservable.query)); | ||
@@ -353,3 +350,2 @@ if (this.getOptions().skip) { | ||
data = currentResult.data; | ||
data = data || Object.create(null); | ||
@@ -362,3 +358,3 @@ if (errors && errors.length > 0) { | ||
Object.assign(result, { | ||
result = (0, _tslib.__assign)({}, result, { | ||
loading: loading, | ||
@@ -371,4 +367,4 @@ networkStatus: networkStatus, | ||
if (loading) { | ||
var previousData = this.previousData.result ? this.previousData.result.data : {}; | ||
Object.assign(result.data, previousData, data); | ||
var previousData = this.previousData.result && this.previousData.result.data; | ||
result.data = previousData && data ? (0, _tslib.__assign)({}, previousData, data) : previousData || data; | ||
} else if (error) { | ||
@@ -382,3 +378,3 @@ Object.assign(result, { | ||
if (partialRefetch && Object.keys(data).length === 0 && partial && fetchPolicy !== 'cache-only') { | ||
if (partialRefetch && !data && partial && fetchPolicy !== 'cache-only') { | ||
Object.assign(result, { | ||
@@ -392,3 +388,3 @@ loading: true, | ||
Object.assign(result.data, data); | ||
result.data = data; | ||
} | ||
@@ -395,0 +391,0 @@ } |
@@ -1,1 +0,1 @@ | ||
exports.__esModule=!0,exports.useApolloClient=function(){var e=r.default.useContext((0,t.getApolloContext)()).client;return(0,o.invariant)(e,1),e},exports.useLazyQuery=function(t,e){return a(t,e,!0)},exports.useMutation=function(i,s){var o=(0,r.useContext)((0,t.getApolloContext)()),n=(0,r.useState)({called:!1,loading:!1}),u=n[0],a=n[1],c=s?(0,e.__assign)({},s,{mutation:i}):{mutation:i},l=(0,r.useRef)();var h=function(){l.current||(l.current=new p({options:c,context:o,result:u,setResult:a}));return l.current}();return h.setOptions(c),h.context=o,(0,r.useEffect)(function(){return h.afterExecute()}),h.execute(u)},exports.useQuery=function(t,e){return a(t,e,!1)},exports.useSubscription=function(i,s){var o=(0,r.useContext)((0,t.getApolloContext)()),n=(0,r.useState)({loading:!0,error:void 0,data:void 0}),u=n[0],a=n[1],p=s?(0,e.__assign)({},s,{subscription:i}):{subscription:i},l=(0,r.useRef)();var h=function(){l.current||(l.current=new c({options:p,context:o,setResult:a}));return l.current}();return h.setOptions(p),h.context=o,(0,r.useEffect)(function(){return h.afterExecute()}),(0,r.useEffect)(function(){return h.cleanup.bind(h)},[]),h.execute(u)},exports.RenderPromises=exports.resetApolloContext=exports.ApolloProvider=exports.ApolloConsumer=void 0;var t=require("@apollo/react-common");exports.getApolloContext=t.getApolloContext,exports.ApolloConsumer=t.ApolloConsumer,exports.ApolloProvider=t.ApolloProvider,exports.resetApolloContext=t.resetApolloContext;var e=require("tslib"),r=function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r in t)if(Object.prototype.hasOwnProperty.call(t,r)){var i=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(t,r):{};i.get||i.set?Object.defineProperty(e,r,i):e[r]=t[r]}return e.default=t,e}(require("react")),i=require("apollo-client"),s=require("@wry/equality"),o=require("ts-invariant");var n=function(){function e(t,e){this.isMounted=!1,this.previousOptions={},this.context={},this.options={},this.options=t||{},this.context=e||{}}return e.prototype.getOptions=function(){return this.options},e.prototype.setOptions=function(t){(0,s.equal)(this.options,t)||(this.previousOptions=this.options),this.options=t},e.prototype.unmount=function(){this.isMounted=!1},e.prototype.refreshClient=function(){var t=this.options&&this.options.client||this.context&&this.context.client;(0,o.invariant)(!!t,2);var e=!1;return t!==this.client&&(e=!0,this.client=t,this.cleanup()),{client:this.client,isNew:e}},e.prototype.verifyDocumentType=function(e,r){var i=(0,t.parser)(e);(0,t.operationName)(r),(0,t.operationName)(i.type);(0,o.invariant)(i.type===r,3)},e}(),u=function(r){function o(t){var e=t.options,i=t.context,s=t.forceUpdate,o=r.call(this,e,i)||this;return o.previousData={},o.currentObservable={},o.runLazy=!1,o.runLazyQuery=function(t){o.runLazy=!0,o.lazyOptions=t,o.forceUpdate()},o.getExecuteResult=function(){var t=o.getQueryResult();return o.startQuerySubscription(),t},o.forceUpdate=s,o}return(0,e.__extends)(o,r),o.prototype.execute=function(){this.refreshClient();var t=this.getOptions(),e=t.skip,r=t.query;return(e||r!==this.previousData.query)&&(this.removeQuerySubscription(),this.previousData.query=r),this.updateObservableQuery(),this.isMounted&&this.startQuerySubscription(),this.getExecuteSsrResult()||this.getExecuteResult()},o.prototype.executeLazy=function(){return this.runLazy?[this.runLazyQuery,this.execute()]:[this.runLazyQuery,{loading:!1,networkStatus:i.NetworkStatus.ready,called:!1,data:void 0}]},o.prototype.fetchData=function(){if(this.getOptions().skip)return!1;var t=this.getOptions(),r=(t.children,t.ssr),i=(t.displayName,t.skip,t.onCompleted,t.onError,t.partialRefetch,(0,e.__rest)(t,["children","ssr","displayName","skip","onCompleted","onError","partialRefetch"])),s=i.fetchPolicy;if(!1===r)return!1;"network-only"!==s&&"cache-and-network"!==s||(s="cache-first");var o=this.refreshClient().client.watchQuery((0,e.__assign)({},i,{fetchPolicy:s}));return this.context&&this.context.renderPromises&&this.context.renderPromises.registerSSRObservable(o,this.getOptions()),!!this.currentObservable.query.getCurrentResult().loading&&o.result()},o.prototype.afterExecute=function(t){var e=this,r=(void 0===t?{}:t).lazy,i=void 0!==r&&r;return this.isMounted=!0,i&&!this.runLazy||(this.handleErrorOrCompleted(),setTimeout(function(){e.currentObservable.query&&e.currentObservable.query.resetQueryStoreErrors()})),this.unmount.bind(this)},o.prototype.cleanup=function(){this.removeQuerySubscription(),delete this.currentObservable.query,delete this.previousData.result},o.prototype.getOptions=function(){var t=r.prototype.getOptions.call(this),i=this.lazyOptions||{},s=(0,e.__assign)({},t,{variables:(0,e.__assign)({},t.variables,i.variables),context:(0,e.__assign)({},t.context,i.context)});return this.runLazy&&delete s.skip,s},o.prototype.getExecuteSsrResult=function(){var t,e={loading:!0,networkStatus:i.NetworkStatus.loading,called:!0,data:{}};return this.context&&this.context.renderPromises&&((t=this.context.renderPromises.addQueryPromise(this,this.getExecuteResult))||(t=e)),t},o.prototype.prepareObservableQueryOptions=function(){this.verifyDocumentType(this.getOptions().query,t.DocumentType.Query);var r=this.getOptions().displayName||"Query";return(0,e.__assign)({},this.getOptions(),{displayName:r,context:this.getOptions().context||{},metadata:{reactComponent:{displayName:r}}})},o.prototype.observableQueryFields=function(t){return{variables:t.variables,refetch:t.refetch.bind(t),fetchMore:t.fetchMore.bind(t),updateQuery:t.updateQuery.bind(t),startPolling:t.startPolling.bind(t),stopPolling:t.stopPolling.bind(t),subscribeToMore:t.subscribeToMore.bind(t)}},o.prototype.initializeObservableQuery=function(){if(this.context&&this.context.renderPromises&&(this.currentObservable.query=this.context.renderPromises.getSSRObservable(this.getOptions())),!this.currentObservable.query){var t=this.prepareObservableQueryOptions();this.previousData.observableQueryOptions=(0,e.__assign)({},t,{children:null}),this.currentObservable.query=this.refreshClient().client.watchQuery(t)}},o.prototype.updateObservableQuery=function(){this.currentObservable.query||this.initializeObservableQuery();var t=(0,e.__assign)({},this.prepareObservableQueryOptions(),{children:null});(0,s.equal)(t,this.previousData.observableQueryOptions)||(this.previousData.observableQueryOptions=t,this.currentObservable.query.setOptions(t).catch(function(){}))},o.prototype.startQuerySubscription=function(){var t=this;if(!this.currentObservable.subscription&&!this.getOptions().skip){var e=this.currentObservable.query;this.currentObservable.subscription=e.subscribe({next:function(e){var r=e.loading,i=e.networkStatus,o=e.data;t.previousData.result&&t.previousData.result.loading===r&&t.previousData.result.networkStatus===i&&(0,s.equal)(t.previousData.result.data,o||{})||t.forceUpdate()},error:function(e){if(t.resubscribeToQuery(),!e.hasOwnProperty("graphQLErrors"))throw e;(0,s.equal)(e,t.previousData.error)||(t.previousData.error=e,t.forceUpdate())}})}},o.prototype.resubscribeToQuery=function(){this.removeQuerySubscription();var t=this.currentObservable.query.getLastError(),e=this.currentObservable.query.getLastResult();this.currentObservable.query.resetLastResults(),this.startQuerySubscription(),Object.assign(this.currentObservable.query,{lastError:t,lastResult:e})},o.prototype.getQueryResult=function(){var t={data:Object.create(null)};if(Object.assign(t,this.observableQueryFields(this.currentObservable.query)),this.getOptions().skip)t=(0,e.__assign)({},t,{data:void 0,error:void 0,loading:!1,called:!0});else{var r=this.currentObservable.query.getCurrentResult(),s=r.loading,o=r.partial,n=r.networkStatus,u=r.errors,a=r.error,p=r.data;if(p=p||Object.create(null),u&&u.length>0&&(a=new i.ApolloError({graphQLErrors:u})),Object.assign(t,{loading:s,networkStatus:n,error:a,called:!0}),s){var c=this.previousData.result?this.previousData.result.data:{};Object.assign(t.data,c,p)}else if(a)Object.assign(t,{data:(this.currentObservable.query.getLastResult()||{}).data});else{var l=this.currentObservable.query.options.fetchPolicy;if(this.getOptions().partialRefetch&&0===Object.keys(p).length&&o&&"cache-only"!==l)return Object.assign(t,{loading:!0,networkStatus:i.NetworkStatus.loading}),t.refetch(),t;Object.assign(t.data,p)}}return t.client=this.client,this.previousData.loading=this.previousData.result&&this.previousData.result.loading||!1,this.previousData.result=t,t},o.prototype.handleErrorOrCompleted=function(){var t=this.currentObservable.query.getCurrentResult(),e=t.data,r=t.loading,i=t.error;if(!r){var o=this.getOptions(),n=o.query,u=o.variables,a=o.onCompleted,p=o.onError;if(this.previousOptions&&!this.previousData.loading&&(0,s.equal)(this.previousOptions.query,n)&&(0,s.equal)(this.previousOptions.variables,u))return;a&&!i?a(e):p&&i&&p(i)}},o.prototype.removeQuerySubscription=function(){this.currentObservable.subscription&&(this.currentObservable.subscription.unsubscribe(),delete this.currentObservable.subscription)},o}(n);function a(i,o,n){void 0===n&&(n=!1);var a=(0,r.useContext)((0,t.getApolloContext)()),p=(0,r.useReducer)(function(t){return t+1},0),c=p[0],l=p[1],h=o?(0,e.__assign)({},o,{query:i}):{query:i},b=(0,r.useRef)();b.current||(b.current=new u({options:h,context:a,forceUpdate:l}));var v=b.current;v.setOptions(h),v.context=a;var y,d,f,g=(y=function(){return n?v.executeLazy():v.execute()},d={options:h,context:a,tick:c},(f=(0,r.useRef)()).current&&(0,s.equal)(d,f.current.key)||(f.current={key:d,value:y()}),f.current.value);return(0,r.useEffect)(function(){return v.afterExecute({lazy:n})},[g]),(0,r.useEffect)(function(){return function(){return v.cleanup()}},[]),g}var p=function(r){function o(e){var i=e.options,s=e.context,o=e.result,n=e.setResult,u=r.call(this,i,s)||this;return u.runMutation=function(t){void 0===t&&(t={}),u.onMutationStart();var e=u.generateNewMutationId();return u.mutate(t).then(function(t){return u.onMutationCompleted(t,e),t}).catch(function(t){if(u.onMutationError(t,e),!u.getOptions().onError)throw t})},u.verifyDocumentType(i.mutation,t.DocumentType.Mutation),u.result=o,u.setResult=n,u.mostRecentMutationId=0,u}return(0,e.__extends)(o,r),o.prototype.execute=function(e){return this.isMounted=!0,this.verifyDocumentType(this.getOptions().mutation,t.DocumentType.Mutation),[this.runMutation,e]},o.prototype.afterExecute=function(){return this.isMounted=!0,this.unmount.bind(this)},o.prototype.cleanup=function(){},o.prototype.mutate=function(t){var r=this.getOptions(),i=r.mutation,s=r.variables,o=r.optimisticResponse,n=r.update,u=r.context,a=void 0===u?{}:u,p=r.awaitRefetchQueries,c=void 0!==p&&p,l=r.fetchPolicy,h=(0,e.__assign)({},t),b=Object.assign({},s,h.variables);return delete h.variables,this.refreshClient().client.mutate((0,e.__assign)({mutation:i,optimisticResponse:o,refetchQueries:h.refetchQueries||this.getOptions().refetchQueries,awaitRefetchQueries:c,update:n,context:a,fetchPolicy:l,variables:b},h))},o.prototype.onMutationStart=function(){this.result.loading||this.getOptions().ignoreResults||this.updateResult({loading:!0,error:void 0,data:void 0,called:!0})},o.prototype.onMutationCompleted=function(t,e){var r=this.getOptions(),s=r.onCompleted,o=r.ignoreResults,n=t.data,u=t.errors,a=u&&u.length>0?new i.ApolloError({graphQLErrors:u}):void 0;this.isMostRecentMutation(e)&&!o&&this.updateResult({called:!0,loading:!1,data:n,error:a}),s&&s(n)},o.prototype.onMutationError=function(t,e){var r=this.getOptions().onError;this.isMostRecentMutation(e)&&this.updateResult({loading:!1,error:t,data:void 0,called:!0}),r&&r(t)},o.prototype.generateNewMutationId=function(){return++this.mostRecentMutationId},o.prototype.isMostRecentMutation=function(t){return this.mostRecentMutationId===t},o.prototype.updateResult=function(t){!this.isMounted||this.previousResult&&(0,s.equal)(this.previousResult,t)||(this.setResult(t),this.previousResult=t)},o}(n);var c=function(t){function r(e){var r=e.options,i=e.context,s=e.setResult,o=t.call(this,r,i)||this;return o.currentObservable={},o.setResult=s,o.initialize(r),o}return(0,e.__extends)(r,t),r.prototype.execute=function(t){var r=t;this.refreshClient().isNew&&(r=this.getLoadingResult());var i=this.getOptions().shouldResubscribe;return"function"==typeof i&&(i=!!i(this.getOptions())),!1!==i&&this.previousOptions&&Object.keys(this.previousOptions).length>0&&(this.previousOptions.subscription!==this.getOptions().subscription||!(0,s.equal)(this.previousOptions.variables,this.getOptions().variables))&&(this.endSubscription(),delete this.currentObservable.query,r=this.getLoadingResult()),this.initialize(this.getOptions()),this.startSubscription(),this.previousOptions=this.getOptions(),(0,e.__assign)({},r,{variables:this.getOptions().variables})},r.prototype.afterExecute=function(){this.isMounted=!0},r.prototype.cleanup=function(){this.endSubscription(),delete this.currentObservable.query},r.prototype.initialize=function(t){this.currentObservable.query||(this.currentObservable.query=this.refreshClient().client.subscribe({query:t.subscription,variables:t.variables,fetchPolicy:t.fetchPolicy}))},r.prototype.startSubscription=function(){this.currentObservable.subscription||(this.currentObservable.subscription=this.currentObservable.query.subscribe({next:this.updateCurrentData.bind(this),error:this.updateError.bind(this),complete:this.completeSubscription.bind(this)}))},r.prototype.getLoadingResult=function(){return{loading:!0,error:void 0,data:void 0}},r.prototype.updateResult=function(t){this.isMounted&&this.setResult(t)},r.prototype.updateCurrentData=function(t){var e=this.getOptions().onSubscriptionData;this.updateResult({data:t.data,loading:!1,error:void 0}),e&&e({client:this.refreshClient().client,subscriptionData:t})},r.prototype.updateError=function(t){this.updateResult({error:t,loading:!1})},r.prototype.completeSubscription=function(){var t=this.getOptions().onSubscriptionComplete;t&&t(),this.endSubscription()},r.prototype.endSubscription=function(){this.currentObservable.subscription&&(this.currentObservable.subscription.unsubscribe(),delete this.currentObservable.subscription)},r}(n);var l=function(){function t(){this.queryPromises=new Map,this.queryInfoTrie=new Map}return t.prototype.registerSSRObservable=function(t,e){this.lookupQueryInfo(e).observable=t},t.prototype.getSSRObservable=function(t){return this.lookupQueryInfo(t).observable},t.prototype.addQueryPromise=function(t,e){return this.lookupQueryInfo(t.getOptions()).seen?e():(this.queryPromises.set(t.getOptions(),new Promise(function(e){e(t.fetchData())})),null)},t.prototype.hasPromises=function(){return this.queryPromises.size>0},t.prototype.consumeAndAwaitPromises=function(){var t=this,e=[];return this.queryPromises.forEach(function(r,i){t.lookupQueryInfo(i).seen=!0,e.push(r)}),this.queryPromises.clear(),Promise.all(e)},t.prototype.lookupQueryInfo=function(t){var e=this.queryInfoTrie,r=t.query,i=t.variables,s=e.get(r)||new Map;e.has(r)||e.set(r,s);var o=JSON.stringify(i),n=s.get(o)||{seen:!1,observable:null};return s.has(o)||s.set(o,n),n},t}();exports.RenderPromises=l; | ||
exports.__esModule=!0,exports.useApolloClient=function(){var e=r.default.useContext((0,t.getApolloContext)()).client;return(0,o.invariant)(e,1),e},exports.useLazyQuery=function(t,e){return a(t,e,!0)},exports.useMutation=function(i,s){var o=(0,r.useContext)((0,t.getApolloContext)()),n=(0,r.useState)({called:!1,loading:!1}),u=n[0],a=n[1],c=s?(0,e.__assign)({},s,{mutation:i}):{mutation:i},l=(0,r.useRef)();var h=function(){l.current||(l.current=new p({options:c,context:o,result:u,setResult:a}));return l.current}();return h.setOptions(c),h.context=o,(0,r.useEffect)(function(){return h.afterExecute()}),h.execute(u)},exports.useQuery=function(t,e){return a(t,e,!1)},exports.useSubscription=function(i,s){var o=(0,r.useContext)((0,t.getApolloContext)()),n=(0,r.useState)({loading:!0,error:void 0,data:void 0}),u=n[0],a=n[1],p=s?(0,e.__assign)({},s,{subscription:i}):{subscription:i},l=(0,r.useRef)();var h=function(){l.current||(l.current=new c({options:p,context:o,setResult:a}));return l.current}();return h.setOptions(p),h.context=o,(0,r.useEffect)(function(){return h.afterExecute()}),(0,r.useEffect)(function(){return h.cleanup.bind(h)},[]),h.execute(u)},exports.RenderPromises=exports.resetApolloContext=exports.ApolloProvider=exports.ApolloConsumer=void 0;var t=require("@apollo/react-common");exports.getApolloContext=t.getApolloContext,exports.ApolloConsumer=t.ApolloConsumer,exports.ApolloProvider=t.ApolloProvider,exports.resetApolloContext=t.resetApolloContext;var e=require("tslib"),r=function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r in t)if(Object.prototype.hasOwnProperty.call(t,r)){var i=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(t,r):{};i.get||i.set?Object.defineProperty(e,r,i):e[r]=t[r]}return e.default=t,e}(require("react")),i=require("apollo-client"),s=require("@wry/equality"),o=require("ts-invariant");var n=function(){function e(t,e){this.isMounted=!1,this.previousOptions={},this.context={},this.options={},this.options=t||{},this.context=e||{}}return e.prototype.getOptions=function(){return this.options},e.prototype.setOptions=function(t){(0,s.equal)(this.options,t)||(this.previousOptions=this.options),this.options=t},e.prototype.unmount=function(){this.isMounted=!1},e.prototype.refreshClient=function(){var t=this.options&&this.options.client||this.context&&this.context.client;(0,o.invariant)(!!t,2);var e=!1;return t!==this.client&&(e=!0,this.client=t,this.cleanup()),{client:this.client,isNew:e}},e.prototype.verifyDocumentType=function(e,r){var i=(0,t.parser)(e);(0,t.operationName)(r),(0,t.operationName)(i.type);(0,o.invariant)(i.type===r,3)},e}(),u=function(r){function o(t){var e=t.options,i=t.context,s=t.forceUpdate,o=r.call(this,e,i)||this;return o.previousData={},o.currentObservable={},o.runLazy=!1,o.runLazyQuery=function(t){o.runLazy=!0,o.lazyOptions=t,o.forceUpdate()},o.getExecuteResult=function(){var t=o.getQueryResult();return o.startQuerySubscription(),t},o.forceUpdate=s,o}return(0,e.__extends)(o,r),o.prototype.execute=function(){this.refreshClient();var t=this.getOptions(),e=t.skip,r=t.query;return(e||r!==this.previousData.query)&&(this.removeQuerySubscription(),this.previousData.query=r),this.updateObservableQuery(),this.isMounted&&this.startQuerySubscription(),this.getExecuteSsrResult()||this.getExecuteResult()},o.prototype.executeLazy=function(){return this.runLazy?[this.runLazyQuery,this.execute()]:[this.runLazyQuery,{loading:!1,networkStatus:i.NetworkStatus.ready,called:!1,data:void 0}]},o.prototype.fetchData=function(){if(this.getOptions().skip)return!1;var t=this.getOptions(),r=(t.children,t.ssr),i=(t.displayName,t.skip,t.onCompleted,t.onError,t.partialRefetch,(0,e.__rest)(t,["children","ssr","displayName","skip","onCompleted","onError","partialRefetch"])),s=i.fetchPolicy;if(!1===r)return!1;"network-only"!==s&&"cache-and-network"!==s||(s="cache-first");var o=this.refreshClient().client.watchQuery((0,e.__assign)({},i,{fetchPolicy:s}));return this.context&&this.context.renderPromises&&this.context.renderPromises.registerSSRObservable(o,this.getOptions()),!!this.currentObservable.query.getCurrentResult().loading&&o.result()},o.prototype.afterExecute=function(t){var e=this,r=(void 0===t?{}:t).lazy,i=void 0!==r&&r;return this.isMounted=!0,i&&!this.runLazy||(this.handleErrorOrCompleted(),setTimeout(function(){e.currentObservable.query&&e.currentObservable.query.resetQueryStoreErrors()})),this.unmount.bind(this)},o.prototype.cleanup=function(){this.removeQuerySubscription(),delete this.currentObservable.query,delete this.previousData.result},o.prototype.getOptions=function(){var t=r.prototype.getOptions.call(this),i=this.lazyOptions||{},s=(0,e.__assign)({},t,{variables:(0,e.__assign)({},t.variables,i.variables),context:(0,e.__assign)({},t.context,i.context)});return this.runLazy&&delete s.skip,s},o.prototype.getExecuteSsrResult=function(){var t,e={loading:!0,networkStatus:i.NetworkStatus.loading,called:!0,data:void 0};return this.context&&this.context.renderPromises&&((t=this.context.renderPromises.addQueryPromise(this,this.getExecuteResult))||(t=e)),t},o.prototype.prepareObservableQueryOptions=function(){this.verifyDocumentType(this.getOptions().query,t.DocumentType.Query);var r=this.getOptions().displayName||"Query";return(0,e.__assign)({},this.getOptions(),{displayName:r,context:this.getOptions().context||{},metadata:{reactComponent:{displayName:r}}})},o.prototype.observableQueryFields=function(t){return{variables:t.variables,refetch:t.refetch.bind(t),fetchMore:t.fetchMore.bind(t),updateQuery:t.updateQuery.bind(t),startPolling:t.startPolling.bind(t),stopPolling:t.stopPolling.bind(t),subscribeToMore:t.subscribeToMore.bind(t)}},o.prototype.initializeObservableQuery=function(){if(this.context&&this.context.renderPromises&&(this.currentObservable.query=this.context.renderPromises.getSSRObservable(this.getOptions())),!this.currentObservable.query){var t=this.prepareObservableQueryOptions();this.previousData.observableQueryOptions=(0,e.__assign)({},t,{children:null}),this.currentObservable.query=this.refreshClient().client.watchQuery(t)}},o.prototype.updateObservableQuery=function(){this.currentObservable.query||this.initializeObservableQuery();var t=(0,e.__assign)({},this.prepareObservableQueryOptions(),{children:null});(0,s.equal)(t,this.previousData.observableQueryOptions)||(this.previousData.observableQueryOptions=t,this.currentObservable.query.setOptions(t).catch(function(){}))},o.prototype.startQuerySubscription=function(){var t=this;if(!this.currentObservable.subscription&&!this.getOptions().skip){var e=this.currentObservable.query;this.currentObservable.subscription=e.subscribe({next:function(e){var r=e.loading,i=e.networkStatus,o=e.data;t.previousData.result&&t.previousData.result.loading===r&&t.previousData.result.networkStatus===i&&(0,s.equal)(t.previousData.result.data,o)||t.forceUpdate()},error:function(e){if(t.resubscribeToQuery(),!e.hasOwnProperty("graphQLErrors"))throw e;(0,s.equal)(e,t.previousData.error)||(t.previousData.error=e,t.forceUpdate())}})}},o.prototype.resubscribeToQuery=function(){this.removeQuerySubscription();var t=this.currentObservable.query.getLastError(),e=this.currentObservable.query.getLastResult();this.currentObservable.query.resetLastResults(),this.startQuerySubscription(),Object.assign(this.currentObservable.query,{lastError:t,lastResult:e})},o.prototype.getQueryResult=function(){var t=(0,e.__assign)({},this.observableQueryFields(this.currentObservable.query));if(this.getOptions().skip)t=(0,e.__assign)({},t,{data:void 0,error:void 0,loading:!1,called:!0});else{var r=this.currentObservable.query.getCurrentResult(),s=r.loading,o=r.partial,n=r.networkStatus,u=r.errors,a=r.error,p=r.data;if(u&&u.length>0&&(a=new i.ApolloError({graphQLErrors:u})),t=(0,e.__assign)({},t,{loading:s,networkStatus:n,error:a,called:!0}),s){var c=this.previousData.result&&this.previousData.result.data;t.data=c&&p?(0,e.__assign)({},c,p):c||p}else if(a)Object.assign(t,{data:(this.currentObservable.query.getLastResult()||{}).data});else{var l=this.currentObservable.query.options.fetchPolicy;if(this.getOptions().partialRefetch&&!p&&o&&"cache-only"!==l)return Object.assign(t,{loading:!0,networkStatus:i.NetworkStatus.loading}),t.refetch(),t;t.data=p}}return t.client=this.client,this.previousData.loading=this.previousData.result&&this.previousData.result.loading||!1,this.previousData.result=t,t},o.prototype.handleErrorOrCompleted=function(){var t=this.currentObservable.query.getCurrentResult(),e=t.data,r=t.loading,i=t.error;if(!r){var o=this.getOptions(),n=o.query,u=o.variables,a=o.onCompleted,p=o.onError;if(this.previousOptions&&!this.previousData.loading&&(0,s.equal)(this.previousOptions.query,n)&&(0,s.equal)(this.previousOptions.variables,u))return;a&&!i?a(e):p&&i&&p(i)}},o.prototype.removeQuerySubscription=function(){this.currentObservable.subscription&&(this.currentObservable.subscription.unsubscribe(),delete this.currentObservable.subscription)},o}(n);function a(i,o,n){void 0===n&&(n=!1);var a=(0,r.useContext)((0,t.getApolloContext)()),p=(0,r.useReducer)(function(t){return t+1},0),c=p[0],l=p[1],h=o?(0,e.__assign)({},o,{query:i}):{query:i},b=(0,r.useRef)();b.current||(b.current=new u({options:h,context:a,forceUpdate:l}));var v=b.current;v.setOptions(h),v.context=a;var y,d,f,g=(y=function(){return n?v.executeLazy():v.execute()},d={options:h,context:a,tick:c},(f=(0,r.useRef)()).current&&(0,s.equal)(d,f.current.key)||(f.current={key:d,value:y()}),f.current.value);return(0,r.useEffect)(function(){return v.afterExecute({lazy:n})},[g]),(0,r.useEffect)(function(){return function(){return v.cleanup()}},[]),g}var p=function(r){function o(e){var i=e.options,s=e.context,o=e.result,n=e.setResult,u=r.call(this,i,s)||this;return u.runMutation=function(t){void 0===t&&(t={}),u.onMutationStart();var e=u.generateNewMutationId();return u.mutate(t).then(function(t){return u.onMutationCompleted(t,e),t}).catch(function(t){if(u.onMutationError(t,e),!u.getOptions().onError)throw t})},u.verifyDocumentType(i.mutation,t.DocumentType.Mutation),u.result=o,u.setResult=n,u.mostRecentMutationId=0,u}return(0,e.__extends)(o,r),o.prototype.execute=function(e){return this.isMounted=!0,this.verifyDocumentType(this.getOptions().mutation,t.DocumentType.Mutation),[this.runMutation,e]},o.prototype.afterExecute=function(){return this.isMounted=!0,this.unmount.bind(this)},o.prototype.cleanup=function(){},o.prototype.mutate=function(t){var r=this.getOptions(),i=r.mutation,s=r.variables,o=r.optimisticResponse,n=r.update,u=r.context,a=void 0===u?{}:u,p=r.awaitRefetchQueries,c=void 0!==p&&p,l=r.fetchPolicy,h=(0,e.__assign)({},t),b=Object.assign({},s,h.variables);return delete h.variables,this.refreshClient().client.mutate((0,e.__assign)({mutation:i,optimisticResponse:o,refetchQueries:h.refetchQueries||this.getOptions().refetchQueries,awaitRefetchQueries:c,update:n,context:a,fetchPolicy:l,variables:b},h))},o.prototype.onMutationStart=function(){this.result.loading||this.getOptions().ignoreResults||this.updateResult({loading:!0,error:void 0,data:void 0,called:!0})},o.prototype.onMutationCompleted=function(t,e){var r=this.getOptions(),s=r.onCompleted,o=r.ignoreResults,n=t.data,u=t.errors,a=u&&u.length>0?new i.ApolloError({graphQLErrors:u}):void 0;this.isMostRecentMutation(e)&&!o&&this.updateResult({called:!0,loading:!1,data:n,error:a}),s&&s(n)},o.prototype.onMutationError=function(t,e){var r=this.getOptions().onError;this.isMostRecentMutation(e)&&this.updateResult({loading:!1,error:t,data:void 0,called:!0}),r&&r(t)},o.prototype.generateNewMutationId=function(){return++this.mostRecentMutationId},o.prototype.isMostRecentMutation=function(t){return this.mostRecentMutationId===t},o.prototype.updateResult=function(t){!this.isMounted||this.previousResult&&(0,s.equal)(this.previousResult,t)||(this.setResult(t),this.previousResult=t)},o}(n);var c=function(t){function r(e){var r=e.options,i=e.context,s=e.setResult,o=t.call(this,r,i)||this;return o.currentObservable={},o.setResult=s,o.initialize(r),o}return(0,e.__extends)(r,t),r.prototype.execute=function(t){var r=t;this.refreshClient().isNew&&(r=this.getLoadingResult());var i=this.getOptions().shouldResubscribe;return"function"==typeof i&&(i=!!i(this.getOptions())),!1!==i&&this.previousOptions&&Object.keys(this.previousOptions).length>0&&(this.previousOptions.subscription!==this.getOptions().subscription||!(0,s.equal)(this.previousOptions.variables,this.getOptions().variables))&&(this.endSubscription(),delete this.currentObservable.query,r=this.getLoadingResult()),this.initialize(this.getOptions()),this.startSubscription(),this.previousOptions=this.getOptions(),(0,e.__assign)({},r,{variables:this.getOptions().variables})},r.prototype.afterExecute=function(){this.isMounted=!0},r.prototype.cleanup=function(){this.endSubscription(),delete this.currentObservable.query},r.prototype.initialize=function(t){this.currentObservable.query||(this.currentObservable.query=this.refreshClient().client.subscribe({query:t.subscription,variables:t.variables,fetchPolicy:t.fetchPolicy}))},r.prototype.startSubscription=function(){this.currentObservable.subscription||(this.currentObservable.subscription=this.currentObservable.query.subscribe({next:this.updateCurrentData.bind(this),error:this.updateError.bind(this),complete:this.completeSubscription.bind(this)}))},r.prototype.getLoadingResult=function(){return{loading:!0,error:void 0,data:void 0}},r.prototype.updateResult=function(t){this.isMounted&&this.setResult(t)},r.prototype.updateCurrentData=function(t){var e=this.getOptions().onSubscriptionData;this.updateResult({data:t.data,loading:!1,error:void 0}),e&&e({client:this.refreshClient().client,subscriptionData:t})},r.prototype.updateError=function(t){this.updateResult({error:t,loading:!1})},r.prototype.completeSubscription=function(){var t=this.getOptions().onSubscriptionComplete;t&&t(),this.endSubscription()},r.prototype.endSubscription=function(){this.currentObservable.subscription&&(this.currentObservable.subscription.unsubscribe(),delete this.currentObservable.subscription)},r}(n);var l=function(){function t(){this.queryPromises=new Map,this.queryInfoTrie=new Map}return t.prototype.registerSSRObservable=function(t,e){this.lookupQueryInfo(e).observable=t},t.prototype.getSSRObservable=function(t){return this.lookupQueryInfo(t).observable},t.prototype.addQueryPromise=function(t,e){return this.lookupQueryInfo(t.getOptions()).seen?e():(this.queryPromises.set(t.getOptions(),new Promise(function(e){e(t.fetchData())})),null)},t.prototype.hasPromises=function(){return this.queryPromises.size>0},t.prototype.consumeAndAwaitPromises=function(){var t=this,e=[];return this.queryPromises.forEach(function(r,i){t.lookupQueryInfo(i).seen=!0,e.push(r)}),this.queryPromises.clear(),Promise.all(e)},t.prototype.lookupQueryInfo=function(t){var e=this.queryInfoTrie,r=t.query,i=t.variables,s=e.get(r)||new Map;e.has(r)||e.set(r,s);var o=JSON.stringify(i),n=s.get(o)||{seen:!1,observable:null};return s.has(o)||s.set(o,n),n},t}();exports.RenderPromises=l; |
@@ -153,3 +153,3 @@ import { parser, operationName, DocumentType, getApolloContext } from '@apollo/react-common'; | ||
called: true, | ||
data: {} | ||
data: undefined | ||
}; | ||
@@ -213,3 +213,3 @@ if (this.context && this.context.renderPromises) { | ||
_this.previousData.result.networkStatus === networkStatus && | ||
equal(_this.previousData.result.data, data || {})) { | ||
equal(_this.previousData.result.data, data)) { | ||
return; | ||
@@ -242,6 +242,3 @@ } | ||
QueryData.prototype.getQueryResult = function () { | ||
var result = { | ||
data: Object.create(null) | ||
}; | ||
Object.assign(result, this.observableQueryFields(this.currentObservable.query)); | ||
var result = __assign({}, this.observableQueryFields(this.currentObservable.query)); | ||
if (this.getOptions().skip) { | ||
@@ -254,12 +251,13 @@ result = __assign({}, result, { data: undefined, error: undefined, loading: false, called: true }); | ||
var error = currentResult.error, data = currentResult.data; | ||
data = data || Object.create(null); | ||
if (errors && errors.length > 0) { | ||
error = new ApolloError({ graphQLErrors: errors }); | ||
} | ||
Object.assign(result, { loading: loading, networkStatus: networkStatus, error: error, called: true }); | ||
result = __assign({}, result, { loading: loading, | ||
networkStatus: networkStatus, | ||
error: error, called: true }); | ||
if (loading) { | ||
var previousData = this.previousData.result | ||
? this.previousData.result.data | ||
: {}; | ||
Object.assign(result.data, previousData, data); | ||
var previousData = this.previousData.result && this.previousData.result.data; | ||
result.data = | ||
previousData && data | ||
? __assign({}, previousData, data) : previousData || data; | ||
} | ||
@@ -276,3 +274,3 @@ else if (error) { | ||
if (partialRefetch && | ||
Object.keys(data).length === 0 && | ||
!data && | ||
partial && | ||
@@ -287,3 +285,3 @@ fetchPolicy !== 'cache-only') { | ||
} | ||
Object.assign(result.data, data); | ||
result.data = data; | ||
} | ||
@@ -290,0 +288,0 @@ } |
@@ -219,3 +219,3 @@ (function (global, factory) { | ||
called: true, | ||
data: {} | ||
data: undefined | ||
}; | ||
@@ -300,3 +300,3 @@ | ||
if (_this.previousData.result && _this.previousData.result.loading === loading && _this.previousData.result.networkStatus === networkStatus && (0, _equality.equal)(_this.previousData.result.data, data || {})) { | ||
if (_this.previousData.result && _this.previousData.result.loading === loading && _this.previousData.result.networkStatus === networkStatus && (0, _equality.equal)(_this.previousData.result.data, data)) { | ||
return; | ||
@@ -334,6 +334,3 @@ } | ||
QueryData.prototype.getQueryResult = function () { | ||
var result = { | ||
data: Object.create(null) | ||
}; | ||
Object.assign(result, this.observableQueryFields(this.currentObservable.query)); | ||
var result = (0, _tslib.__assign)({}, this.observableQueryFields(this.currentObservable.query)); | ||
@@ -355,3 +352,2 @@ if (this.getOptions().skip) { | ||
data = currentResult.data; | ||
data = data || Object.create(null); | ||
@@ -364,3 +360,3 @@ if (errors && errors.length > 0) { | ||
Object.assign(result, { | ||
result = (0, _tslib.__assign)({}, result, { | ||
loading: loading, | ||
@@ -373,4 +369,4 @@ networkStatus: networkStatus, | ||
if (loading) { | ||
var previousData = this.previousData.result ? this.previousData.result.data : {}; | ||
Object.assign(result.data, previousData, data); | ||
var previousData = this.previousData.result && this.previousData.result.data; | ||
result.data = previousData && data ? (0, _tslib.__assign)({}, previousData, data) : previousData || data; | ||
} else if (error) { | ||
@@ -384,3 +380,3 @@ Object.assign(result, { | ||
if (partialRefetch && Object.keys(data).length === 0 && partial && fetchPolicy !== 'cache-only') { | ||
if (partialRefetch && !data && partial && fetchPolicy !== 'cache-only') { | ||
Object.assign(result, { | ||
@@ -394,3 +390,3 @@ loading: true, | ||
Object.assign(result.data, data); | ||
result.data = data; | ||
} | ||
@@ -397,0 +393,0 @@ } |
{ | ||
"name": "@apollo/react-hooks", | ||
"description": "React Apollo Hooks.", | ||
"version": "3.0.1", | ||
"version": "3.1.0-beta.0", | ||
"author": "opensource@apollographql.com", | ||
@@ -32,3 +32,3 @@ "keywords": [ | ||
"predeploy": "npm run build", | ||
"deploy": "npm publish", | ||
"deploy": "npm publish --tag beta", | ||
"test": "npx jest --config ../../config/jest.config.js --testPathPattern packages/hooks", | ||
@@ -47,3 +47,3 @@ "test:watch": "npx jest --config ../../config/jest.config.js --testPathPattern packages/hooks --watch", | ||
"dependencies": { | ||
"@apollo/react-common": "^3.0.1", | ||
"@apollo/react-common": "^3.1.0-beta.0", | ||
"@wry/equality": "^0.1.9", | ||
@@ -60,3 +60,3 @@ "ts-invariant": "^0.4.4", | ||
"devDependencies": { | ||
"jest": "24.8.0", | ||
"jest": "24.9.0", | ||
"rollup": "1.19.4", | ||
@@ -66,3 +66,3 @@ "tsc-watch": "2.4.0", | ||
}, | ||
"gitHead": "ec4208c20750a8eb605bda2f9f0ffb44fb4723b0" | ||
"gitHead": "241ccacb9e27ab9cda90aa4a5c1f7ee29c05391b" | ||
} |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
347218
3191
3