{
  "version": 3,
  "sources": ["../../src/compilerTransforms/retainConnectionDirectiveTransform.ts"],
  "sourcesContent": ["import { IRTransform } from \"relay-compiler/lib/core/CompilerContext\";\nimport { Directive, LinkedField } from \"relay-compiler/lib/core/IR\";\nimport { visit } from \"relay-compiler/lib/core/IRVisitor\";\n\ntype PathWithConnectionDirective = [any[], Directive];\n\n/**\n * relay-compiler will strip out all of its client-specific directives, which\n * makes sense as these should normally not be sent to the schema, but in our\n * case we want to keep this directive as we will need to send it to Apollo\n * Client. So we collect all the directives, let the relay-compiler filter\n * transform do its work, and then re-instate the connection directives.\n *\n * @param wrappedFilterDirectivesTransform\n *   relay-compiler's filter directives transform\n */\nexport function retainConnectionDirectiveTransform(\n  wrappedFilterDirectivesTransform: IRTransform,\n): IRTransform {\n  const filterDirectivesTransformWrapper: IRTransform = (context) => {\n    let nextContext = context;\n    const documentsWithConnectionDirectives: Record<\n      string,\n      PathWithConnectionDirective[]\n    > = {};\n\n    // Store @connection directives\n    nextContext.forEachDocument((document) => {\n      const fieldPathsWithConnectionDirectives: PathWithConnectionDirective[] =\n        [];\n      visit(document, {\n        Directive(directiveNode, _key, _parent, path) {\n          if (directiveNode.name === \"connection\") {\n            fieldPathsWithConnectionDirectives.push([\n              path!.slice(0, -2),\n              directiveNode,\n            ]);\n          }\n        },\n      });\n      documentsWithConnectionDirectives[document.name] =\n        fieldPathsWithConnectionDirectives;\n    });\n\n    // Apply original upstream transform\n    nextContext = wrappedFilterDirectivesTransform(context);\n\n    // Re-add @connection directives\n    nextContext.forEachDocument((document) => {\n      const fieldPathsWithConnectionDirectives =\n        documentsWithConnectionDirectives[document.name];\n      if (fieldPathsWithConnectionDirectives.length > 0) {\n        const nextDocument = visit(document, {\n          LinkedField(linkedFieldNode, _key, _parent, path) {\n            const match = fieldPathsWithConnectionDirectives.find(\n              ([p, _]) =>\n                path!.length === p.length && path!.every((x, i) => p[i] === x),\n            );\n            if (match) {\n              const nextLinkedFieldNode: LinkedField = {\n                ...linkedFieldNode,\n                directives: [...linkedFieldNode.directives, match[1]],\n              };\n              return nextLinkedFieldNode;\n            }\n          },\n        });\n        nextContext = nextContext.replace(nextDocument);\n      }\n    });\n\n    return nextContext;\n  };\n  return filterDirectivesTransformWrapper;\n}\n"],
  "mappings": ";AAEA,SAAS,aAAa;AAcf,SAAS,mCACd,kCACa;AACb,QAAM,mCAAgD,CAAC,YAAY;AACjE,QAAI,cAAc;AAClB,UAAM,oCAGF,CAAC;AAGL,gBAAY,gBAAgB,CAAC,aAAa;AACxC,YAAM,qCACJ,CAAC;AACH,YAAM,UAAU;AAAA,QACd,UAAU,eAAe,MAAM,SAAS,MAAM;AAC5C,cAAI,cAAc,SAAS,cAAc;AACvC,+CAAmC,KAAK;AAAA,cACtC,KAAM,MAAM,GAAG,EAAE;AAAA,cACjB;AAAA,YACF,CAAC;AAAA,UACH;AAAA,QACF;AAAA,MACF,CAAC;AACD,wCAAkC,SAAS,IAAI,IAC7C;AAAA,IACJ,CAAC;AAGD,kBAAc,iCAAiC,OAAO;AAGtD,gBAAY,gBAAgB,CAAC,aAAa;AACxC,YAAM,qCACJ,kCAAkC,SAAS,IAAI;AACjD,UAAI,mCAAmC,SAAS,GAAG;AACjD,cAAM,eAAe,MAAM,UAAU;AAAA,UACnC,YAAY,iBAAiB,MAAM,SAAS,MAAM;AAChD,kBAAM,QAAQ,mCAAmC;AAAA,cAC/C,CAAC,CAAC,GAAG,CAAC,MACJ,KAAM,WAAW,EAAE,UAAU,KAAM,MAAM,CAAC,GAAG,MAAM,EAAE,CAAC,MAAM,CAAC;AAAA,YACjE;AACA,gBAAI,OAAO;AACT,oBAAM,sBAAmC;AAAA,gBACvC,GAAG;AAAA,gBACH,YAAY,CAAC,GAAG,gBAAgB,YAAY,MAAM,CAAC,CAAC;AAAA,cACtD;AACA,qBAAO;AAAA,YACT;AAAA,UACF;AAAA,QACF,CAAC;AACD,sBAAc,YAAY,QAAQ,YAAY;AAAA,MAChD;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT;AACA,SAAO;AACT;",
  "names": []
}
