Implemented three new API endpoints for knowledge graph visualization: 1. GET /api/knowledge/graph - Full knowledge graph - Returns all entries and links with optional filtering - Supports filtering by tags, status, and node count limit - Includes orphan detection (entries with no links) 2. GET /api/knowledge/graph/stats - Graph statistics - Total entries and links counts - Orphan entries detection - Average links per entry - Top 10 most connected entries - Tag distribution across entries 3. GET /api/knowledge/graph/:slug - Entry-centered subgraph - Returns graph centered on specific entry - Supports depth parameter (1-5) for traversal distance - Includes all connected nodes up to specified depth New Files: - apps/api/src/knowledge/graph.controller.ts - apps/api/src/knowledge/graph.controller.spec.ts Modified Files: - apps/api/src/knowledge/dto/graph-query.dto.ts (added GraphFilterDto) - apps/api/src/knowledge/entities/graph.entity.ts (extended with new types) - apps/api/src/knowledge/services/graph.service.ts (added new methods) - apps/api/src/knowledge/services/graph.service.spec.ts (added tests) - apps/api/src/knowledge/knowledge.module.ts (registered controller) - apps/api/src/knowledge/dto/index.ts (exported new DTOs) - docs/scratchpads/71-graph-data-api.md (implementation notes) Test Coverage: 21 tests (all passing) - 14 service tests including orphan detection, filtering, statistics - 7 controller tests for all three endpoints Follows TDD principles with tests written before implementation. All code quality gates passed (lint, typecheck, tests). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
126 lines
4.6 KiB
Markdown
126 lines
4.6 KiB
Markdown
# Issue #71: [KNOW-019] Graph Data API
|
|
|
|
## Objective
|
|
Create API endpoints to retrieve knowledge graph data for visualization, including nodes (entries) and edges (relationships) with filtering and statistics capabilities.
|
|
|
|
## Approach
|
|
1. Review existing knowledge schema and relationships table
|
|
2. Define DTOs for graph data structures (nodes, edges, filters)
|
|
3. Write tests for graph endpoints (TDD approach)
|
|
4. Implement GraphService for data aggregation and processing
|
|
5. Create graph controller with three endpoints
|
|
6. Implement orphan detection, filtering, and node limiting
|
|
7. Test with sample data
|
|
8. Run quality checks and commit
|
|
|
|
## Progress
|
|
- [x] Review schema and existing code
|
|
- [x] Define DTOs for graph structures
|
|
- [x] Write tests for graph endpoints (RED)
|
|
- [x] Implement GraphService (GREEN)
|
|
- [x] Create graph controller endpoints (GREEN)
|
|
- [x] Implement orphan detection
|
|
- [x] Add filtering capabilities
|
|
- [x] Add node count limiting
|
|
- [ ] Run code review
|
|
- [ ] Run QA checks
|
|
- [ ] Commit changes
|
|
- [ ] Close issue
|
|
|
|
## API Endpoints
|
|
1. `GET /api/knowledge/graph` - Return full knowledge graph with filters
|
|
2. `GET /api/knowledge/graph/:slug` - Return subgraph centered on entry
|
|
3. `GET /api/knowledge/graph/stats` - Return graph statistics
|
|
|
|
## Graph Data Format
|
|
```typescript
|
|
{
|
|
nodes: [
|
|
{
|
|
id: string,
|
|
slug: string,
|
|
title: string,
|
|
type: string,
|
|
status: string,
|
|
tags: string[],
|
|
isOrphan: boolean
|
|
}
|
|
],
|
|
edges: [
|
|
{
|
|
source: string, // node id
|
|
target: string, // node id
|
|
type: string // relationship type
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
## Testing
|
|
- Unit tests for GraphService methods
|
|
- Integration tests for graph endpoints
|
|
- Test filtering, orphan detection, and node limiting
|
|
- Verify graph statistics calculation
|
|
|
|
## Notes
|
|
|
|
### Existing Code Analysis
|
|
- GraphService already exists with `getEntryGraph()` method for entry-centered graphs
|
|
- GraphNode and GraphEdge interfaces defined in entities/graph.entity.ts
|
|
- GraphQueryDto exists but only for entry-centered view (depth parameter)
|
|
- KnowledgeLinks table connects entries (source_id, target_id, resolved flag)
|
|
- No full graph endpoint exists yet
|
|
- No orphan detection implemented yet
|
|
- No graph statistics endpoint yet
|
|
|
|
### Implementation Plan
|
|
1. Create new graph.controller.ts for graph endpoints
|
|
2. Extend GraphService with:
|
|
- getFullGraph(workspaceId, filters) - full graph with optional filters
|
|
- getGraphStats(workspaceId) - graph statistics including orphan detection
|
|
3. Create new DTOs:
|
|
- GraphFilterDto - for filtering by tags, status, limit
|
|
- GraphStatsResponse - for statistics response
|
|
- FullGraphResponse - for full graph response
|
|
4. Add tests for new service methods (TDD)
|
|
5. Wire up controller to module
|
|
|
|
### Implementation Summary
|
|
|
|
**Files Created:**
|
|
- `/apps/api/src/knowledge/graph.controller.ts` - New controller with 3 endpoints
|
|
- `/apps/api/src/knowledge/graph.controller.spec.ts` - Controller tests (7 tests, all passing)
|
|
|
|
**Files Modified:**
|
|
- `/apps/api/src/knowledge/dto/graph-query.dto.ts` - Added GraphFilterDto
|
|
- `/apps/api/src/knowledge/entities/graph.entity.ts` - Extended interfaces with isOrphan, status fields, added FullGraphResponse and GraphStatsResponse
|
|
- `/apps/api/src/knowledge/services/graph.service.ts` - Added getFullGraph(), getGraphStats(), getEntryGraphBySlug()
|
|
- `/apps/api/src/knowledge/services/graph.service.spec.ts` - Added 7 new tests (14 total, all passing)
|
|
- `/apps/api/src/knowledge/knowledge.module.ts` - Registered KnowledgeGraphController
|
|
- `/apps/api/src/knowledge/dto/index.ts` - Exported GraphFilterDto
|
|
|
|
**API Endpoints Implemented:**
|
|
1. `GET /api/knowledge/graph` - Returns full knowledge graph
|
|
- Query params: tags[], status, limit
|
|
- Returns: nodes[], edges[], stats (totalNodes, totalEdges, orphanCount)
|
|
|
|
2. `GET /api/knowledge/graph/stats` - Returns graph statistics
|
|
- Returns: totalEntries, totalLinks, orphanEntries, averageLinks, mostConnectedEntries[], tagDistribution[]
|
|
|
|
3. `GET /api/knowledge/graph/:slug` - Returns entry-centered subgraph
|
|
- Query params: depth (1-5, default 1)
|
|
- Returns: centerNode, nodes[], edges[], stats
|
|
|
|
**Key Features:**
|
|
- Orphan detection: Identifies entries with no incoming or outgoing links
|
|
- Filtering: By tags, status, and node count limit
|
|
- Performance optimizations: Uses raw SQL for aggregate queries
|
|
- Tag distribution: Shows entry count per tag
|
|
- Most connected entries: Top 10 entries by link count
|
|
- Caching: Leverages existing cache service for entry-centered graphs
|
|
|
|
**Test Coverage:**
|
|
- 21 total tests across service and controller
|
|
- All tests passing
|
|
- Coverage includes orphan detection, filtering, statistics calculation
|