|
| 1 | +/* eslint-disable */ |
| 2 | +import type { |
| 3 | + ResultOf, |
| 4 | + DocumentTypeDecoration, |
| 5 | +} from "@graphql-typed-document-node/core" |
| 6 | +import type { Incremental, TypedDocumentString } from "./graphql.ts" |
| 7 | + |
| 8 | +export type FragmentType< |
| 9 | + TDocumentType extends DocumentTypeDecoration<any, any>, |
| 10 | +> = |
| 11 | + TDocumentType extends DocumentTypeDecoration<infer TType, any> |
| 12 | + ? [TType] extends [{ " $fragmentName"?: infer TKey }] |
| 13 | + ? TKey extends string |
| 14 | + ? { " $fragmentRefs"?: { [key in TKey]: TType } } |
| 15 | + : never |
| 16 | + : never |
| 17 | + : never |
| 18 | + |
| 19 | +// return non-nullable if `fragmentType` is non-nullable |
| 20 | +export function useFragment<TType>( |
| 21 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 22 | + fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>, |
| 23 | +): TType |
| 24 | +// return nullable if `fragmentType` is undefined |
| 25 | +export function useFragment<TType>( |
| 26 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 27 | + fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | undefined, |
| 28 | +): TType | undefined |
| 29 | +// return nullable if `fragmentType` is nullable |
| 30 | +export function useFragment<TType>( |
| 31 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 32 | + fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null, |
| 33 | +): TType | null |
| 34 | +// return nullable if `fragmentType` is nullable or undefined |
| 35 | +export function useFragment<TType>( |
| 36 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 37 | + fragmentType: |
| 38 | + | FragmentType<DocumentTypeDecoration<TType, any>> |
| 39 | + | null |
| 40 | + | undefined, |
| 41 | +): TType | null | undefined |
| 42 | +// return array of non-nullable if `fragmentType` is array of non-nullable |
| 43 | +export function useFragment<TType>( |
| 44 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 45 | + fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>>, |
| 46 | +): Array<TType> |
| 47 | +// return array of nullable if `fragmentType` is array of nullable |
| 48 | +export function useFragment<TType>( |
| 49 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 50 | + fragmentType: |
| 51 | + | Array<FragmentType<DocumentTypeDecoration<TType, any>>> |
| 52 | + | null |
| 53 | + | undefined, |
| 54 | +): Array<TType> | null | undefined |
| 55 | +// return readonly array of non-nullable if `fragmentType` is array of non-nullable |
| 56 | +export function useFragment<TType>( |
| 57 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 58 | + fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>, |
| 59 | +): ReadonlyArray<TType> |
| 60 | +// return readonly array of nullable if `fragmentType` is array of nullable |
| 61 | +export function useFragment<TType>( |
| 62 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 63 | + fragmentType: |
| 64 | + | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> |
| 65 | + | null |
| 66 | + | undefined, |
| 67 | +): ReadonlyArray<TType> | null | undefined |
| 68 | +export function useFragment<TType>( |
| 69 | + _documentNode: DocumentTypeDecoration<TType, any>, |
| 70 | + fragmentType: |
| 71 | + | FragmentType<DocumentTypeDecoration<TType, any>> |
| 72 | + | Array<FragmentType<DocumentTypeDecoration<TType, any>>> |
| 73 | + | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> |
| 74 | + | null |
| 75 | + | undefined, |
| 76 | +): TType | Array<TType> | ReadonlyArray<TType> | null | undefined { |
| 77 | + return fragmentType as any |
| 78 | +} |
| 79 | + |
| 80 | +export function makeFragmentData< |
| 81 | + F extends DocumentTypeDecoration<any, any>, |
| 82 | + FT extends ResultOf<F>, |
| 83 | +>(data: FT, _fragment: F): FragmentType<F> { |
| 84 | + return data as FragmentType<F> |
| 85 | +} |
| 86 | +export function isFragmentReady<TQuery, TFrag>( |
| 87 | + queryNode: TypedDocumentString<TQuery, any>, |
| 88 | + fragmentNode: TypedDocumentString<TFrag, any>, |
| 89 | + data: |
| 90 | + | FragmentType<TypedDocumentString<Incremental<TFrag>, any>> |
| 91 | + | null |
| 92 | + | undefined, |
| 93 | +): data is FragmentType<typeof fragmentNode> { |
| 94 | + const deferredFields = queryNode.__meta__?.deferredFields as Record< |
| 95 | + string, |
| 96 | + (keyof TFrag)[] |
| 97 | + > |
| 98 | + const fragName = fragmentNode.__meta__?.fragmentName as string | undefined |
| 99 | + |
| 100 | + if (!deferredFields || !fragName) return true |
| 101 | + |
| 102 | + const fields = deferredFields[fragName] ?? [] |
| 103 | + return fields.length > 0 && fields.every(field => data && field in data) |
| 104 | +} |
0 commit comments