{
  "version": 3,
  "sources": ["../src/index.ts"],
  "sourcesContent": ["import { parse } from \"graphql\";\nimport type { DocumentNode } from \"graphql\";\nimport invariant from \"invariant\";\n\n// The similiar cache as Apollo Client's `graphql-tag` package. https://github.com/apollographql/graphql-tag/blob/main/src/index.ts#L10\n// A map docString -> array of cached entries with their fragments\ntype CacheEntry = { fragments: DocumentNode[]; result: DocumentNode };\nconst docCache = new Map<string, CacheEntry[]>();\n\n/**\n * This tagged template function is used to capture a single GraphQL document, such as an operation or a fragment. When\n * a document refers to fragments, those should be interpolated as trailing components, but *no* other interpolation is\n * allowed. Put differently, the documents themselves should be entirely static, which allows for static optimizations.\n * \n * Documents will be de-duped, in case multiple documents refer to the same fragment(s).\n * \n * For memory optimization, appended documents are copied by reference rather than by value. Similarly, the document\n * de-duping is done by reference, not by (deep) value; a tool that ensures documents are unique and defined only once\n * should be able to ensure that these fragments cannot semantically be different.\n * \n * @note\n * \n * Be aware that this parses the documents *at runtime*. When this is no longer appropriate, a build-step should be\n * introduced that parses the documents ahead-of-time and replaces the inline document with the AST nodes.\n * \n * Additionally, an `invariant` check can safely be removed for production builds.\n *\n * @example\n *\n ```ts\n const id = 42\n const FooFragment = graphql`\n   fragment FooFragment on Bar {\n     # This is BAD interpolation\n     foo(id: ${id})\n   }\n `\n const FooQuery = graphql`\n   query FooQuery {\n     ...FooFragment\n   }\n    # This is GOOD interpolation\n   ${FooFragment}\n `\n ```\n *\n * @param strings\n */\nexport function graphql(\n  document: TemplateStringsArray,\n  ...fragments: DocumentNode[]\n): DocumentNode {\n  invariant(\n    document.map((s) => s.trim()).filter((s) => s.length > 0).length === 1,\n    \"Interpolations are only allowed at the end of the template.\",\n  );\n\n  const cacheEntries = docCache.get(document[0]);\n\n  // We are also handling the case where the same document is used with different interpolated fragments.\n  if (cacheEntries) {\n    for (const entry of cacheEntries) {\n      const areReferencesEqual =\n        entry.fragments.length === fragments.length &&\n        entry.fragments.every((frag, i) => frag === fragments[i]);\n      if (areReferencesEqual) {\n        return entry.result;\n      }\n    }\n  }\n\n  // Parse and create new document\n  const documentNode = parse(document[0], { noLocation: true });\n  const definitions = new Set(documentNode.definitions);\n  fragments.forEach((doc) =>\n    doc.definitions.forEach((def) => definitions.add(def)),\n  );\n  const result: DocumentNode = {\n    kind: \"Document\",\n    definitions: Array.from(definitions),\n  };\n\n  if (cacheEntries) {\n    // Operation was already cached, but with different fragment interpolations. Add a new cache entry for this combination of fragments.\n    cacheEntries.push({ fragments, result });\n  } else {\n    docCache.set(document[0], [{ fragments, result }]);\n  }\n\n  return result;\n}\n"],
  "mappings": ";AAAA,SAAS,aAAa;AAEtB,OAAO,eAAe;AAKtB,IAAM,WAAW,oBAAI,IAA0B;AAyCxC,SAAS,QACd,aACG,WACW;AACd;AAAA,IACE,SAAS,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE,WAAW;AAAA,IACrE;AAAA,EACF;AAEA,QAAM,eAAe,SAAS,IAAI,SAAS,CAAC,CAAC;AAG7C,MAAI,cAAc;AAChB,eAAW,SAAS,cAAc;AAChC,YAAM,qBACJ,MAAM,UAAU,WAAW,UAAU,UACrC,MAAM,UAAU,MAAM,CAAC,MAAM,MAAM,SAAS,UAAU,CAAC,CAAC;AAC1D,UAAI,oBAAoB;AACtB,eAAO,MAAM;AAAA,MACf;AAAA,IACF;AAAA,EACF;AAGA,QAAM,eAAe,MAAM,SAAS,CAAC,GAAG,EAAE,YAAY,KAAK,CAAC;AAC5D,QAAM,cAAc,IAAI,IAAI,aAAa,WAAW;AACpD,YAAU;AAAA,IAAQ,CAAC,QACjB,IAAI,YAAY,QAAQ,CAAC,QAAQ,YAAY,IAAI,GAAG,CAAC;AAAA,EACvD;AACA,QAAM,SAAuB;AAAA,IAC3B,MAAM;AAAA,IACN,aAAa,MAAM,KAAK,WAAW;AAAA,EACrC;AAEA,MAAI,cAAc;AAEhB,iBAAa,KAAK,EAAE,WAAW,OAAO,CAAC;AAAA,EACzC,OAAO;AACL,aAAS,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE,WAAW,OAAO,CAAC,CAAC;AAAA,EACnD;AAEA,SAAO;AACT;",
  "names": []
}
