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