Skip to content

Commit

Permalink
Alternate implementation of UnwrapFragmentRefs
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Oct 7, 2024
1 parent a0329e9 commit 47df71e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
48 changes: 26 additions & 22 deletions src/masking/internal/types.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import type { Prettify, UnionToIntersection } from "../../utilities/index.js";

export type UnwrapFragmentRefs<TData> =
// Leave TData alone if it is Record<string, any> and not a specific shape
string extends keyof NonNullable<TData> ? TData
: " $fragmentRefs" extends keyof NonNullable<TData> ?
TData extends { " $fragmentRefs"?: infer FragmentRefs extends object } ?
Prettify<
{
[K in keyof TData as K extends " $fragmentRefs" ? never
: K]: UnwrapFragmentRefs<TData[K]>;
} & CombineFragmentRefs<FragmentRefs>
>
: keyof NonNullable<TData> extends never ? TData
: //: TData extends Array<infer U> ? Array<UnwrapFragmentRefs<U>>
TData extends { " $fragmentRefs"?: object | null } ?
Combine<KeyTuples<TData>> extends infer Flattened ?
{ [K in keyof Flattened]: UnwrapFragmentRefs<Flattened[K]> }
: never
: TData extends object ? { [K in keyof TData]: UnwrapFragmentRefs<TData[K]> }
: TData;

type CombineFragmentRefs<FragmentRefs extends Record<string, any>> =
UnionToIntersection<
{
[K in keyof FragmentRefs]-?: UnwrapFragmentRefs<
RemoveFragmentName<FragmentRefs[K]>
>;
}[keyof FragmentRefs]
>;

export type RemoveMaskedMarker<T> = Omit<T, "__masked">;
// force distrubution when T is a union with | undefined
export type RemoveFragmentName<T> =
T extends any ? Omit<T, " $fragmentName"> : T;

type Values<T> = T extends object ? T[keyof T] : never;

type KeyTuples<V> =
V extends object ?
keyof V extends infer K ?
K extends keyof V ?
K extends " $fragmentRefs" ? KeyTuples<Values<V[K]>>
: K extends " $fragmentName" ? never
: [K, V[K], {} extends Pick<V, K> ? true : false]
: never
: never
: never;

type Combine<
Tuple extends [key: string | number | symbol, value: any, optional: boolean],
> = {
[P in Tuple as P[2] extends true ? P[0] : never]?: P[1];
} & {
[P in Tuple as P[2] extends false ? P[0] : never]: P[1];
};
5 changes: 1 addition & 4 deletions src/masking/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { TypedDocumentNode } from "@graphql-typed-document-node/core";
import type {
RemoveFragmentName,
RemoveMaskedMarker,
UnwrapFragmentRefs,
} from "./internal/types.ts";
Expand Down Expand Up @@ -45,6 +44,4 @@ export type MaybeMasked<TData> =
* Unmasks a type to provide its full result.
*/
export type Unmasked<TData> =
TData extends object ?
UnwrapFragmentRefs<RemoveMaskedMarker<RemoveFragmentName<TData>>>
: TData;
TData extends object ? UnwrapFragmentRefs<RemoveMaskedMarker<TData>> : TData;

0 comments on commit 47df71e

Please sign in to comment.