{
  "version": 3,
  "sources": ["../src/collectFields.ts"],
  "sourcesContent": ["import {\n  Kind,\n  FieldNode,\n  FragmentDefinitionNode,\n  FragmentSpreadNode,\n  InlineFragmentNode,\n  SelectionNode,\n  SelectionSetNode,\n} from \"graphql\";\nimport { getDirectiveValues } from \"./values\";\nimport {\n  GraphQLSkipDirective,\n  GraphQLIncludeDirective,\n  GraphQLDeferDirective,\n} from \"./schema/directives\";\n\nimport { AccumulatorMap } from \"./jsutils/AccumulatorMap\";\nimport invariant from \"invariant\";\nimport { ExecutionContext } from \"./executeWithoutSchema\";\nimport { isAbstractType, isSubType } from \"./schema/definition\";\nimport { SchemaFragment } from \"./types\";\n\nexport type FieldGroup = ReadonlyArray<FieldNode>;\n\nexport type GroupedFieldSet = Map<string, FieldGroup>;\n\nexport interface PatchFields {\n  label: string | undefined;\n  groupedFieldSet: GroupedFieldSet;\n}\n\nexport interface FieldsAndPatches {\n  groupedFieldSet: GroupedFieldSet;\n  patches: Array<PatchFields>;\n}\n\n/**\n * Given a selectionSet, collects all of the fields and returns them.\n *\n * CollectFields requires the \"runtime type\" of an object. For a field that\n * returns an Interface or Union type, the \"runtime type\" will be the actual\n * object type returned by that field.\n *\n * @internal\n */\nexport function collectFields(\n  exeContext: ExecutionContext,\n  runtimeTypeName: string,\n): FieldsAndPatches {\n  const { operation } = exeContext;\n  const groupedFieldSet = new AccumulatorMap<string, FieldNode>();\n  const patches: Array<PatchFields> = [];\n  collectFieldsImpl(\n    exeContext,\n    runtimeTypeName,\n    operation.selectionSet,\n    groupedFieldSet,\n    patches,\n    new Set(),\n  );\n  return { groupedFieldSet, patches };\n}\n\n/**\n * Given an array of field nodes, collects all of the subfields of the passed\n * in fields, and returns them at the end.\n *\n * CollectSubFields requires the \"return type\" of an object. For a field that\n * returns an Interface or Union type, the \"return type\" will be the actual\n * object type returned by that field.\n *\n * @internal\n */\n// eslint-disable-next-line max-params\nexport function collectSubfields(\n  exeContext: ExecutionContext,\n  returnTypeName: string,\n  fieldGroup: FieldGroup,\n): FieldsAndPatches {\n  const subGroupedFieldSet = new AccumulatorMap<string, FieldNode>();\n  const visitedFragmentNames = new Set<string>();\n\n  const subPatches: Array<PatchFields> = [];\n  const subFieldsAndPatches = {\n    groupedFieldSet: subGroupedFieldSet,\n    patches: subPatches,\n  };\n\n  for (const node of fieldGroup) {\n    if (node.selectionSet) {\n      collectFieldsImpl(\n        exeContext,\n        returnTypeName,\n        node.selectionSet,\n        subGroupedFieldSet,\n        subPatches,\n        visitedFragmentNames,\n      );\n    }\n  }\n  return subFieldsAndPatches;\n}\n\n// eslint-disable-next-line max-params\nfunction collectFieldsImpl(\n  exeContext: ExecutionContext,\n  runtimeTypeName: string,\n  selectionSet: SelectionSetNode,\n  groupedFieldSet: AccumulatorMap<string, FieldNode>,\n  patches: Array<PatchFields>,\n  visitedFragmentNames: Set<string>,\n): void {\n  for (const selection of selectionSet.selections) {\n    switch (selection.kind) {\n      case Kind.FIELD: {\n        if (!shouldIncludeNode(exeContext, selection)) {\n          continue;\n        }\n        groupedFieldSet.add(getFieldEntryKey(selection), selection);\n        break;\n      }\n      case Kind.INLINE_FRAGMENT: {\n        if (\n          !shouldIncludeNode(exeContext, selection) ||\n          !doesFragmentConditionMatch(\n            selection,\n            runtimeTypeName,\n            exeContext.schemaFragment,\n          )\n        ) {\n          continue;\n        }\n\n        const defer = getDeferValues(exeContext, selection);\n\n        if (defer) {\n          const patchFields = new AccumulatorMap<string, FieldNode>();\n          collectFieldsImpl(\n            exeContext,\n            runtimeTypeName,\n            selection.selectionSet,\n            patchFields,\n            patches,\n            visitedFragmentNames,\n          );\n          patches.push({\n            label: defer.label,\n            groupedFieldSet: patchFields,\n          });\n        } else {\n          collectFieldsImpl(\n            exeContext,\n            runtimeTypeName,\n            selection.selectionSet,\n            groupedFieldSet,\n            patches,\n            visitedFragmentNames,\n          );\n        }\n        break;\n      }\n      case Kind.FRAGMENT_SPREAD: {\n        const fragName = selection.name.value;\n\n        if (!shouldIncludeNode(exeContext, selection)) {\n          continue;\n        }\n\n        const defer = getDeferValues(exeContext, selection);\n        if (visitedFragmentNames.has(fragName) && !defer) {\n          continue;\n        }\n\n        const fragment = exeContext.fragments[fragName];\n        if (\n          fragment == null ||\n          !doesFragmentConditionMatch(\n            fragment,\n            runtimeTypeName,\n            exeContext.schemaFragment,\n          )\n        ) {\n          continue;\n        }\n\n        if (!defer) {\n          visitedFragmentNames.add(fragName);\n        }\n\n        if (defer) {\n          const patchFields = new AccumulatorMap<string, FieldNode>();\n          collectFieldsImpl(\n            exeContext,\n            runtimeTypeName,\n            fragment.selectionSet,\n            patchFields,\n            patches,\n            visitedFragmentNames,\n          );\n          patches.push({\n            label: defer.label,\n            groupedFieldSet: patchFields,\n          });\n        } else {\n          collectFieldsImpl(\n            exeContext,\n            runtimeTypeName,\n            fragment.selectionSet,\n            groupedFieldSet,\n            patches,\n            visitedFragmentNames,\n          );\n        }\n        break;\n      }\n    }\n  }\n}\n\n/**\n * Determines if a field should be included based on the @include and @skip\n * directives, where @skip has higher precedence than @include.\n */\nfunction shouldIncludeNode(\n  exeContext: ExecutionContext,\n  node: SelectionNode,\n): boolean {\n  if (!node.directives?.length) {\n    return true;\n  }\n\n  const skip = getDirectiveValues(exeContext, GraphQLSkipDirective, node);\n  if (skip?.if === true) {\n    return false;\n  }\n\n  const include = getDirectiveValues(exeContext, GraphQLIncludeDirective, node);\n  if (include?.if === false) {\n    return false;\n  }\n\n  return true;\n}\n\n/**\n * Determines if a fragment is applicable to the given type.\n */\nfunction doesFragmentConditionMatch(\n  fragment: FragmentDefinitionNode | InlineFragmentNode,\n  typeName: string,\n  { definitions }: SchemaFragment,\n): boolean {\n  const typeConditionNode = fragment.typeCondition;\n  if (!typeConditionNode) {\n    return true;\n  }\n\n  const conditionalTypeName = typeConditionNode.name.value;\n\n  if (conditionalTypeName === typeName) {\n    return true;\n  }\n  if (isAbstractType(definitions, conditionalTypeName)) {\n    return isSubType(definitions, conditionalTypeName, typeName);\n  }\n  return false;\n}\n\n/**\n * Implements the logic to compute the key of a given field's entry\n */\nfunction getFieldEntryKey(node: FieldNode): string {\n  return node.alias ? node.alias.value : node.name.value;\n}\n\n/**\n * Returns an object containing the `@defer` arguments if a field should be\n * deferred based on the experimental flag, defer directive present and\n * not disabled by the \"if\" argument.\n */\nfunction getDeferValues(\n  exeContext: ExecutionContext,\n  node: FragmentSpreadNode | InlineFragmentNode,\n): undefined | { label: string | undefined } {\n  const defer = getDirectiveValues(exeContext, GraphQLDeferDirective, node);\n\n  if (!defer) {\n    return;\n  }\n\n  if (defer.if === false) {\n    return;\n  }\n\n  invariant(\n    exeContext.operation.operation !== \"subscription\",\n    \"`@defer` directive not supported on subscription operations. Disable `@defer` by setting the `if` argument to `false`.\",\n  );\n\n  return {\n    label: typeof defer.label === \"string\" ? defer.label : undefined,\n  };\n}\n"],
  "mappings": ";AAAA;AAAA,EACE;AAAA,OAOK;AACP,SAAS,0BAA0B;AACnC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,sBAAsB;AAC/B,OAAO,eAAe;AAEtB,SAAS,gBAAgB,iBAAiB;AA0BnC,SAAS,cACd,YACA,iBACkB;AAClB,QAAM,EAAE,UAAU,IAAI;AACtB,QAAM,kBAAkB,IAAI,eAAkC;AAC9D,QAAM,UAA8B,CAAC;AACrC;AAAA,IACE;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA,oBAAI,IAAI;AAAA,EACV;AACA,SAAO,EAAE,iBAAiB,QAAQ;AACpC;AAaO,SAAS,iBACd,YACA,gBACA,YACkB;AAClB,QAAM,qBAAqB,IAAI,eAAkC;AACjE,QAAM,uBAAuB,oBAAI,IAAY;AAE7C,QAAM,aAAiC,CAAC;AACxC,QAAM,sBAAsB;AAAA,IAC1B,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAEA,aAAW,QAAQ,YAAY;AAC7B,QAAI,KAAK,cAAc;AACrB;AAAA,QACE;AAA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