Query Knowledge Graph
Observable: Query Knowledge Graph
User Intent
Operation
TypeScript (Canonical)
import { Graphlit } from 'graphlit-client';
import { ObservableTypes } from 'graphlit-client/dist/generated/graphql-types';
const graphlit = new Graphlit();
// Query knowledge graph for specific content
const graphResponse = await graphlit.queryContentsGraph({
contents: [{ id: contentId }]
});
console.log('Knowledge Graph:');
console.log(`Nodes: ${graphResponse.graph.nodes?.length || 0}`);
console.log(`Edges: ${graphResponse.graph.edges?.length || 0}`);
// Access nodes (entities)
graphResponse.graph.nodes?.forEach(node => {
console.log(`\n${node.type}: ${node.name}`);
if (node.description) {
console.log(` Description: ${node.description}`);
}
});
// Access edges (relationships)
graphResponse.graph.edges?.forEach(edge => {
const from = graphResponse.graph.nodes?.find(n => n.id === edge.from);
const to = graphResponse.graph.nodes?.find(n => n.id === edge.to);
console.log(`${from?.name} → ${edge.type} → ${to?.name}`);
});
// Query graph across all content
const fullGraph = await graphlit.queryContentsGraph();
console.log(`\nFull knowledge graph:`);
console.log(`Total entities: ${fullGraph.graph.nodes?.length || 0}`);
console.log(`Total relationships: ${fullGraph.graph.edges?.length || 0}`);Query knowledge graph (snake_case)
Access nodes
Access edges
Parameters
ContentFilter (Optional)
Response
Developer Hints
Graph Must Be Built First
Filter by Content
Analyze Relationships
Visualize Graph
Variations
1. Query Full Knowledge Graph
2. Graph for Specific Document
3. Graph by Entity Type
4. Collection Knowledge Graph
5. Find Entity Connections
6. Graph Analytics
Common Issues
Production Example
Last updated