newOS for Developers

Back to Graph Traversal

One-Hop Query

Direct edge connections from a node

beginnergetEdges(type, id)

Overview

One-hop queries return all direct connections from a single node. This is the building block for more complex graph traversals.

The result is an array of connected node IDs, not full objects.

How It Works

// One-hop: direct connections only
[Start Node] ──edge──▶ [Connected Nodes]

// Example: Get all posts in a folder
folder+abc123+attachment+post
        │
        └──▶ ["post-1", "post-2", "post-3"]

// Example: Get all followers
user+xyz789+ratedBy+user
        │
        └──▶ ["follower-1", "follower-2"]

Usage Example

import { getEdges } from "@/lib/cache";

// Basic one-hop query
const connectedIds = await getEdges(
  "folder",      // source entity type
  "folder-123",  // source entity ID
  "attachment",  // relationship type
  "post"         // target entity type
);

// Returns: ["post-1", "post-2", "post-3"]

// Common one-hop queries
const postsInFolder = await getEdges("folder", folderId, "attachment", "post");
const usersMoods = await getEdges("user", userId, "author", "folder");
const followers = await getEdges("user", userId, "ratedBy", "user");
const following = await getEdges("user", userId, "rates", "user");
const accessList = await getEdges("folder", moodId, "access", "user");

// Hydrate results with full data
const fullPosts = await Promise.all(
  postsInFolder.map(id => getPost({ id }))
);

One-Hop vs Two-Hop

// ONE-HOP: Direct connections
User A ──follows──▶ User B
         │
         └── Returns: [User B]

// TWO-HOP: Friends of friends (chain queries)
User A ──follows──▶ User B ──follows──▶ User C
         │                      │
         │                      └── Second hop
         └── First hop

// Implementation:
const firstHop = await getEdges("user", "A", "rates", "user");
// Returns: ["B"]

const secondHop = await Promise.all(
  firstHop.map(id => getEdges("user", id, "rates", "user"))
);
// Returns: [["C", "D"]]

Performance

  • Cache hit: ~1ms (IndexedDB lookup)
  • Cache miss: ~50-100ms (API + cache write)
  • No network call if edge is in cache
  • Edges are updated on write operations