/** * 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 { Disposable, FragmentType, GraphQLTaggedNode, IEnvironment, Observer, Variables, } from 'relay-runtime'; export type GeneratedNodeMap = {[key: string]: GraphQLTaggedNode, ...}; export type ObserverOrCallback = Observer | ((error: ?Error) => mixed); // NOTE: This is an inexact type in order to allow a RelayPaginationProp or // RelayRefetchProp to flow into a RelayProp. export type RelayProp = {+environment: IEnvironment, ...}; export type RelayPaginationProp = { +environment: IEnvironment, +hasMore: () => boolean, +isLoading: () => boolean, +loadMore: ( pageSize: number, observerOrCallback: ?ObserverOrCallback, options?: RefetchOptions, ) => ?Disposable, +refetchConnection: ( totalCount: number, observerOrCallback: ?ObserverOrCallback, refetchVariables: ?Variables, ) => ?Disposable, }; export type RelayRefetchProp = { +environment: IEnvironment, +refetch: ( refetchVariables: Variables | ((fragmentVariables: Variables) => Variables), renderVariables: ?Variables, observerOrCallback: ?ObserverOrCallback, options?: RefetchOptions, ) => Disposable, }; export type RefetchOptions = { +force?: boolean, +fetchPolicy?: 'store-or-network' | 'network-only', +metadata?: {[key: string]: mixed, ...}, }; /** * A utility type which takes the type of a fragment's data (typically found in * a relay generated file) and returns a fragment reference type. This is used * when the input to a Relay component needs to be explicitly typed: * * // ExampleComponent.js * import type {ExampleComponent_data} from './generated/ExampleComponent_data.graphql'; * type Props = { * title: string, * data: ExampleComponent_data, * }; * * export default createFragmentContainer( * (props: Props) =>
{props.title}, {props.data.someField}
, * graphql` * fragment ExampleComponent_data on SomeType { * someField * }` * ); * * // ExampleUsage.js * import type {ExampleComponent_data} from './generated/ExampleComponent_data.graphql'; * type Props = { * title: string, * data: $FragmentRef, * }; * * export default function ExampleUsage(props: Props) { * return * } * */ export type $FragmentRef = { +$fragmentSpreads: T['$fragmentType'], ... }; /* $FlowExpectedError[unclear-type]: Intentional so that it won't fail, * even if the type we want to exclude doesn't exist in Props */ type LooseOmitRelayProps> = Pick< Props, Exclude<$Keys, K>, >; /** * A utility type that takes the Props of a component and the type of * `props.relay` and returns the props of the container. */ // prettier-ignore // $FlowFixMe[extra-type-arg] xplat redux flow type error export type $RelayProps = MapRelayProps< LooseOmitRelayProps, >; type MapRelayProps = {[K in keyof Props]: MapRelayProp}; type MapRelayProp = [+t: T] extends [+t: {+$fragmentType: empty, ...}] ? T : [+t: T] extends [+t: ?{+$fragmentType: empty, ...}] ? ?T : [+t: T] extends [+t: {+$fragmentType: FragmentType, ...}] ? $FragmentRef : [+t: T] extends [+t: ?{+$fragmentType: FragmentType, ...}] ? ?$FragmentRef<$NonMaybeType> : [+t: T] extends [ +t: $ReadOnlyArray< infer V extends {+$fragmentType: FragmentType, ...}, >, ] ? $ReadOnlyArray<$FragmentRef> : [+t: T] extends [ +t: ?$ReadOnlyArray< infer V extends {+$fragmentType: FragmentType, ...}, >, ] ? ?$ReadOnlyArray<$FragmentRef> : [+t: T] extends [ +t: $ReadOnlyArray, ] ? $ReadOnlyArray>> : [+t: T] extends [ +t: ?$ReadOnlyArray, ] ? ?$ReadOnlyArray>> : T; export type RelayFragmentContainer = component( ...$RelayProps, RelayProp> ); export type RelayPaginationContainer = component( ...$RelayProps, RelayPaginationProp> ); export type RelayRefetchContainer = component( ...$RelayProps, RelayRefetchProp> );