|
| 1 | +import { runRead } from "../clients/neo4j.js"; |
| 2 | +import { logger } from "../logger.js"; |
| 3 | + |
| 4 | +export interface GraphSchema { |
| 5 | + fetchedAt: string; |
| 6 | + dbComponents: Array<{ name: string; versions: string[]; edition: string }>; |
| 7 | + stats: { |
| 8 | + nodeCount: number; |
| 9 | + relCount: number; |
| 10 | + labels: Record<string, number>; |
| 11 | + relTypes: Record<string, number>; |
| 12 | + relTypesCount: Record<string, number>; |
| 13 | + }; |
| 14 | + schema: Record<string, unknown>; |
| 15 | + nodeTypeProperties: Array<{ |
| 16 | + nodeType: string; |
| 17 | + nodeLabels: string[]; |
| 18 | + propertyName: string; |
| 19 | + propertyTypes: string[]; |
| 20 | + mandatory: boolean; |
| 21 | + }>; |
| 22 | + relTypeProperties: Array<{ |
| 23 | + relType: string; |
| 24 | + sourceNodeLabels: string[]; |
| 25 | + targetNodeLabels: string[]; |
| 26 | + propertyName: string; |
| 27 | + propertyTypes: string[]; |
| 28 | + mandatory: boolean; |
| 29 | + }>; |
| 30 | + indexes: unknown[]; |
| 31 | + constraints: unknown[]; |
| 32 | +} |
| 33 | + |
| 34 | +// apoc.meta.schema() can scan many nodes; give the schema queries a longer |
| 35 | +// budget than the default Cypher-query timeout. |
| 36 | +const SCHEMA_FETCH_TIMEOUT_MS = 60_000; |
| 37 | + |
| 38 | +let schemaCache: GraphSchema | null = null; |
| 39 | +let schemaPending: Promise<GraphSchema> | null = null; |
| 40 | + |
| 41 | +/** |
| 42 | + * Fetch the live graph schema via APOC (+ fallbacks for indexes and |
| 43 | + * constraints). Cached in-memory after the first successful call so |
| 44 | + * subsequent tool invocations are free. Concurrent first-callers share |
| 45 | + * one round-trip via the `schemaPending` promise. |
| 46 | + */ |
| 47 | +export async function fetchGraphSchema(): Promise<GraphSchema> { |
| 48 | + if (schemaCache) return schemaCache; |
| 49 | + if (schemaPending) return schemaPending; |
| 50 | + |
| 51 | + const opts = { timeoutMs: SCHEMA_FETCH_TIMEOUT_MS }; |
| 52 | + const start = Date.now(); |
| 53 | + |
| 54 | + schemaPending = (async () => { |
| 55 | + try { |
| 56 | + type Comp = { name: string; versions: string[]; edition: string }; |
| 57 | + type Stats = GraphSchema["stats"]; |
| 58 | + type NodeProp = GraphSchema["nodeTypeProperties"][number]; |
| 59 | + type RelProp = GraphSchema["relTypeProperties"][number]; |
| 60 | + |
| 61 | + const [components, stats, schemaRow, nodeProps, relProps, indexes, constraints] = await Promise.all([ |
| 62 | + runRead<Comp>( |
| 63 | + "CALL dbms.components() YIELD name, versions, edition RETURN name, versions, edition", |
| 64 | + {}, |
| 65 | + opts |
| 66 | + ), |
| 67 | + runRead<Stats>( |
| 68 | + "CALL apoc.meta.stats() YIELD labels, relTypes, relTypesCount, nodeCount, relCount RETURN labels, relTypes, relTypesCount, nodeCount, relCount", |
| 69 | + {}, |
| 70 | + opts |
| 71 | + ), |
| 72 | + runRead<{ value: Record<string, unknown> }>( |
| 73 | + "CALL apoc.meta.schema() YIELD value RETURN value", |
| 74 | + {}, |
| 75 | + opts |
| 76 | + ), |
| 77 | + runRead<NodeProp>( |
| 78 | + "CALL apoc.meta.nodeTypeProperties() YIELD nodeType, nodeLabels, propertyName, propertyTypes, mandatory RETURN nodeType, nodeLabels, propertyName, propertyTypes, mandatory", |
| 79 | + {}, |
| 80 | + opts |
| 81 | + ), |
| 82 | + runRead<RelProp>( |
| 83 | + "CALL apoc.meta.relTypeProperties() YIELD relType, sourceNodeLabels, targetNodeLabels, propertyName, propertyTypes, mandatory RETURN relType, sourceNodeLabels, targetNodeLabels, propertyName, propertyTypes, mandatory", |
| 84 | + {}, |
| 85 | + opts |
| 86 | + ).catch(() => [] as RelProp[]), |
| 87 | + runRead<unknown>( |
| 88 | + "CALL db.indexes() YIELD name, state, type, entityType, labelsOrTypes, properties RETURN name, state, type, entityType, labelsOrTypes, properties", |
| 89 | + {}, |
| 90 | + opts |
| 91 | + ).catch(() => [] as unknown[]), |
| 92 | + runRead<unknown>( |
| 93 | + "CALL db.constraints() YIELD name, description RETURN name, description", |
| 94 | + {}, |
| 95 | + opts |
| 96 | + ).catch(() => [] as unknown[]), |
| 97 | + ]); |
| 98 | + |
| 99 | + const result: GraphSchema = { |
| 100 | + fetchedAt: new Date().toISOString(), |
| 101 | + dbComponents: components, |
| 102 | + stats: stats[0] ?? { |
| 103 | + nodeCount: 0, |
| 104 | + relCount: 0, |
| 105 | + labels: {}, |
| 106 | + relTypes: {}, |
| 107 | + relTypesCount: {}, |
| 108 | + }, |
| 109 | + schema: schemaRow[0]?.value ?? {}, |
| 110 | + nodeTypeProperties: nodeProps, |
| 111 | + relTypeProperties: relProps, |
| 112 | + indexes, |
| 113 | + constraints, |
| 114 | + }; |
| 115 | + |
| 116 | + logger.info("graph schema fetched", { |
| 117 | + durationMs: Date.now() - start, |
| 118 | + nodeCount: result.stats.nodeCount, |
| 119 | + relCount: result.stats.relCount, |
| 120 | + labels: Object.keys(result.stats.labels ?? {}).length, |
| 121 | + }); |
| 122 | + |
| 123 | + schemaCache = result; |
| 124 | + return result; |
| 125 | + } finally { |
| 126 | + schemaPending = null; |
| 127 | + } |
| 128 | + })(); |
| 129 | + |
| 130 | + return schemaPending; |
| 131 | +} |
| 132 | + |
| 133 | +/** For tests — clears both the cached value and any in-flight fetch. */ |
| 134 | +export function _resetGraphSchemaCache(): void { |
| 135 | + schemaCache = null; |
| 136 | + schemaPending = null; |
| 137 | +} |
0 commit comments