UNPKG

react-apollo-graphql

Version:

Get rid of decorators and use Apollo GraphQL queries and mutations in the simple and readable way.

2 lines (1 loc) 5.02 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["exports","react","prop-types"],t):t(e.GraphQL=e.GraphQL||{},e.React,e.PropTypes)}(this,function(e,t,r){"use strict";var n="default"in t?t.default:t;r="default"in r?r.default:r;var o=function(e){return function(){var t=e.apply(this,arguments);return new Promise(function(e,r){function n(o,s){try{var i=t[o](s),u=i.value}catch(e){return void r(e)}if(!i.done)return Promise.resolve(u).then(function(e){n("next",e)},function(e){n("throw",e)});e(u)}return n("next")})}},s=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},u=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},a=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},c=function(){var e=o(regeneratorRuntime.mark(function e(r){var n,o,s,u,a,f,l=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(null!=r){e.next=2;break}return e.abrupt("return");case 2:if("function"!=typeof(n=r.type)){e.next=26;break}if(o=i({},n.defaultProps,r.props),s=l,u=void 0,!n.prototype||!n.prototype.isReactComponent){e.next=21;break}if(a=new n(o,l),a.props=a.props||o,a.context=a.context||l,a.setState=function(e,t){a.state="function"==typeof e?i({},a.state,e(a.state)):i({},a.state,e),"function"==typeof t&&t()},a.componentWillMount&&a.componentWillMount(),a.getChildContext&&(s=i({},l,a.getChildContext())),r.type!==p){e.next=17;break}return e.next=17,Promise.all(a.getObservers().map(function(e){return new Promise(function(t,r){try{e.result().then(function(){return t()},function(){return t()})}catch(e){r(e)}})}));case 17:u=a.render(),r.type===p&&a.componentWillUnmount(),e.next=22;break;case 21:u=n(o,l);case 22:return e.next=24,c(u,s);case 24:e.next=31;break;case 26:if(!r.props||!r.props.children){e.next=31;break}return f=[],t.Children.forEach(r.props.children,function(e){e&&f.push(c(e,l))}),e.next=31,Promise.all(f);case 31:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}(),p=(function(e){function t(){return s(this,t),a(this,e.apply(this,arguments))}u(t,e)}(Error),function(e){function t(){var r,n,o;s(this,t);for(var u=arguments.length,c=Array(u),p=0;p<u;p++)c[p]=arguments[p];return r=n=a(this,e.call.apply(e,[this].concat(c))),n.hasMount=!1,n.observers={},n.state={},n.subscriptions={},n.componentWillReceiveProps=function(e){Object.keys(n.observers).forEach(function(t){var r=n.observers[t].options.hasVariablesChanged(n.props,e);if(!1!==r){if(!0===r)throw new Error("hasVariablesChanged can't return true");n.observers[t].observer.setVariables(r)}})},n.getClient=function(){var e=n.context.client,t=n.props.client;if(e)return e;if(t)return t;throw new Error("Please specify apollo client using client prop or provide it using <ApolloProvider />.")},n.getObservers=function(){return Object.keys(n.observers).map(function(e){return n.observers[e].observer})},n.updateResults=function(e,t){n.hasMount&&n.setState(function(r){var n;return i({},r,(n={},n[e]=t,n))})},o=r,a(n,o)}return u(t,e),t.prototype.componentWillMount=function(){var e=this;this.hasMount=!0;var t=this.props,r=this.props,n=(r.mutations,r.queries),o=void 0===n?{}:n,s=(r.render,this.getClient()),u=Object.keys(o).reduce(function(r,n){var u,a={hasVariablesChanged:function(){return!1}},c=o[n](s,t,{hasVariablesChanged:function(e){if("function"!=typeof e)throw new Error("hasVariablesChanged for query "+n+" must be a function");a.hasVariablesChanged=e}});e.observers[n]={options:a,observer:c};var p=c.subscribe({next:function(){return e.updateResults(n,c.currentResult())},error:function(){return e.updateResults(n,c.currentResult())}});return e.subscriptions[n]=p,i({},r,(u={},u[n]=c.currentResult(),u))},{});this.setState(u)},t.prototype.componentWillUnmount=function(){var e=this;this.hasMount=!1,Object.keys(this.subscriptions).forEach(function(t){e.observers[t].observer.stopPolling(),e.subscriptions[t].unsubscribe()}),this.subscriptions={}},t.prototype.render=function(){var e=this,t=this.props,r=t.mutations,n=void 0===r?{}:r,o=t.render,s=this.getClient(),u=Object.keys(n).reduce(function(t,r){var o;return i({},t,(o={},o[r]=n[r](s,e.props),o))},{});return o(this.state,u,this.props)},t}(n.Component));p.contextTypes={client:r.object},e.default=p,e.getDataFromTree=c,Object.defineProperty(e,"__esModule",{value:!0})});