Skip to content

Commit

Permalink
run pnpm run generate
Browse files Browse the repository at this point in the history
  • Loading branch information
tnyo43 committed Oct 15, 2023
1 parent 8c57e25 commit 7e72f4f
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
import { DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
import { FragmentDefinitionNode } from 'graphql';
import { Incremental } from './graphql';


type Primitive = string | number | boolean | bigint | symbol | null | undefined;
type ExcludePrimitive<T> = Exclude<T, Primitive>;
type ExtractPrimitive<T> = Exclude<T, Exclude<T, Primitive>>;
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I
) => void
? I
: never;


export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<
infer TType,
any
Expand Down Expand Up @@ -42,11 +52,44 @@ export function useFragment<TType>(
}


type UnionToIntersectGroupByTypeName<U, V = U> = [V] extends [
{ __typename?: infer TypeName }
]
? TypeName extends any
? UnionToIntersection<U extends { __typename?: TypeName } ? U : never>
: never
: never;

type UnionFieldToIntersection<T> = [T] extends [never] ? never
: [T] extends [Array<unknown>]
? Array<
| UnionFieldToIntersection<ExcludePrimitive<T[number]>>
| ExtractPrimitive<T[number]>
>
: UnionToIntersectGroupByTypeName<T> extends infer V
? {
[Key in keyof V]:
| UnionFieldToIntersection<ExcludePrimitive<V[Key]>>
| ExtractPrimitive<V[Key]>;
}
: never;

type Flatten<F> = [F] extends [never] ? never
: F extends Array<unknown>
? Array<Flatten<ExcludePrimitive<F[number]>> | ExtractPrimitive<F[number]>>
: {
[Key in keyof Omit<F, " $fragmentRefs" | " $fragmentName">]:
| Flatten<ExcludePrimitive<F[Key]>>
| ExtractPrimitive<F[Key]>;
} & (F extends { " $fragmentRefs"?: { [K in string]: infer FRefs }; }
? (FRefs extends any ? Flatten<FRefs> : never) : {}
);
export type UnmaskFragment<F> = UnionFieldToIntersection<Flatten<F>>;

export function makeFragmentData<
F extends DocumentTypeDecoration<any, any>,
FT extends ResultOf<F>
>(data: FT, _fragment: F): FragmentType<F> {
return data as FragmentType<F>;
>(data: UnmaskFragment<FragmentType<F>>, _fragment: F): FragmentType<F> {
return data as any;
}
export function isFragmentReady<TQuery, TFrag>(
queryNode: DocumentTypeDecoration<TQuery, any>,
Expand Down

0 comments on commit 7e72f4f

Please sign in to comment.