Skip to content

GraphInspector

reference
1 min readUpdated

Kind: Class

Source: packages/core/inspector/graph-inspector.ts

Part of: Core

GraphInspector builds and enriches Nest’s serialized dependency graph by inspecting modules, providers, controllers, and instance wrappers. It records class nodes, entry points, and enhancer relationships, while tracking partial graphs when inspection cannot complete successfully. This class is primarily used by Nest’s internal scanning and diagnostics infrastructure.

Methods

MethodSignatureReturns
inspectModulesinspectModules(modules: Map<string, Module>)void
registerPartialregisterPartial(error: unknown)void
inspectInstanceWrapperinspectInstanceWrapper(source: InstanceWrapper<T>, moduleRef: Module)void
insertEnhancerMetadataCacheinsertEnhancerMetadataCache(entry: EnhancerMetadataCacheEntry)void
insertOrphanedEnhancerinsertOrphanedEnhancer(entry: OrphanedEnhancerDefinition)void
insertAttachedEnhancerinsertAttachedEnhancer(wrapper: InstanceWrapper)void
insertEntrypointDefinitioninsertEntrypointDefinition(definition: Entrypoint<T>, parentId: string)void
insertClassNodeinsertClassNode(moduleRef: Module, wrapper: InstanceWrapper, type: Exclude<Node['metadata']['type'], 'module'>)void

Diagram

mermaid
graph LR
  Modules[Module container] --> InspectModules[inspectModules()]
  InspectModules --> Wrappers[Instance wrappers]
  Wrappers --> InspectWrapper[inspectInstanceWrapper()]
  InspectWrapper --> ClassNode[insertClassNode()]
  InspectWrapper --> Entrypoint[insertEntrypointDefinition()]
  InspectWrapper --> Enhancers[Enhancer metadata]

  Enhancers --> Cache[insertEnhancerMetadataCache()]
  Cache --> Attached[insertAttachedEnhancer()]
  Cache --> Orphaned[insertOrphanedEnhancer()]

  ClassNode --> Graph[SerializedGraph]
  Entrypoint --> Graph
  Attached --> Graph
  Orphaned --> Graph

  InspectModules -. inspection failure .-> Partial[registerPartial()]
  Partial --> Graph

Usage

ts
import { GraphInspector } from '@nestjs/core/inspector/graph-inspector';
import { SerializedGraph } from '@nestjs/core/inspector/serialized-graph';

// Typically created and used internally during Nest application scanning.
const graph = new SerializedGraph();
const inspector = new GraphInspector(graph);

try {
  // `container` represents Nest's internal module container.
  inspector.inspectModules(container.getModules());
} catch (error) {
  // Preserve the graph and mark it as incomplete when inspection fails.
  inspector.registerPartial(error);
}

// The serialized graph can then be consumed by diagnostics or tooling.
console.log(graph);

AI Coding Instructions

  • Treat GraphInspector as internal infrastructure: preserve its relationship with SerializedGraph, module scanning, and dependency-wrapper metadata.
  • When adding a new graph element, ensure it is inserted consistently through the appropriate node, entry-point, or enhancer method.
  • Cache enhancer metadata before resolving attached or orphaned enhancers; enhancer relationships may be discovered after their source wrapper is inspected.
  • Use registerPartial() when inspection failures should not prevent application startup or diagnostic graph output.
  • Avoid assuming every wrapper has a concrete class or host relationship; dynamic providers, aliases, and orphaned enhancers require defensive handling.

Used by

7 references from 7 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (7)

  • ListenersControllerpackages/microservices/listeners-controller.ts:45
  • MicroservicesModulepackages/microservices/microservices-module.ts:23
  • NestMicroservicepackages/microservices/nest-microservice.ts:35
  • TestingModuleOptionspackages/testing/testing-module.builder.ts:29
  • TestingModulepackages/testing/testing-module.ts:26
  • SocketModulepackages/websockets/socket-module.ts:27
  • WebSocketsControllerpackages/websockets/web-sockets-controller.ts:29

Was this page helpful?

Download as PDF
GraphInspector — NestJS head-to-head