@shopify/react-graphql
Version:
Tools for creating type-safe and asynchronous GraphQL components for React
30 lines (20 loc) • 1.15 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
var ApolloContext = require('../ApolloContext.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
function useApolloClient(overrideClient) {
const context = React__default['default'].useContext(ApolloContext.ApolloContext); // Ensures that the number of hooks called from one render to another remains
// constant, despite the Apollo client read from context being swapped for
// one passed directly as prop.
if (overrideClient) {
return overrideClient;
}
if (!context || !context.client) {
// https://github.com/apollographql/react-apollo/blob/5cb63b3625ce5e4a3d3e4ba132eaec2a38ef5d90/src/component-utils.tsx#L19-L22
throw new Error(['Could not find "client" in the context or passed in as a prop. ', 'Wrap the root component in an <ApolloProvider>, or pass an ', 'ApolloClient instance in via props.'].join());
}
return context.client;
}
exports.default = useApolloClient;