UNPKG

@shopify/react-graphql

Version:

Tools for creating type-safe and asynchronous GraphQL components for React

70 lines (64 loc) 1.86 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var reactIdle = require('@shopify/react-idle'); var query = require('./query.js'); var query$1 = require('../hooks/query.js'); function createAsyncQueryComponent(options) { const asyncQuery = query.createAsyncQuery(options); const { resolver, usePreload, usePrefetch, useKeepFresh } = asyncQuery; function AsyncQuery(options) { const result = query$1["default"](asyncQuery, options); return options.children(result); } function Preload() { reactIdle.useIdleCallback(usePreload()); return null; } function Prefetch(options) { reactIdle.useIdleCallback(usePrefetch(options)); return null; } function KeepFresh(options) { reactIdle.useIdleCallback(useKeepFresh(options)); return null; } // Once we upgrade past TS 3.1, this will no longer be necessary, // because you can statically assign values to functions and TS // will know to augment its type const FinalComponent = AsyncQuery; Reflect.defineProperty(FinalComponent, 'resolver', { value: resolver, writable: false }); Reflect.defineProperty(FinalComponent, 'Preload', { value: Preload, writable: false }); Reflect.defineProperty(FinalComponent, 'Prefetch', { value: Prefetch, writable: false }); Reflect.defineProperty(FinalComponent, 'KeepFresh', { value: KeepFresh, writable: false }); Reflect.defineProperty(FinalComponent, 'usePreload', { value: usePreload, writable: false }); Reflect.defineProperty(FinalComponent, 'usePrefetch', { value: usePrefetch, writable: false }); Reflect.defineProperty(FinalComponent, 'useKeepFresh', { value: useKeepFresh, writable: false }); return FinalComponent; } exports.createAsyncQueryComponent = createAsyncQueryComponent;