github.com/99designs/gqlgen@v0.17.45/integration/src/generated/fragment-masking.ts (about)

     1  import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
     2  import { FragmentDefinitionNode } from 'graphql';
     3  import { Incremental } from './graphql';
     4  
     5  
     6  export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<
     7    infer TType,
     8    any
     9  >
    10    ? [TType] extends [{ ' $fragmentName'?: infer TKey }]
    11      ? TKey extends string
    12        ? { ' $fragmentRefs'?: { [key in TKey]: TType } }
    13        : never
    14      : never
    15    : never;
    16  
    17  // return non-nullable if `fragmentType` is non-nullable
    18  export function useFragment<TType>(
    19    _documentNode: DocumentTypeDecoration<TType, any>,
    20    fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>
    21  ): TType;
    22  // return nullable if `fragmentType` is nullable
    23  export function useFragment<TType>(
    24    _documentNode: DocumentTypeDecoration<TType, any>,
    25    fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined
    26  ): TType | null | undefined;
    27  // return array of non-nullable if `fragmentType` is array of non-nullable
    28  export function useFragment<TType>(
    29    _documentNode: DocumentTypeDecoration<TType, any>,
    30    fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
    31  ): ReadonlyArray<TType>;
    32  // return array of nullable if `fragmentType` is array of nullable
    33  export function useFragment<TType>(
    34    _documentNode: DocumentTypeDecoration<TType, any>,
    35    fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
    36  ): ReadonlyArray<TType> | null | undefined;
    37  export function useFragment<TType>(
    38    _documentNode: DocumentTypeDecoration<TType, any>,
    39    fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
    40  ): TType | ReadonlyArray<TType> | null | undefined {
    41    return fragmentType as any;
    42  }
    43  
    44  
    45  export function makeFragmentData<
    46    F extends DocumentTypeDecoration<any, any>,
    47    FT extends ResultOf<F>
    48  >(data: FT, _fragment: F): 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  }