/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow strict-local * @format * @oncall relay */ 'use strict'; import type {Options} from './useRefetchableFragmentInternal'; import type { Disposable, FragmentType, RefetchableFragment, Variables, } from 'relay-runtime'; const useRefetchableFragmentInternal = require('./useRefetchableFragmentInternal'); const useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning'); const {useDebugValue} = require('react'); const {getFragment} = require('relay-runtime'); type RefetchVariables = // NOTE: This type ensures that the type of the returned variables is either: // - nullable if the provided ref type is nullable // - non-nullable if the provided ref type is non-nullable [+key: TKey] extends [+key: {+$fragmentSpreads: mixed, ...}] ? Partial : TVariables; type RefetchFnBase = ( vars: TVars, options?: TOptions, ) => Disposable; export type RefetchFn = RefetchFnBase< RefetchVariables, TOptions, >; export type ReturnType< TVariables, TData, TKey: ?{+$fragmentSpreads: mixed, ...}, > = [ // NOTE: This type ensures that the type of the returned data is either: // - nullable if the provided ref type is nullable // - non-nullable if the provided ref type is non-nullable [+key: TKey] extends [+key: {+$fragmentSpreads: mixed, ...}] ? TData : ?TData, RefetchFn, ]; export type UseRefetchableFragmentType = < TFragmentType: FragmentType, TVariables: Variables, TData, TKey: ?{+$fragmentSpreads: TFragmentType, ...}, >( fragment: RefetchableFragment, key: TKey, ) => ReturnType; hook useRefetchableFragment< TFragmentType: FragmentType, TVariables: Variables, TData, TKey: ?{+$fragmentSpreads: TFragmentType, ...}, >( fragmentInput: RefetchableFragment, fragmentRef: TKey, ): ReturnType { const fragmentNode = getFragment(fragmentInput); useStaticFragmentNodeWarning( fragmentNode, 'first argument of useRefetchableFragment()', ); const {fragmentData, refetch} = useRefetchableFragmentInternal< {variables: TVariables, response: TData}, {data?: TData}, >(fragmentNode, fragmentRef, 'useRefetchableFragment()'); if (__DEV__) { // eslint-disable-next-line react-hooks/rules-of-hooks // $FlowFixMe[react-rule-hook] // $FlowFixMe[react-rule-hook-conditional] useDebugValue({fragment: fragmentNode.name, data: fragmentData}); } // $FlowFixMe[incompatible-return] // $FlowFixMe[prop-missing] return [fragmentData, refetch]; } module.exports = useRefetchableFragment;