This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
vue-relay.min.js
1 lines (1 loc) · 20.4 KB
/
vue-relay.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e["vue-relay"]=t()}(this,function(){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},n=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),r=function(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e},a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},i=require("fbjs/lib/invariant"),o=function(){function e(){t(this,e),this._selectionReferences=[]}return n(e,[{key:"lookupInStore",value:function(e,t){return e.check(t.root)?(this._retainCachedOperation(e,t),e.lookup(t.fragment)):null}},{key:"execute",value:function(e){var t=this,n=e.environment,r=e.operation,a=e.cacheConfig,i=e.preservePreviousReferences,o=void 0!==i&&i,s=n.unstable_internal.createOperationSelector,l=[];return n.execute({operation:r,cacheConfig:a}).map(function(e){var t=s(r.node,e.variables,e.operation);return l.push(n.retain(t.root)),e}).do({error:function(){t._selectionReferences=t._selectionReferences.concat(l)},complete:function(){o||t._disposeSelectionReferences(),t._selectionReferences=t._selectionReferences.concat(l)},unsubscribe:function(){t._selectionReferences=t._selectionReferences.concat(l)}})}},{key:"fetch",value:function(e){var t=this,n=e.cacheConfig,r=e.environment,a=e.onDataChange,i=e.operation,o=!1,s=void 0;this._disposeRequest(),this._fetchOptions=e;var l=this.execute({environment:r,operation:i,cacheConfig:n}).finally(function(){t._pendingRequest=null}).subscribe({next:function(){t._onQueryDataAvailable({notifyFirstResult:o})},error:function(e){o?a({error:e}):s=e}});if(this._pendingRequest={dispose:function(){l.unsubscribe()}},o=!0,s)throw s;return this._snapshot}},{key:"retry",value:function(){return i(this._fetchOptions,"RelayQueryFetcher: `retry` should be called after having called `fetch`"),this.fetch(this._fetchOptions)}},{key:"dispose",value:function(){this._disposeRequest(),this._disposeSelectionReferences()}},{key:"_disposeRequest",value:function(){this._snapshot=null,this._pendingRequest&&this._pendingRequest.dispose(),this._rootSubscription&&(this._rootSubscription.dispose(),this._rootSubscription=null)}},{key:"_retainCachedOperation",value:function(e,t){this._disposeCacheSelectionReference(),this._cacheSelectionReference=e.retain(t.root)}},{key:"_disposeCacheSelectionReference",value:function(){this._cacheSelectionReference&&this._cacheSelectionReference.dispose(),this._cacheSelectionReference=null}},{key:"_disposeSelectionReferences",value:function(){this._disposeCacheSelectionReference(),this._selectionReferences.forEach(function(e){return e.dispose()}),this._selectionReferences=[]}},{key:"_onQueryDataAvailable",value:function(e){var t=e.notifyFirstResult;i(this._fetchOptions,"RelayQueryFetcher: `_onQueryDataAvailable` should have been called after having called `fetch`");var n=this._fetchOptions,r=n.environment,a=n.onDataChange,o=n.operation;this._snapshot||(this._snapshot=r.lookup(o.fragment),this._rootSubscription=r.subscribe(this._snapshot,function(e){return a({snapshot:e})}),this._snapshot&&t&&a({snapshot:this._snapshot}))}}]),e}(),s=require("fbjs/lib/areEqual"),l=function(e,t,n,r){return{error:e,props:t?t.data:null,retry:function(){var t=n.retry();t?r.handleDataChange({snapshot:t}):e&&r.handleRetryAfterError(e)}}},c=function(e,t,n){var r=e.environment,a=e.query,i=e.variables;if(!a)return t.dispose(),{relayContextEnvironment:r,relayContextVariables:i,renderProps:{error:null,props:{},retry:null}};var o=r,s=o.unstable_internal,c=(0,s.createOperationSelector)((0,s.getRequest)(a),i);try{var u="STORE_THEN_NETWORK"===e.dataFrom?t.lookupInStore(o,c):null,h=t.fetch({cacheConfig:e.cacheConfig,dataFrom:e.dataFrom,environment:o,onDataChange:n.handleDataChange,operation:c})||u;return h?{relayContextEnvironment:r,relayContextVariables:c.variables,renderProps:l(null,h,t,n)}:{relayContextEnvironment:r,relayContextVariables:c.variables,renderProps:{error:null,props:null,retry:null}}}catch(e){return{relayContextEnvironment:r,relayContextVariables:c.variables,renderProps:l(e,null,t,n)}}},u={cacheConfig:{},dataFrom:{},environment:{required:!0},query:{},variables:{type:Object,default:function(){return{}}}},h={name:"relay-query-renderer",props:u,data:function(){var e=this,t={handleDataChange:function(r){var a=r.error,i=r.snapshot;e.setState({renderProps:l(a,i,n,t)})},handleRetryAfterError:function(t){e.setState({renderProps:{error:null,props:null,retry:null}})}},n=new o,r=c(this.$props,n,t);return{context:Object.freeze({relay:{environment:r.relayContextEnvironment,variables:r.relayContextVariables}}),state:Object.freeze(a({prevPropsEnvironment:this.environment,prevPropsVariables:this.variables,prevQuery:this.query,queryFetcher:n,retryCallbacks:t},r))}},methods:{setState:function(e){this.state=Object.freeze(a({},this.state,e))}},watch:a({},Object.keys(u).map(function(e){return r({},e,function(){if(this.state.prevQuery!==this.query||this.state.prevPropsEnvironment!==this.environment||!s(this.state.prevPropsVariables,this.variables)){var e=c(this.$props,this.state.queryFetcher,this.state.retryCallbacks);this.context.relay.environment=e.relayContextEnvironment,this.context.relay.variables=e.relayContextVariables,this.setState(a({prevQuery:this.query,prevPropsEnvironment:this.environment,prevPropsVariables:this.variables},e))}})})),render:function(e){return e(this.component)},created:function(){var e=this;this.component={name:"relay-context-provider",provide:{relay:this.context.relay},render:function(t){return t("keep-alive",{props:{include:[]}},e.$scopedSlots.default(e.state.renderProps))}}},beforeDestroy:function(){this.state.queryFetcher.dispose()}},p=function(t){return"object"===(void 0===t?"undefined":e(t))&&null!==t&&!Array.isArray(t)&&("object"===(void 0===(r=t.environment)?"undefined":e(r))&&null!==r&&"function"==typeof r.applyMutation&&"function"==typeof r.check&&"function"==typeof r.check&&"function"==typeof r.lookup&&"function"==typeof r.retain&&"function"==typeof r.sendMutation&&"function"==typeof r.sendQuery&&"function"==typeof r.execute&&"function"==typeof r.subscribe)&&("object"===(void 0===(n=t.variables)?"undefined":e(n))&&null!==n&&!Array.isArray(n));var n,r},f=require("fbjs/lib/invariant"),v=require("fbjs/lib/mapObject"),b=function(e,t,n){return{name:"relay-context-consumer",inject:["relay"],render:function(e){return e(this.component,{props:this.$attrs})},created:function(){var r=function(e){return f(p(e),"RelayContextConsumer: Expected `relayContext` to be an object conforming to the `RelayContext` interface, got `%s`.",e),e}(this.relay).environment.unstable_internal.getFragment,i=v(t,r),o=this;this.component={extends:n.call(this,i),props:Object.keys(i),render:function(e){return this.context?e(this.component):this.component.render(e)},created:function(){var t=this;this.component={name:"relay-context-provider",provide:{relay:(this.context||o).relay},render:function(n){return null!=e?n(e,{props:a({},t.$attrs,t.state.data,{relay:t.state.relayProp})}):n("keep-alive",{props:{include:[]}},o.$scopedSlots.default(a({},t.state.data,{relay:t.state.relayProp})))}}}}}}},y=require("fbjs/lib/areEqual"),d=require("fbjs/lib/invariant"),m=require("relay-runtime").Observable,g=require("fbjs/lib/areEqual"),_=require("fbjs/lib/invariant"),S=require("fbjs/lib/warning"),R=require("relay-runtime"),C=R.ConnectionInterface,P=R.Observable,F=function(e){return"function"==typeof e?{error:e,complete:e,unsubscribe:function(t){"function"==typeof e&&e()}}:e||{}},x=function(t,n){var i=this.relay,s=function(e){var t=null,n=!1;for(var r in e){var i=e[r],o=i.metadata&&i.metadata.connection;void 0!==i.metadata&&(n=!0),o&&(_(1===o.length,"RelayPaginationContainer: Only a single @connection is supported, `%s` has %s.",r,o.length),_(!t,"RelayPaginationContainer: Only a single fragment with @connection is supported."),t=a({},o[0],{fragmentName:r}))}return _(!n||null!==t,"RelayPaginationContainer: A @connection directive must be present."),t||{}}(t),l=n.getConnectionFromProps||function(t){var n=t.path;return _(n,"RelayPaginationContainer: Unable to synthesize a getConnectionFromProps function."),function(r){for(var a=r[t.fragmentName],i=0;i<n.length;i++){if(!a||"object"!==(void 0===a?"undefined":e(a)))return null;a=a[n[i]]}return a}}(s),c=n.direction||s.direction;_(c,"RelayPaginationContainer: Unable to infer direction of the connection, possibly because both first and last are provided.");var u=n.getFragmentVariables||function(e){var t=e.count;return _(t,"RelayPaginationContainer: Unable to synthesize a getFragmentVariables function."),function(e,n){return a({},e,r({},t,n))}}(s);return{name:"relay-pagination-container",data:function(){var e=(0,i.environment.unstable_internal.createFragmentSpecResolver)(i,this.$options.name,t,this.$props,this._handleFragmentDataUpdate);return{context:Object.freeze({relay:{environment:i.environment,variables:i.variables}}),state:Object.freeze({data:e.resolve(),prevProps:this.$props,relayEnvironment:i.environment,relayVariables:i.variables,relayProp:this._buildRelayProp(i),isARequestInFlight:!1,localVariables:null,refetchSubscription:null,resolver:e})}},methods:{setState:function(e){this.state=Object.freeze(a({},this.state,e))},_buildRelayProp:function(e){return{hasMore:this._hasMore,isLoading:this._isLoading,loadMore:this._loadMore,refetchConnection:this._refetchConnection,environment:e.environment}},_handleFragmentDataUpdate:function(){this.setState({data:this.state.resolver.resolve()})},_getConnectionData:function(){var t=a({},this.$props,this.state.data),n=l(t);if(null==n)return null;var r=C.get(),i=r.EDGES,o=r.PAGE_INFO,s=r.HAS_NEXT_PAGE,u=r.HAS_PREV_PAGE,h=r.END_CURSOR,p=r.START_CURSOR;_("object"===(void 0===n?"undefined":e(n)),"RelayPaginationContainer: Expected `getConnectionFromProps()` in `%s`to return `null` or a plain object with %s and %s properties, got `%s`.",this.$options.name,i,o,n);var f=n[i],v=n[o];if(null==f||null==v)return null;_(Array.isArray(f),"RelayPaginationContainer: Expected `getConnectionFromProps()` in `%s`to return an object with %s: Array, got `%s`.",this.$options.name,i,f),_("object"===(void 0===v?"undefined":e(v)),"RelayPaginationContainer: Expected `getConnectionFromProps()` in `%s`to return an object with %s: Object, got `%s`.",this.$options.name,o,v);var b="forward"===c?v[s]:v[u],y="forward"===c?v[h]:v[p];return"boolean"!=typeof b||0!==f.length&&void 0===y?(S(!1,"RelayPaginationContainer: Cannot paginate without %s fields in `%s`. Be sure to fetch %s (got `%s`) and %s (got `%s`).",o,this.$options.name,"forward"===c?s:u,b,"forward"===c?h:p,y),null):{cursor:y,edgeCount:f.length,hasMore:b}},_hasMore:function(){var e=this._getConnectionData();return!!(e&&e.hasMore&&e.cursor)},_isLoading:function(){return!!this.state.refetchSubscription},_refetchConnection:function(e,t,n){var r={count:e,cursor:null,totalCount:e};return{dispose:this._fetchPage(r,F(t),{force:!0},n).unsubscribe}},_loadMore:function(e,t,n){var r=F(t),a=this._getConnectionData();if(!a)return P.create(function(e){return e.complete()}).subscribe(r),null;var i=a.edgeCount+e;if(n&&n.force)return this._refetchConnection(i,t);var o=C.get(),s=o.END_CURSOR,l=o.START_CURSOR,u=a.cursor;S(u,"RelayPaginationContainer: Cannot `loadMore` without valid `%s` (got `%s`)","forward"===c?s:l,u);var h={count:e,cursor:u,totalCount:i};return{dispose:this._fetchPage(h,r,n).unsubscribe}},_getQueryFetcher:function(){return this.state.queryFetcher||this.setState({queryFetcher:new o}),this.state.queryFetcher},_fetchPage:function(r,o,s,l){var c=this,h=i.environment,p=h.unstable_internal,f=p.createOperationSelector,v=p.getRequest,b=p.getVariablesFromObject,y=a({},this.$props,this.state.data),d=b(this.context.relay.variables,t,this.$props);d=a({},d,l);var m=n.getVariables(y,{count:r.count,cursor:r.cursor},d);_("object"===(void 0===m?"undefined":e(m))&&null!==m,"RelayPaginationContainer: Expected `getVariables()` to return an object, got `%s` in `%s`.",m,this.$options.name),m=a({},m,l),this.setState({localVariables:m});var S=s?{force:!!s.force}:void 0;S&&s&&s.rerunParamExperimental&&(S.rerunParamExperimental=s.rerunParamExperimental);var R=f(v(n.query),m);this.state.refetchSubscription&&this.state.refetchSubscription.unsubscribe();var C=function(){c.state.refetchSubscription===F&&(c.state.refetchSubscription.unsubscribe(),c.setState({refetchSubscription:null,isARequestInFlight:!1}))};this.setState({isARequestInFlight:!0});var F=this._getQueryFetcher().execute({environment:h,operation:R,cacheConfig:S,preservePreviousReferences:!0}).mergeMap(function(e){return P.create(function(e){!function(e,t){c.context.relay.environment=i.environment,c.context.relay.variables=a({},i.variables,d);var n=c.state.resolver.resolve();c.state.resolver.setVariables(u(d,r.totalCount));var o=c.state.resolver.resolve();g(n,o)||c.setState({data:o}),t()}(0,function(){e.next(),e.complete()})})}).do({error:C,complete:C,unsubscribe:C}).subscribe(o||{});return this.setState({refetchSubscription:this.state.isARequestInFlight?F:null}),F},_release:function(){this.state.resolver.dispose(),this.state.refetchSubscription&&(this.state.refetchSubscription.unsubscribe(),this.setState({refetchSubscription:null,isARequestInFlight:!1})),this.state.queryFetcher&&this.state.queryFetcher.dispose()}},watch:a({},Object.keys(t).map(function(e){return r({},e,function(){var e=i.environment.unstable_internal,n=e.createFragmentSpecResolver,r=e.getDataIDsFromObject,a=r(t,this.state.prevProps),o=r(t,this.$props);if(this.state.relayEnvironment===i.environment&&this.state.relayVariables===i.variables&&g(a,o))this.state.localVariables||this.state.resolver.setProps(this.$props);else{this._release(),this.context.relay.environment=i.environment,this.context.relay.variables=i.variables;var s=n(i,this.$options.name,t,this.$props,this._handleFragmentDataUpdate);this.setState({prevProps:this.$props,relayEnvironment:i.environment,relayVariables:i.variables,relayProp:this._buildRelayProp(i),localVariables:null,resolver:s})}var l=this.state.resolver.resolve();l!==this.state.data&&this.setState({data:l})})})),beforeDestroy:function(){this._release()}}},q=require("fbjs/lib/areEqual"),j=require("fbjs/lib/invariant"),O=function(e){var t=this.relay;return{name:"relay-fragment-container",data:function(){var n=(0,t.environment.unstable_internal.createFragmentSpecResolver)(t,this.$options.name,e,this.$props,this._handleFragmentDataUpdate);return{state:Object.freeze({data:n.resolve(),prevProps:this.$props,relayEnvironment:t.environment,relayVariables:t.variables,relayProp:{isLoading:n.isLoading(),environment:t.environment},resolver:n})}},methods:{setState:function(e){this.state=Object.freeze(a({},this.state,e))},_handleFragmentDataUpdate:function(){this.setState({data:this.state.resolver.resolve(),relayProp:{isLoading:this.state.resolver.isLoading(),environment:this.state.relayProp.environment}})}},watch:a({},Object.keys(e).map(function(n){return r({},n,function(){var n=t.environment.unstable_internal,r=n.createFragmentSpecResolver,a=n.getDataIDsFromObject,i=a(e,this.state.prevProps),o=a(e,this.$props),s=this.state.resolver;if(this.state.relayEnvironment===t.environment&&this.state.relayVariables===t.variables&&q(i,o)){s.setProps(this.$props);var l=s.resolve();l!==this.state.data&&this.setState({data:l,prevProps:this.$props,relayEnvironment:t.environment,relayVariables:t.variables,relayProp:{isLoading:s.isLoading(),environment:t.environment}})}else s.dispose(),s=r(t,this.$options.name,e,this.$props,this._handleFragmentDataUpdate),this.setState({data:s.resolve(),prevProps:this.$props,relayEnvironment:t.environment,relayVariables:t.variables,relayProp:{isLoading:s.isLoading(),environment:t.environment},resolver:s})})})),beforeDestroy:function(){this.state.resolver.dispose()}}},E=require("relay-runtime");return{QueryRenderer:h,MutationTypes:E.MutationTypes,RangeOperations:E.RangeOperations,commitLocalUpdate:E.commitLocalUpdate,commitMutation:E.commitMutation,createRefetchContainer:function(){d(2===arguments.length||3===arguments.length,"createRefetchContainer: Expected `arguments.length` to be 2 or 3, got `%s`.",arguments),2===arguments.length&&[].unshift.call(arguments,null);var e=Array.prototype.slice.call(arguments),t=e[0],n=e[1],i=e[2];return b(t,n,function(e){return function(e,t){var n=this.relay;return{name:"relay-refetch-container",data:function(){var t=(0,n.environment.unstable_internal.createFragmentSpecResolver)(n,this.$options.name,e,this.$props,this._handleFragmentDataUpdate);return{context:Object.freeze({relay:{environment:n.environment,variables:n.variables}}),state:Object.freeze({data:t.resolve(),prevProps:this.$props,relayEnvironment:n.environment,relayVariables:n.variables,relayProp:this._buildRelayProp(n),localVariables:null,refetchSubscription:null,resolver:t})}},methods:{setState:function(e){this.state=Object.freeze(a({},this.state,e))},_buildRelayProp:function(e){return{environment:e.environment,refetch:this._refetch}},_handleFragmentDataUpdate:function(){this.setState({data:this.state.resolver.resolve()})},_getFragmentVariables:function(){return(0,n.environment.unstable_internal.getVariablesFromObject)(n.variables,e,this.$props)},_getQueryFetcher:function(){return this.state.queryFetcher||this.setState({queryFetcher:new o}),this.state.queryFetcher},_refetch:function(e,r,i,o){var s=this,l=n.environment,c=n.variables,u=l.unstable_internal,h=u.createOperationSelector,p=u.getRequest,f="function"==typeof e?e(this._getFragmentVariables()):e;f=a({},c,f);var v=r?a({},c,r):f;this.setState({localVariables:f});var b=o?{force:!!o.force}:void 0,y="function"==typeof i?{next:i,error:i}:i||{},d=h(p(t),f);this.state.refetchSubscription&&this.state.refetchSubscription.unsubscribe();var g=void 0;return this._getQueryFetcher().execute({environment:l,operation:d,cacheConfig:b,preservePreviousReferences:!0}).mergeMap(function(e){return s.context.relay.environment=n.environment,s.context.relay.variables=v,s.state.resolver.setVariables(v),m.create(function(e){s.setState({data:s.state.resolver.resolve()}),e.next(),e.complete()})}).finally(function(){s.state.refetchSubscription===g&&(s.state.refetchSubscription.unsubscribe(),s.setState({refetchSubscription:null}))}).subscribe(a({},y,{start:function(e){g=e,s.setState({refetchSubscription:e}),y.start&&y.start(e)}})),{dispose:function(){g&&g.unsubscribe()}}},_release:function(){this.state.resolver.dispose(),this.state.references.forEach(function(e){return e.dispose()}),this.state.references.length=0,this.state.refetchSubscription&&(this.state.refetchSubscription.unsubscribe(),this.setState({refetchSubscription:null})),this.state.queryFetcher&&this.state.queryFetcher.dispose()}},watch:a({},Object.keys(e).map(function(t){return r({},t,function(){var t=n.environment.unstable_internal,r=t.createFragmentSpecResolver,a=t.getDataIDsFromObject,i=a(e,this.state.prevProps),o=a(e,this.$props);if(this.state.relayEnvironment===n.environment&&this.state.relayVariables===n.variables&&y(i,o))this.state.localVariables||this.state.resolver.setProps(this.$props);else{this._release(),this.context.relay.environment=n.environment,this.context.relay.variables=n.variables;var s=r(n,this.$options.name,e,this.$props,this._handleFragmentDataUpdate);this.setState({prevProps:this.$props,relayEnvironment:n.environment,relayVariables:n.variables,relayProp:this._buildRelayProp(n),localVariables:null,resolver:s})}var l=this.state.resolver.resolve();l!==this.state.data&&this.setState({data:l})})})),beforeDestroy:function(){this._release()}}}.call(this,e,i)})},createPaginationContainer:function(){_(2===arguments.length||3===arguments.length,"createPaginationContainer: Expected `arguments.length` to be 2 or 3, got `%s`.",arguments),2===arguments.length&&[].unshift.call(arguments,null);var e=Array.prototype.slice.call(arguments),t=e[0],n=e[1],r=e[2];return b(t,n,function(e){return x.call(this,e,r)})},createFragmentContainer:function(){j(1===arguments.length||2===arguments.length,"createFragmentContainer: Expected `arguments.length` to be 1 or 2, got `%s`.",arguments),1===arguments.length&&[].unshift.call(arguments,null);var e=Array.prototype.slice.call(arguments),t=e[0],n=e[1];return b(t,n,O)},fetchQuery:E.fetchQuery,graphql:E.graphql,requestSubscription:E.requestSubscription}});