|
| 1 | +/* Functions used to output query graphs as [mermaid graphs](https://mermaid.js.org/syntax/flowchart.html). */ |
| 2 | + |
| 3 | +import { ObjectType } from "@apollo/federation-internals"; |
| 4 | +import { Edge, FEDERATED_GRAPH_ROOT_SOURCE, QueryGraph, Vertex, isFederatedGraphRootType, simpleTraversal } from "./querygraph"; |
| 5 | + |
| 6 | +export type MermaidOptions = { |
| 7 | + includeRootTypeLinks?: boolean, |
| 8 | +} |
| 9 | + |
| 10 | +export class MermaidGraph { |
| 11 | + private readonly before: string[] = []; |
| 12 | + private readonly after: string[] = []; |
| 13 | + private readonly subgraphs = new Map<string, string[]>(); |
| 14 | + |
| 15 | + private isBuilt = false; |
| 16 | + |
| 17 | + constructor( |
| 18 | + private readonly graph: QueryGraph, |
| 19 | + private readonly options: MermaidOptions = {}, |
| 20 | + ) { |
| 21 | + for (const name of graph.sources.keys()) { |
| 22 | + if (name === this.graph.name || name === FEDERATED_GRAPH_ROOT_SOURCE) { |
| 23 | + continue; |
| 24 | + } |
| 25 | + this.subgraphs.set(name, []); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private subgraphName(vertex: Vertex): string | undefined { |
| 30 | + if (vertex.source === this.graph.name || vertex.source === FEDERATED_GRAPH_ROOT_SOURCE) { |
| 31 | + return undefined; |
| 32 | + } |
| 33 | + return vertex.source; |
| 34 | + } |
| 35 | + |
| 36 | + private vertexName(vertex: Vertex): string { |
| 37 | + if (isFederatedGraphRootType(vertex.type)) { |
| 38 | + return `root-${vertex.type.name.slice(1, vertex.type.name.length-1)}`; |
| 39 | + } |
| 40 | + const sg = this.subgraphName(vertex); |
| 41 | + const n = sg ? `${vertex.type.name}-${sg}` : `${vertex.type.name}`; |
| 42 | + return vertex.provideId ? `${n}-${vertex.provideId}` : n; |
| 43 | + } |
| 44 | + |
| 45 | + addVertex(vertex: Vertex): void { |
| 46 | + const sg = this.subgraphName(vertex); |
| 47 | + const addTo = sg ? this.subgraphs.get(sg)! : this.before; |
| 48 | + if (isFederatedGraphRootType(vertex.type)) { |
| 49 | + addTo.push(`${this.vertexName(vertex)}(["root(${vertex.type.name.slice(1, vertex.type.name.length)})"])`); |
| 50 | + } else { |
| 51 | + addTo.push(`${this.vertexName(vertex)}["${vertex.toString()}"]`); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + addEdge(edge: Edge): boolean { |
| 56 | + switch (edge.transition.kind) { |
| 57 | + case 'FieldCollection': |
| 58 | + if (edge.transition.definition.name.startsWith('_')) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + break; |
| 62 | + case 'RootTypeResolution': |
| 63 | + if (!(this.options.includeRootTypeLinks ?? true)) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + break; |
| 67 | + case 'SubgraphEnteringTransition': |
| 68 | + const rt = edge.tail.type as ObjectType; |
| 69 | + if (rt.fields().filter((f) => !f.name.startsWith('_')).length === 0) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + const head = this.vertexName(edge.head); |
| 76 | + const tail = this.vertexName(edge.tail); |
| 77 | + const addTo = edge.head.source !== this.graph.name && edge.head.source === edge.tail.source |
| 78 | + ? this.subgraphs.get(edge.head.source)! |
| 79 | + : this.after; |
| 80 | + const label = edge.label(); |
| 81 | + if (label.length === 0) { |
| 82 | + addTo.push(`${head} --> ${tail}`); |
| 83 | + } else { |
| 84 | + addTo.push(`${head} -->|"${label}"| ${tail}`); |
| 85 | + } |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + build(): void { |
| 90 | + if (this.isBuilt) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + simpleTraversal( |
| 95 | + this.graph, |
| 96 | + (v) => this.addVertex(v), |
| 97 | + (e) => this.addEdge(e), |
| 98 | + ); |
| 99 | + |
| 100 | + this.isBuilt = true; |
| 101 | + } |
| 102 | + |
| 103 | + toString(): string { |
| 104 | + this.build(); |
| 105 | + |
| 106 | + const final = [ 'flowchart TD' ]; |
| 107 | + this.before.forEach((b) => final.push(' ' + b)); |
| 108 | + for (const [name, data] of this.subgraphs.entries()) { |
| 109 | + final.push(` subgraph ${name}`); |
| 110 | + data.forEach((d) => final.push(' ' + d)); |
| 111 | + final.push(' end'); |
| 112 | + } |
| 113 | + this.after.forEach((a) => final.push(' ' + a)); |
| 114 | + return final.join('\n'); |
| 115 | + } |
| 116 | +} |
0 commit comments