graphql
Version:
A Query Language and Runtime which can target any service.
1 lines • 2.18 kB
Source Map (JSON)
{"version":3,"file":"Path.js","sourceRoot":"","sources":["../../src/jsutils/Path.ts"],"names":[],"mappings":"AAmBA,MAAM,UAAU,OAAO,CACrB,IAAgC,EAChC,GAAoB,EACpB,QAA4B;IAE5B,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC;AACjC,CAAC;AA4BD,MAAM,UAAU,WAAW,CACzB,IAA2B;IAE3B,MAAM,SAAS,GAAG,EAAE,CAAC;IACrB,IAAI,IAAI,GAAG,IAAI,CAAC;IAChB,OAAO,IAAI,EAAE,CAAC;QACZ,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACzB,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IACD,OAAO,SAAS,CAAC,OAAO,EAAE,CAAC;AAC7B,CAAC","sourcesContent":["/** @category Paths */\n\nimport type { Maybe } from './Maybe.ts';\n\n/** Represents a linked response path from a field back to the root response. */\nexport interface Path {\n /** The previous segment in the linked response path, or undefined at the root. */\n readonly prev: Path | undefined;\n /** The field name or list index for this response path segment. */\n readonly key: string | number;\n /** The runtime object type name associated with this path segment, if known. */\n readonly typename: string | undefined;\n}\n\n/**\n * Given a Path and a key, return a new Path containing the new key.\n *\n * @internal\n */\nexport function addPath(\n prev: Readonly<Path> | undefined,\n key: string | number,\n typename: string | undefined,\n): Path {\n return { prev, key, typename };\n}\n\n/**\n * Given a Path, return an Array of the path keys.\n * @param path - The linked response path to flatten.\n * @returns An array of response path keys from root to leaf.\n * @example\n * ```ts\n * import { pathToArray } from 'graphql/jsutils/Path';\n *\n * const path = {\n * prev: {\n * prev: {\n * prev: undefined,\n * key: 'viewer',\n * typename: 'Query',\n * },\n * key: 'friends',\n * typename: 'User',\n * },\n * key: 0,\n * typename: undefined,\n * };\n *\n * pathToArray(path); // => ['viewer', 'friends', 0]\n * pathToArray(undefined); // => []\n * ```\n */\nexport function pathToArray(\n path: Maybe<Readonly<Path>>,\n): Array<string | number> {\n const flattened = [];\n let curr = path;\n while (curr) {\n flattened.push(curr.key);\n curr = curr.prev;\n }\n return flattened.reverse();\n}\n"]}