import type { GQtyClient } from 'gqty' ; import * as React from 'react' ; import { OnErrorHandler , useInterceptSelections } from '../common' ; import type { ReactClientOptionsWithDefaults } from '../utils' ; export interface GraphQLHOCOptions { suspense ? : boolean | { fallback : React . SuspenseProps [ 'fallback' ] ; } ; staleWhileRevalidate ? : boolean ; onError ? : OnErrorHandler ; } export interface GraphQLHOC { < P > ( component , options ? ) : ( props : P ) => React . ReactElement | null ; } export function createGraphqlHOC ( { scheduler , eventHandler , interceptorManager } , { defaults : { suspense : defaultSuspense , staleWhileRevalidate : defaultStaleWhileRevalidate , } , } ) { const graphql = function graphql < P > ( component , { suspense = defaultSuspense , staleWhileRevalidate = defaultStaleWhileRevalidate , onError , } = { } ) { const withGraphQL = function WithGraphQL ( props ) : React . ReactElement | null { const { fetchingPromise , unsubscribe } = useInterceptSelections ( { interceptorManager , eventHandler , scheduler , staleWhileRevalidate , onError , } ) ; let returnValue = null ; try { returnValue = component ( props ) ?? null ; } finally { unsubscribe ( ) ; } if ( suspense && fetchingPromise . current ) { function Suspend ( ) { if ( ! fetchingPromise . current ) return null ; throw fetchingPromise . current ; } const value = ( < > { returnValue } < Suspend / > < />); if ( typeof suspense === 'object' ) { return React . createElement ( React . Suspense , { fallback : suspense . fallback , children : value , } ) ; } return value ; } return returnValue ; } ; withGraphQL . displayName = `GraphQLComponent(${ component ?. displayName || component ?. name || 'Anonymous' })${ Date . now }` ; return withGraphQL ; } ; return graphql ; }