|
| 1 | +import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from "@graphql-typed-document-node/core"; |
| 2 | +import { FragmentDefinitionNode } from "graphql"; |
| 3 | +import { Incremental } from "./graphql"; |
| 4 | + |
| 5 | +export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = |
| 6 | + TDocumentType extends DocumentTypeDecoration<infer TType, any> |
| 7 | + ? [TType] extends [{ " $fragmentName"?: infer TKey }] |
| 8 | + ? TKey extends string |
| 9 | + ? { " $fragmentRefs"?: { [key in TKey]: TType } } |
| 10 | + : never |
| 11 | + : never |
| 12 | + : never; |
| 13 | + |
| 14 | +// return non-nullable if `fragmentType` is non-nullable |
| 15 | +export function useFragment<TType>( |
| 16 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 17 | + fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> |
| 18 | +): TType; |
| 19 | +// return nullable if `fragmentType` is nullable |
| 20 | +export function useFragment<TType>( |
| 21 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 22 | + fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined |
| 23 | +): TType | null | undefined; |
| 24 | +// return array of non-nullable if `fragmentType` is array of non-nullable |
| 25 | +export function useFragment<TType>( |
| 26 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 27 | + fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> |
| 28 | +): ReadonlyArray<TType>; |
| 29 | +// return array of nullable if `fragmentType` is array of nullable |
| 30 | +export function useFragment<TType>( |
| 31 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 32 | + fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined |
| 33 | +): ReadonlyArray<TType> | null | undefined; |
| 34 | +export function useFragment<TType>( |
| 35 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 36 | + fragmentType: |
| 37 | + | FragmentType<DocumentTypeDecoration<TType, any>> |
| 38 | + | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> |
| 39 | + | null |
| 40 | + | undefined |
| 41 | +): TType | ReadonlyArray<TType> | null | undefined { |
| 42 | + return fragmentType as any; |
| 43 | +} |
| 44 | + |
| 45 | +export function makeFragmentData<F extends DocumentTypeDecoration<any, any>, FT extends ResultOf<F>>( |
| 46 | + data: FT, |
| 47 | + _fragment: F |
| 48 | +): FragmentType<F> { |
| 49 | + return data as FragmentType<F>; |
| 50 | +} |
| 51 | +export function isFragmentReady<TQuery, TFrag>( |
| 52 | + queryNode: DocumentTypeDecoration<TQuery, any>, |
| 53 | + fragmentNode: TypedDocumentNode<TFrag>, |
| 54 | + data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined |
| 55 | +): data is FragmentType<typeof fragmentNode> { |
| 56 | + const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__ |
| 57 | + ?.deferredFields; |
| 58 | + |
| 59 | + if (!deferredFields) return true; |
| 60 | + |
| 61 | + const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; |
| 62 | + const fragName = fragDef?.name?.value; |
| 63 | + |
| 64 | + const fields = (fragName && deferredFields[fragName]) || []; |
| 65 | + return fields.length > 0 && fields.every((field) => data && field in data); |
| 66 | +} |
0 commit comments