@airbnb/lunar-apollo
Version:
Apollo and GraphQL utilities.
61 lines (48 loc) • 2.28 kB
JavaScript
import _pt from "prop-types";
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from 'react';
import { Query as BaseQuery } from '@apollo/client/react/components';
import ErrorMessage from '@airbnb/lunar/lib/components/ErrorMessage';
import Loader from '@airbnb/lunar/lib/components/Loader';
import renderElementOrFunction from '@airbnb/lunar/lib/utils/renderElementOrFunction';
/**
* A declarative component to make GraphQL queries.
* Based on Apollo's [Query](https://www.apollographql.com/docs/react/essentials/queries.html#props) component.
*/
export default class Query extends React.Component {
constructor(...args) {
super(...args);
_defineProperty(this, "handleRender", result => {
if (result.loading) {
return renderElementOrFunction(this.props.loading) || /*#__PURE__*/React.createElement(Loader, {
static: true
});
}
if (result.error && (!this.props.ignoreGraphQLErrors || result.error.networkError)) {
return renderElementOrFunction(this.props.error, result.error) || /*#__PURE__*/React.createElement(ErrorMessage, {
error: result.error
});
}
return this.props.children(result.data || null, result);
});
}
render() {
const _this$props = this.props,
props = _objectWithoutPropertiesLoose(_this$props, ["children", "loading", "error"]); // @ts-ignore Prop spreading
return /*#__PURE__*/React.createElement(BaseQuery, props, this.handleRender);
}
}
_defineProperty(Query, "propTypes", {
children: _pt.func.isRequired,
ignoreGraphQLErrors: _pt.bool
});
_defineProperty(Query, "defaultProps", {
ignoreGraphQLErrors: false,
notifyOnNetworkStatusChange: false,
partialRefetch: false,
pollInterval: 0,
skip: false,
ssr: false,
variables: {}
});