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.96 kB
JavaScript
!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.React,e.PropTypes)}(this,function(e,t,r){"use strict";var n="default"in t?t.default:t;r=r&&r.hasOwnProperty("default")?r.default:r;var o=function(e){return function(){var t=e.apply(this,arguments);return new Promise(function(e,r){function n(o,i){try{var s=t[o](i),a=s.value}catch(e){return void r(e)}if(!s.done)return Promise.resolve(a).then(function(e){n("next",e)},function(e){n("throw",e)});e(a)}return n("next")})}},i=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},s=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},a=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)},c=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},u=function(){var e=o(regeneratorRuntime.mark(function e(r){var n,o,i,a,c,p,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=s({},n.defaultProps,r.props),i=l,a=void 0,!n.prototype||!n.prototype.isReactComponent){e.next=21;break}if(c=new n(o,l),c.props=c.props||o,c.context=c.context||l,c.setState=function(e,t){c.state="function"==typeof e?s({},c.state,e(c.state)):s({},c.state,e),"function"==typeof t&&t()},c.componentWillMount&&c.componentWillMount(),c.getChildContext&&(i=s({},l,c.getChildContext())),r.type!==f){e.next=17;break}return e.next=17,Promise.all(c.getObservers().map(function(e){return e.stopPolling(),new Promise(function(t,r){try{e.result().then(function(){return t()},function(){return t()})}catch(e){r(e)}})}));case 17:a=c.render(),r.type===f&&c.componentWillUnmount(),e.next=22;break;case 21:a=n(o,l);case 22:return e.next=24,u(a,i);case 24:e.next=31;break;case 26:if(!r.props||!r.props.children){e.next=31;break}return p=[],t.Children.forEach(r.props.children,function(e){e&&p.push(u(e,l))}),e.next=31,Promise.all(p);case 31:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}(),f=function(e){function t(r,n){i(this,t);var o=c(this,e.call(this,r,n));p.call(o);var s=o.props,a=s.fetchers,u=void 0===a?{}:a,f=s.fragments,l=void 0===f?{}:f,h=s.mutations,b=void 0===h?{}:h,v=s.queries,d=void 0===v?{}:v,y=(s.render,o.getClient());return Object.keys(d).forEach(function(e){var t={hasVariablesChanged:function(){return!1}},n=d[e](y,r,{hasVariablesChanged:function(r){if("function"!=typeof r)throw new Error("hasVariablesChanged for query "+e+" must be a function");t.hasVariablesChanged=r}});o.observers[e]={options:t,observer:n};var i=n.subscribe({next:function(){return o.forceRerender()},error:function(){return o.forceRerender()}});o.subscriptions[e]=i}),o.initializeFetchers(u,o.props),o.initializeMutations(b,o.props),o.fetchFragments(l,o.props),o}return a(t,e),t.prototype.componentWillMount=function(){this.hasMount=!0},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.fetchers,t.render);this.getClient();return r(Object.keys(this.observers).reduce(function(t,r){var n;return s({},t,(n={},n[r]=s({observer:e.observers[r].observer},e.observers[r].observer.currentResult()),n))},{}),this.mutations,this.fetchers,this.fragments,this.props)},t}(n.Component);f.contextTypes={client:r.object};var p=function(){var e=this;this.fragments={},this.fragmentsProps={},this.fetchers={},this.hasMount=!1,this.mutations={},this.observers={},this.subscriptions={},this.componentWillReceiveProps=function(t){Object.keys(e.observers).forEach(function(r){var n=e.observers[r].options.hasVariablesChanged(e.props,t);if(!1!==n){if(!0===n)throw new Error("hasVariablesChanged can't return true");e.observers[r].observer.setVariables(n)}}),e.initializeMutations(t.mutations,t),e.initializeFetchers(t.fetchers,t),e.fetchFragments(t.fragments,t)},this.getClient=function(){var t=e.context.client,r=e.props.client;if(t)return t;if(r)return r;throw new Error("Please specify apollo client using client prop or provide it using <ApolloProvider />.")},this.getObservers=function(){return Object.keys(e.observers).map(function(t){return e.observers[t].observer})},this.fetchFragments=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=arguments[1],n=e.getClient();e.fragments=Object.keys(t).reduce(function(o,i){var a,c=e.fragmentsProps[i],u=e.fragments[i],f=null;try{!1===(f=t[i](n,c,r))&&(f=u)}catch(e){"production"!==process.env.NODE_ENV&&console.error(e)}return s({},o,(a={},a[i]=f,a))},{})},this.initializeFetchers=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=arguments[1],n=e.getClient();e.fetchers=Object.keys(t).reduce(function(e,o){var i;return s({},e,(i={},i[o]=t[o](n,r),i))},{})},this.initializeMutations=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=arguments[1],n=e.getClient();e.mutations=Object.keys(t).reduce(function(e,o){var i;return s({},e,(i={},i[o]=t[o](n,r),i))},{})},this.forceRerender=function(){e.hasMount&&e.setState({})}};e.default=f,e.getDataFromTree=u,Object.defineProperty(e,"__esModule",{value:!0})});