{
  "version": 3,
  "sources": ["../../../src/storeObservation/compiledHooks/useCompiledFragment.ts"],
  "sourcesContent": ["import { useEffect, useMemo } from \"react\";\nimport invariant from \"invariant\";\nimport { useForceUpdate } from \"./useForceUpdate\";\nimport { useOverridenOrDefaultApolloClient } from \"../../useOverridenOrDefaultApolloClient\";\n\nimport type { FragmentReference } from \"./types\";\nimport type { CompiledArtefactModule } from \"@graphitation/apollo-react-relay-duct-tape-compiler\";\nimport { Cache } from \"@apollo/client/core\";\n\n/**\n * @param documents Compiled watch query document that is used to setup a narrow\n *                  observable for just the data selected by the original fragment.\n * @param fragmentReference A Node object that has a globally unique `id` field.\n */\n\nexport function useCompiledFragment(\n  documents: CompiledArtefactModule,\n  fragmentReference: FragmentReference,\n): object {\n  invariant(\n    fragmentReference,\n    \"useFragment(): Expected metadata to have been extracted from \" +\n      \"the fragment. Did you forget to invoke the compiler?\",\n  );\n  const { watchQueryDocument, metadata } = documents;\n  invariant(\n    watchQueryDocument,\n    \"useFragment(): Expected a `watchQueryDocument` to have been \" +\n      \"extracted. Did you forget to invoke the compiler?\",\n  );\n\n  const client = useOverridenOrDefaultApolloClient();\n  const forceUpdate = useForceUpdate();\n\n  const observableQuery = useMemo(\n    () =>\n      client.watchQuery({\n        fetchPolicy: \"cache-only\",\n        query: watchQueryDocument,\n        returnPartialData: false,\n        variables: {\n          ...fragmentReference.__fragments,\n          id: fragmentReference.id,\n          __fragments: fragmentReference.__fragments,\n        },\n      }),\n    [client, fragmentReference.id, fragmentReference.__fragments],\n  );\n\n  useEffect(() => {\n    let skipFirst = true;\n    const subscription = observableQuery.subscribe(\n      () => {\n        // Unclear why, but this yields twice with the same results, so skip one.\n        if (skipFirst) {\n          skipFirst = false;\n        } else {\n          forceUpdate();\n        }\n      },\n      (error) => {\n        console.log(error);\n      },\n    );\n    return () => subscription.unsubscribe();\n  }, [observableQuery]);\n\n  const result = observableQuery.getCurrentResult();\n  if (result.partial) {\n    invariant(\n      false,\n      \"useFragment(): Missing data expected to be seeded by the execution query document: %s. Please check your type policies and possibleTypes configuration. If only subset of properties is missing you might need to configure merge functions for non-normalized types.\",\n      JSON.stringify(\n        // we need the cast because queryInfo and lastDiff are private but very useful for debugging\n        (\n          observableQuery as unknown as {\n            queryInfo?: { lastDiff?: { diff?: Cache.DiffResult<unknown> } };\n          }\n        ).queryInfo?.lastDiff?.diff?.missing?.map((e) => e.path),\n      ),\n    );\n  }\n  let data = result.data;\n  if (metadata?.rootSelection) {\n    invariant(\n      data,\n      \"useFragment(): Expected Apollo to respond with previously seeded data of the execution query document: %s. Did you configure your type policies and possibleTypes correctly? Check apollo-react-relay-duct-tape README for more details.\",\n      JSON.stringify({\n        selection: metadata.rootSelection,\n        mainFragment: metadata.mainFragment,\n      }),\n    );\n    data = data[metadata.rootSelection];\n  }\n  invariant(\n    data,\n    \"useFragment(): Expected Apollo to respond with previously seeded data of the execution query document. Did you configure your type policies and possibleTypes correctly? Check apollo-react-relay-duct-tape README for more details.\",\n  );\n  return data;\n}\n"],
  "mappings": ";AAAA,SAAS,WAAW,eAAe;AACnC,OAAO,eAAe;AACtB,SAAS,sBAAsB;AAC/B,SAAS,yCAAyC;AAY3C,SAAS,oBACd,WACA,mBACQ;AAlBV;AAmBE;AAAA,IACE;AAAA,IACA;AAAA,EAEF;AACA,QAAM,EAAE,oBAAoB,SAAS,IAAI;AACzC;AAAA,IACE;AAAA,IACA;AAAA,EAEF;AAEA,QAAM,SAAS,kCAAkC;AACjD,QAAM,cAAc,eAAe;AAEnC,QAAM,kBAAkB;AAAA,IACtB,MACE,OAAO,WAAW;AAAA,MAChB,aAAa;AAAA,MACb,OAAO;AAAA,MACP,mBAAmB;AAAA,MACnB,WAAW;AAAA,QACT,GAAG,kBAAkB;AAAA,QACrB,IAAI,kBAAkB;AAAA,QACtB,aAAa,kBAAkB;AAAA,MACjC;AAAA,IACF,CAAC;AAAA,IACH,CAAC,QAAQ,kBAAkB,IAAI,kBAAkB,WAAW;AAAA,EAC9D;AAEA,YA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