Query Knowledge Graph

Observable: Query Knowledge Graph

User Intent

"I want to explore entity relationships and visualize my knowledge graph"

Operation

  • SDK Method: graphlit.queryContentsGraph()

  • GraphQL: queryContentsGraph query

  • Entity Type: Content/Observable (graph relationships)

  • Common Use Cases: Visualize knowledge graphs, explore entity relationships, discover connections, graph analytics

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)

graph_response = await graphlit.queryContentsGraph( filter=ContentFilterInput( contents=[EntityReferenceFilterInput(id=content_id)] ) )

print(f"Nodes: {len(graph_response.graph.nodes or [])}") print(f"Edges: {len(graph_response.graph.edges or [])}")

Access nodes

for node in (graph_response.graph.nodes or []): print(f"{node.type}: {node.name}")

Access edges

for edge in (graph_response.graph.edges or []): print(f"Relationship: {edge.from_name} → {edge.type} → {edge.to_name}")

Parameters

ContentFilter (Optional)

  • contents (EntityReferenceFilter[]): Filter by specific content

    • Query graph for specific documents

  • collections (EntityReferenceFilter[]): Filter by collection

  • observableTypes (ObservableTypes[]): Filter by entity types

    • PERSON, ORGANIZATION, PLACE, etc.

Response

Developer Hints

Graph Must Be Built First

Important: Knowledge graph requires content with extraction workflow.

Filter by Content

Analyze Relationships

Visualize Graph

Variations

1. Query Full Knowledge Graph

Get entire graph:

2. Graph for Specific Document

Single document graph:

3. Graph by Entity Type

Filter by entity types:

4. Collection Knowledge Graph

Graph for collection:

5. Find Entity Connections

Explore specific entity relationships:

6. Graph Analytics

Analyze graph structure:

Common Issues

Issue: Empty graph returned Solution: Ensure content was ingested with extraction workflow. Check entities were actually extracted.

Issue: No relationships/edges Solution: Relationships are automatically inferred from co-occurrence and context. More content = more relationships.

Issue: Graph too large to visualize Solution: Filter by specific content or collections. Use entity type filters to reduce scope.

Issue: Missing expected entities Solution: Check extraction workflow configuration. Try better extraction model (Claude Sonnet 3.7).

Production Example

Knowledge graph visualization pipeline:

Entity relationship explorer:

Last updated

Was this helpful?