{
  "version": 3,
  "sources": ["../src/hooks.ts"],
  "sourcesContent": ["import invariant from \"invariant\";\nimport {\n  useSubscription as useApolloSubscription,\n  useQuery as useApolloQuery,\n  useMutation as useApolloMutation,\n  SubscriptionHookOptions as ApolloSubscriptionHookOptions,\n  ErrorPolicy as ApolloErrorPolicy,\n  ApolloClient,\n  ApolloCache,\n} from \"@apollo/client\";\n\nimport {\n  FetchPolicy,\n  GraphQLTaggedNode,\n  KeyType,\n  KeyTypeData,\n  OperationType,\n} from \"./types\";\nimport {\n  RefetchFn,\n  PaginationFn,\n  useCompiledLazyLoadQuery,\n  useCompiledFragment,\n  useCompiledRefetchableFragment,\n  useCompiledPaginationFragment,\n} from \"./storeObservation/compiledHooks\";\nimport { convertFetchPolicy } from \"./convertFetchPolicy\";\nimport { useOverridenOrDefaultApolloClient } from \"./useOverridenOrDefaultApolloClient\";\nimport type { CompiledArtefactModule } from \"@graphitation/apollo-react-relay-duct-tape-compiler\";\nimport { FragmentReference } from \"./storeObservation/compiledHooks/types\";\n\n/**\n * Executes a GraphQL query.\n *\n * This hook is called 'lazy' as it is used to fetch a GraphQL query _during_ render. This hook can trigger multiple\n * round trips, one for loading and one for resolving.\n *\n * @see {@link https://microsoft.github.io/graphitation/docs/apollo-react-relay-duct-tape/use-lazy-load-query}\n *\n * @param query The query operation to perform.\n * @param variables Object containing the variable values to fetch the query. These variables need to match GraphQL\n *                  variables declared inside the query.\n * @param options Options passed on to the underlying implementation.\n * @param options.context The query context to pass along the apollo link chain. Should be avoided when possible as\n *                        it will not be compatible with Relay APIs.\n * @returns An object with either an error, the result data, or neither while loading.\n */\nexport function useLazyLoadQuery<TQuery extends OperationType>(\n  query: GraphQLTaggedNode,\n  variables: TQuery[\"variables\"],\n  options?: { fetchPolicy?: FetchPolicy; context?: TQuery[\"context\"] },\n): { error?: Error; data?: TQuery[\"response\"] } {\n  invariant(\n    query?.__brand === undefined,\n    \"useLazyLoadQuery: Document must be a valid runtime type.\",\n  );\n  const apolloOptions = options && {\n    ...options,\n    fetchPolicy: convertFetchPolicy(options.fetchPolicy),\n  };\n  if (query.watchQueryDocument) {\n    return useCompiledLazyLoadQuery(query as CompiledArtefactModule, {\n      variables,\n      ...apolloOptions,\n    });\n  } else {\n    const client = useOverridenOrDefaultApolloClient();\n    return useApolloQuery(\n      // Compiled documents without narrow observables should be treated like\n      // normal queries.\n      query.executionQueryDocument || query,\n      {\n        client,\n        variables,\n        ...apolloOptions,\n      },\n    );\n  }\n}\n\n/**\n * A first-class way for an individual component to express its direct data requirements using GraphQL. The fragment\n * should select all the fields that the component directly uses in its rendering or needs to pass to external\n * functions. It should *not* select data that its children need, unless those children are intended to remain their\n * pure React props as data inputs.\n *\n * For children that *do* have their own data requirements expressed using GraphQL, the fragment should ensure to\n * spread in the child's fragment.\n *\n * @see {@link https://microsoft.github.io/graphitation/docs/apollo-react-relay-duct-tape/use-fragment}\n *\n * @note For migration purposes, this hook supports the notion that the fragment reference can be undefined. This is\n *       to support cases where useFragment is used in a tree that is conditionally using fragments.\n *\n * @param fragmentInput The GraphQL fragment document created using the `graphql` tagged template function.\n * @param fragmentRef The opaque fragment reference passed in by a parent component that has spread in this component's\n *                    fragment.\n * @returns The data corresponding to the field selections.\n */\nexport function useFragment<TKey extends KeyType>(\n  fragmentInput: GraphQLTaggedNode,\n  fragmentRef: TKey,\n): KeyTypeData<TKey>;\nexport function useFragment(\n  fragmentInput: GraphQLTaggedNode,\n  fragmentRef: undefined,\n): undefined;\nexport function useFragment<TKey extends KeyType>(\n  fragmentInput: GraphQLTaggedNode,\n  fragmentRef: TKey | undefined,\n): KeyTypeData<TKey> | undefined;\nexport function useFragment<TKey extends KeyType>(\n  fragmentInput: GraphQLTaggedNode | undefined,\n  fragmentRef: TKey,\n): KeyTypeData<TKey> {\n  invariant(\n    fragmentInput?.__brand === undefined,\n    \"useFragment: fragmentInput must be a valid runtime type.\",\n  );\n  // If fragmentInput is undefined, it means the fragment is not compiled and we should just return the fragmentRef.\n  // This is an implementation detail that should never surface to the user.\n  if (fragmentInput && fragmentInput.watchQueryDocument) {\n    return useCompiledFragment(fragmentInput, fragmentRef as FragmentReference);\n  } else {\n    return fragmentRef as unknown;\n  }\n}\n\n/**\n * Equivalent to `useFragment`, but allows refetching of its subtree of the overall query.\n *\n * @see {@link https://microsoft.github.io/graphitation/docs/apollo-react-relay-duct-tape/use-refetchable-fragment}\n *\n * @param fragmentInput The GraphQL fragment document created using the `graphql` tagged template function.\n * @param fragmentRef The opaque fragment reference passed in by a parent component that has spread in this component's\n *                    fragment.\n * @returns The data corresponding to the field selections and a function to perform the refetch.\n */\nexport function useRefetchableFragment<\n  TQuery extends OperationType,\n  TKey extends KeyType,\n>(\n  fragmentInput: GraphQLTaggedNode,\n  fragmentRef: TKey,\n): [data: KeyTypeData<TKey>, refetch: RefetchFn<TQuery[\"variables\"]>] {\n  invariant(\n    fragmentInput,\n    \"useRefetchableFragment: Missing metadata; did you forget to use the @refetchable directive?\",\n  );\n  invariant(\n    fragmentInput.__brand === undefined,\n    \"useRefetchableFragment: fragmentInput must be a valid runtime type.\",\n  );\n  invariant(\n    !!fragmentInput.watchQueryDocument,\n    \"useRefetchableFragment is only supported at this time when using compilation\",\n  );\n  return useCompiledRefetchableFragment(\n    fragmentInput as CompiledArtefactModule,\n    fragmentRef as FragmentReference,\n  );\n}\n\n/**\n * Equivalent to `useFragment`, but allows pagination of its subtree of the overall query.\n *\n * @see {@link https://microsoft.github.io/graphitation/docs/apollo-react-relay-duct-tape/use-pagination-fragment}\n *\n * @param fragmentInput The GraphQL fragment document created using the `graphql` tagged template function.\n * @param fragmentRef The opaque fragment reference passed in by a parent component that has spread in this component's\n *                    fragment.\n * @returns The data corresponding to the field selections and functions to deal with pagination.\n */\nexport function usePaginationFragment<\n  TQuery extends OperationType,\n  TKey extends KeyType,\n>(\n  fragmentInput: GraphQLTaggedNode,\n  fragmentRef: TKey,\n): {\n  data: KeyTypeData<TKey>;\n  loadNext: PaginationFn;\n  loadPrevious: PaginationFn;\n  hasNext: boolean;\n  hasPrevious: boolean;\n  isLoadingNext: boolean;\n  isLoadingPrevious: boolean;\n  refetch: RefetchFn<TQuery[\"variables\"]>;\n} {\n  invariant(\n    fragmentInput,\n    \"usePaginationFragment: Missing metadata; did you forget to use the @refetchable directive?\",\n  );\n  invariant(\n    fragmentInput.__brand === undefined,\n    \"usePaginationFragment: fragmentInput must be a valid runtime type.\",\n  );\n  invariant(\n    !!fragmentInput.watchQueryDocument,\n    \"usePaginationFragment is only supported at this time when using compilation\",\n  );\n  return useCompiledPaginationFragment(\n    fragmentInput as CompiledArtefactModule,\n    fragmentRef as FragmentReference,\n  );\n}\n\n// https://github.com/facebook/relay/blob/master/website/docs/api-reference/types/GraphQLSubscriptionConfig.md\ninterface GraphQLSubscriptionConfig<\n  TSubscriptionPayload extends OperationType,\n> {\n  subscription: GraphQLTaggedNode;\n  variables: TSubscriptionPayload[\"variables\"];\n  /**\n   * Should be avoided when possible as it will not be compatible with Relay APIs.\n   */\n  context?: TSubscriptionPayload[\"context\"];\n  /**\n   * Should response be nullable?\n   */\n  onNext?: (response: TSubscriptionPayload[\"response\"]) => void;\n  onError?: (error: Error) => void;\n}\n\n/**\n * @see {@link https://microsoft.github.io/graphitation/docs/apollo-react-relay-duct-tape/use-subscription}\n *\n * @param config\n */\nexport function useSubscription<TSubscriptionPayload extends OperationType>(\n  config: GraphQLSubscriptionConfig<TSubscriptionPayload>,\n): void {\n  const document = config.subscription;\n  invariant(\n    document?.__brand === undefined,\n    \"useSubscription: Document must be a valid runtime type.\",\n  );\n  invariant(\n    !document.watchQueryDocument,\n    \"useSubscription: Did not expect a watch query document\",\n  );\n  const client = useOverridenOrDefaultApolloClient();\n  const { error } = useApolloSubscription(\n    document.executionQueryDocument || document,\n    {\n      client,\n      variables: config.variables,\n      context: config.context,\n      onSubscriptionData: ({ subscriptionData }) => {\n        // Supposedly this never gets triggered for an error by design:\n        // https://github.com/apollographql/react-apollo/issues/3177#issuecomment-506758144\n        invariant(\n          !subscriptionData.error,\n          \"Did not expect to receive an error here\",\n        );\n        if (subscriptionData.data && config.onNext) {\n          config.onNext(subscriptionData.data);\n        }\n      },\n      errorPolicy: \"ignore\",\n    } as ApolloSubscriptionHookOptions & {\n      errorPolicy: ApolloErrorPolicy;\n    },\n  );\n  if (error) {\n    if (config.onError) {\n      config.onError(error);\n    } else {\n      console.warn(\n        `An unhandled GraphQL subscription error occurred: ${error.message}`,\n      );\n    }\n  }\n}\n\ninterface IMutationCommitterOptions<TMutationPayload extends OperationType> {\n  variables: TMutationPayload[\"variables\"];\n  optimisticResponse?: Partial<TMutationPayload[\"response\"]> | null;\n  onCompleted?: (response: TMutationPayload[\"response\"]) => void;\n  /**\n   * This version yields the ApolloCache instance, instead of the RelayStore,\n   * and usage of it will not be portable to Relay directly. However, it is a\n   * necessary evil for migration purposes.\n   */\n  updater?: (\n    cache: ApolloCache<unknown>,\n    data: TMutationPayload[\"response\"],\n  ) => void;\n  /**\n   * @deprecated Should be avoided when possible as it will not be compatible with Relay APIs.\n   */\n  context?: TMutationPayload[\"context\"];\n}\n\ntype MutationCommiter<TMutationPayload extends OperationType> = (\n  options: IMutationCommitterOptions<TMutationPayload>,\n) => Promise<{ errors?: Error[]; data?: TMutationPayload[\"response\"] }>;\n\n/**\n * @see {@link https://microsoft.github.io/graphitation/docs/apollo-react-relay-duct-tape/use-mutation}\n *\n * @param mutation\n * @returns\n */\nexport function useMutation<TMutationPayload extends OperationType>(\n  mutation: GraphQLTaggedNode,\n): [MutationCommiter<TMutationPayload>, boolean] {\n  invariant(\n    mutation?.__brand === undefined,\n    \"useMutation: Document must be a valid runtime type.\",\n  );\n  invariant(\n    !mutation.watchQueryDocument,\n    \"useMutation: Did not expect a watch query document\",\n  );\n  const client = useOverridenOrDefaultApolloClient();\n  const [apolloUpdater, { loading: mutationLoading }] = useApolloMutation(\n    mutation.executionQueryDocument || mutation,\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    { client: client as ApolloClient<any> },\n  );\n\n  return [\n    async (options: IMutationCommitterOptions<TMutationPayload>) => {\n      const apolloResult = await apolloUpdater({\n        variables: options.variables || {},\n        context: options.context,\n        optimisticResponse: options.optimisticResponse,\n        onCompleted: options.onCompleted,\n        update: options.updater,\n      });\n      if (apolloResult.errors) {\n        return {\n          errors: Array.from(Object.values(apolloResult.errors)),\n          data: apolloResult.data,\n        };\n      }\n      return {\n        data: apolloResult.data,\n      };\n    },\n    mutationLoading,\n  ];\n}\n"],
  "mappings": ";AAAA,OAAO,eAAe;AACtB;AAAA,EACE,mBAAmB;AAAA,EACnB,YAAY;AAAA,EACZ,eAAe;AAAA,OAKV;AASP;AAAA,EAGE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,0BAA0B;AACnC,SAAS,yCAAyC;AAoB3C,SAAS,iBACd,OACA,WACA,SAC8C;AAC9C;AAAA,KACE,+BAAO,aAAY;AAAA,IACnB;AAAA,EACF;AACA,QAAM,gBAAgB,WAAW;AAAA,IAC/B,GAAG;AAAA,IACH,aAAa,mBAAmB,QAAQ,WAAW;AAAA,EACrD;AACA,MAAI,MAAM,oBAAoB;AAC5B,WAAO,yBAAyB,OAAiC;AAAA,MAC/D;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAAA,EACH,OAAO;AACL,UAAM,SAAS,kCAAkC;AACjD,WAAO;AAAA;AAAA;AAAA,MAGL,MAAM,0BAA0B;AAAA,MAChC;AAAA,QACE;AAAA,QACA;AAAA,QACA,GAAG;AAAA,MACL;AAAA,IACF;AAAA,EACF;AACF;AAiCO,SAAS,YACd,eACA,aACmB;AACnB;AAAA,KACE,+CAAe,aAAY;AAAA,IAC3B;AAAA,EACF;AAGA,MAAI,iBAAiB,cAAc,oBAAoB;AACrD,WAAO,oBAAoB,eAAe,WAAgC;AAAA,EAC5E,OAAO;AACL,WAAO;AAAA,EACT;AACF;AAYO,SAAS,uBAId,eACA,aACoE;AACpE;AAAA,IACE;AAAA,IACA;AAAA,EACF;AAC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