Skip to content

Commit

Permalink
Eliminate final $ObjMap in relay
Browse files Browse the repository at this point in the history
Reviewed By: panagosg7

Differential Revision: D62055702

fbshipit-source-id: 88166065114f30ff2dbf802e35b0a7ed5f2cfdaa
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Aug 31, 2024
1 parent fe4c38b commit c8e8ee1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
34 changes: 14 additions & 20 deletions packages/react-relay/relay-hooks/EntryPointTypes.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ export type PreloadProps<
+[K in keyof TPreloadedEntryPoints]?: ?ThinNestedEntryPointParams,
},
extraProps?: TExtraProps,
// $FlowFixMe[deprecated-type]
queries?: $ObjMap<
TPreloadedQueries,
ExtractQueryTypeHelper<TEnvironmentProviderOptions>,
>,
queries?: ExtractQueryTypes<TEnvironmentProviderOptions, TPreloadedQueries>,
}>;

// Return type of `loadEntryPoint(...)`
Expand Down Expand Up @@ -257,25 +253,23 @@ export type ExtractQueryTypeHelper<TEnvironmentProviderOptions> = <TQuery>(
PreloadedQuery<TQuery>,
) => ThinQueryParams<TQuery, TEnvironmentProviderOptions>;

type ExtractQueryType<
TEnvironmentProviderOptions,
// $FlowExpectedError[unclear-type] Need any to make it supertype of all PreloadedQuery
TPreloadedQuery: ?PreloadedQuery<any>,
> = TPreloadedQuery extends null | void
? TPreloadedQuery
: TPreloadedQuery extends PreloadedQuery<infer TQuery extends OperationType>
? ThinQueryParams<TQuery, TEnvironmentProviderOptions>
: empty;

export type ExtractQueryTypes<
TEnvironmentProviderOptions,
// $FlowExpectedError[unclear-type] Need any to make it supertype of all PreloadedQuery
PreloadedQueries: {+[string]: PreloadedQuery<any>},
PreloadedQueries: {+[string]: PreloadedQuery<any>} | void,
> = {
[K in keyof PreloadedQueries]: ExtractQueryType<
TEnvironmentProviderOptions,
PreloadedQueries[K],
>,
// We need to match both cases without using distributive conditional types,
// because PreloadedQuery's TQuery parameter is almost phantom, and breaking
// up the union type would cause us to lose track of TQuery.
[K in keyof PreloadedQueries]: PreloadedQueries[K] extends PreloadedQuery<
infer TQuery extends OperationType,
>
? ThinQueryParams<TQuery, TEnvironmentProviderOptions>
: PreloadedQueries[K] extends PreloadedQuery<
infer TQuery extends OperationType,
> | void
? ThinQueryParams<TQuery, TEnvironmentProviderOptions> | void
: empty,
};

export type EntryPoint<TEntryPointParams, +TEntryPointComponent> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ test('it should preload entry point with queries', () => {
{
getEnvironment: () => env,
},
// $FlowFixMe[incompatible-call]
entryPoint,
{id: 'my-id'},
);
Expand Down Expand Up @@ -260,6 +261,7 @@ test('with `getEnvironment` function', () => {
{
getEnvironment,
},
// $FlowFixMe[incompatible-call]
entryPoint,
{id: 'my-id'},
);
Expand Down

0 comments on commit c8e8ee1

Please sign in to comment.