|
1 |
| -import { ASTNode, DocumentNode, VariableDefinitionNode } from 'graphql'; |
| 1 | +import { ASTNode, DocumentNode, FragmentDefinitionNode, VariableDefinitionNode } from 'graphql'; |
| 2 | +import { pushToArrayAtKey } from './utils'; |
2 | 3 |
|
3 | 4 | const ignoreKeys = new Set(['loc']);
|
4 | 5 |
|
@@ -29,6 +30,135 @@ export interface NodeAndVarDefs {
|
29 | 30 | variableDefinitions: ReadonlyArray<VariableDefinitionNode>;
|
30 | 31 | }
|
31 | 32 |
|
| 33 | +/** @hidden */ |
| 34 | +export interface FragmentPathMap { |
| 35 | + [fragmentName: string]: ReadonlyArray<ReadonlyArray<string>>; |
| 36 | +} |
| 37 | + |
| 38 | +interface MutableFragmentPathMap { |
| 39 | + [fragmentName: string]: Array<ReadonlyArray<string>>; |
| 40 | +} |
| 41 | + |
| 42 | +/** @hidden */ |
| 43 | +export class FragmentTracer { |
| 44 | + private fragmentPathMap?: FragmentPathMap; |
| 45 | + private doc: DocumentNode; |
| 46 | + |
| 47 | + constructor(doc: DocumentNode) { |
| 48 | + this.doc = doc; |
| 49 | + } |
| 50 | + |
| 51 | + public getPathsToFragment(fragmentName: string): ReadonlyArray<ReadonlyArray<string>> { |
| 52 | + if (!this.fragmentPathMap) { |
| 53 | + this.fragmentPathMap = this.buildFragmentPathMap(); |
| 54 | + } |
| 55 | + return this.fragmentPathMap[fragmentName] || []; |
| 56 | + } |
| 57 | + |
| 58 | + // prepend the paths from the original document into this fragment to the inner fragment paths |
| 59 | + public prependFragmentPaths( |
| 60 | + fragmentName: string, |
| 61 | + pathWithinFragment: ReadonlyArray<string> |
| 62 | + ): ReadonlyArray<ReadonlyArray<string>> { |
| 63 | + return this.getPathsToFragment(fragmentName).map(path => [...path, ...pathWithinFragment]); |
| 64 | + } |
| 65 | + |
| 66 | + private getFragmentDefs(): ReadonlyArray<FragmentDefinitionNode> { |
| 67 | + return this.doc.definitions.filter( |
| 68 | + ({ kind }) => kind === 'FragmentDefinition' |
| 69 | + ) as FragmentDefinitionNode[]; |
| 70 | + } |
| 71 | + |
| 72 | + private getFragmentPartialPathMap(startNode: ASTNode): MutableFragmentPathMap { |
| 73 | + const partialPathMap: MutableFragmentPathMap = {}; |
| 74 | + const recursivelyBuildFragmentPaths = (node: ASTNode, curParents: ReadonlyArray<ASTNode>) => { |
| 75 | + if (node.kind === 'FragmentSpread') { |
| 76 | + pushToArrayAtKey(partialPathMap, node.name.value, extractPath(curParents)); |
| 77 | + } |
| 78 | + const nextParents = [...curParents, node]; |
| 79 | + if ('selectionSet' in node && node.selectionSet) { |
| 80 | + for (const selection of node.selectionSet.selections) { |
| 81 | + recursivelyBuildFragmentPaths(selection, nextParents); |
| 82 | + } |
| 83 | + } |
| 84 | + }; |
| 85 | + recursivelyBuildFragmentPaths(startNode, []); |
| 86 | + return partialPathMap; |
| 87 | + } |
| 88 | + |
| 89 | + private mergeFragmentPaths( |
| 90 | + fragmentName: string, |
| 91 | + paths: Array<ReadonlyArray<string>>, |
| 92 | + fragmentPartialPathsMap: { [fragmentName: string]: FragmentPathMap } |
| 93 | + ) { |
| 94 | + const mergedPaths: MutableFragmentPathMap = {}; |
| 95 | + |
| 96 | + const resursivelyBuildMergedPathsMap = ( |
| 97 | + curFragmentName: string, |
| 98 | + curPaths: Array<ReadonlyArray<string>>, |
| 99 | + seenFragments: ReadonlySet<string> |
| 100 | + ) => { |
| 101 | + // recursive fragments are invalid graphQL - just exit here. otherwise this will be an infinite loop |
| 102 | + if (seenFragments.has(curFragmentName)) return; |
| 103 | + const nextSeenFragments = new Set(seenFragments); |
| 104 | + nextSeenFragments.add(curFragmentName); |
| 105 | + const nextPartialPaths = fragmentPartialPathsMap[curFragmentName]; |
| 106 | + // if there are not other fragments nested inside of this fragment, we're done |
| 107 | + if (!nextPartialPaths) return; |
| 108 | + |
| 109 | + for (const [childFragmentName, childFragmentPaths] of Object.entries(nextPartialPaths)) { |
| 110 | + for (const path of curPaths) { |
| 111 | + const mergedChildPaths: Array<ReadonlyArray<string>> = []; |
| 112 | + for (const childPath of childFragmentPaths) { |
| 113 | + const mergedPath = [...path, ...childPath]; |
| 114 | + mergedChildPaths.push(mergedPath); |
| 115 | + pushToArrayAtKey(mergedPaths, childFragmentName, mergedPath); |
| 116 | + } |
| 117 | + resursivelyBuildMergedPathsMap(childFragmentName, mergedChildPaths, nextSeenFragments); |
| 118 | + } |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + resursivelyBuildMergedPathsMap(fragmentName, paths, new Set()); |
| 123 | + return mergedPaths; |
| 124 | + } |
| 125 | + |
| 126 | + private buildFragmentPathMap(): FragmentPathMap { |
| 127 | + const mainOperation = this.doc.definitions.find(node => node.kind === 'OperationDefinition'); |
| 128 | + if (!mainOperation) return {}; |
| 129 | + |
| 130 | + // partial paths are the paths inside of each fragmnt to other fragments |
| 131 | + const fragmentPartialPathsMap: { [fragmentName: string]: FragmentPathMap } = {}; |
| 132 | + for (const fragmentDef of this.getFragmentDefs()) { |
| 133 | + fragmentPartialPathsMap[fragmentDef.name.value] = this.getFragmentPartialPathMap(fragmentDef); |
| 134 | + } |
| 135 | + |
| 136 | + // start with the direct paths to fragments inside of the main operation |
| 137 | + const simpleFragmentPathMap: MutableFragmentPathMap = this.getFragmentPartialPathMap( |
| 138 | + mainOperation |
| 139 | + ); |
| 140 | + const fragmentPathMap: MutableFragmentPathMap = { ...simpleFragmentPathMap }; |
| 141 | + // next, we'll recursively trace the partials into their subpartials to fill out all possible paths to each fragment |
| 142 | + for (const [fragmentName, simplePaths] of Object.entries(simpleFragmentPathMap)) { |
| 143 | + const mergedFragmentPathsMap = this.mergeFragmentPaths( |
| 144 | + fragmentName, |
| 145 | + simplePaths, |
| 146 | + fragmentPartialPathsMap |
| 147 | + ); |
| 148 | + for (const [mergedFragmentName, mergedFragmentPaths] of Object.entries( |
| 149 | + mergedFragmentPathsMap |
| 150 | + )) { |
| 151 | + fragmentPathMap[mergedFragmentName] = [ |
| 152 | + ...(fragmentPathMap[mergedFragmentName] || []), |
| 153 | + ...mergedFragmentPaths |
| 154 | + ]; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return fragmentPathMap; |
| 159 | + } |
| 160 | +} |
| 161 | + |
32 | 162 | /**
|
33 | 163 | * Walk the document add rewrite nodes along the way
|
34 | 164 | * @param doc
|
|
0 commit comments