Skip to content

Multi-ledger JSON-LD queries fail through /v1/fluree/query (dispatcher + formatter gaps) #1259

Closed

Description

@Jackamus29

Summary

Three distinct bugs prevent multi-ledger JSON-LD queries from working correctly
through the connection-scoped /v1/fluree/query HTTP route. Two live in the
HTTP route's body parsing; two live in FromQueryBuilder::execute_formatted.
They share a theme: the connection path was built around single-ledger queries
with a string from, and multi-ledger support was bolted on without fully
threading the multi-ledger awareness through the HTTP route or the formatter
step.

Reproduced on release/v4.0.4 (HEAD dff319988) and re-confirmed on main
after PR #1250 merged. None of the 85 commits between those touch the
affected code paths.


Issue 1 — from: [array] rejected by the HTTP dispatcher

Symptom

POST /v1/fluree/query
Content-Type: application/json

{ "from": ["a:main", "b:main"], "@context": {...}, "select": [...], "where": {...} }

Returns 400 { "error": "Missing ledger alias", "@type": "err:api/MissingLedger" }.

Same for from: [{"@id": "a"}, {"@id": "b"}]. The
datasets documentation shows
from: ["mydb:main", "otherdb:main"] as a supported multi-default-graph
union form.

Root cause

fluree-db-server/src/routes/query.rs:167

if let Some(from) = body.get("from").and_then(|v| v.as_str()) {
    return Ok(from.to_string());
}
Err(ServerError::MissingLedger)

.as_str() returns None for arrays and objects → bails before the dataset
builder ever runs. The engine itself accepts array from just fine when
called via query_from().jsonld() directly.


Issue 2 — Cross-graph nested projection silently returns the wrong IRI

Symptom

A query with from: A + fromNamed: { alias: B } and a nested select that
hydrates properties through a ref. The hydrated @id comes back with the
wrong prefix — the prefix from A's namespace dict applied to a SID
that was encoded against B's dict.

Observed in production: unit.@id returned as "categories/ml" when the
source ledger stores it as "units/ml" (the application ledgers happened
to assign matching namespace codes to different prefixes).

Root cause

FromQueryBuilder::execute_formatted at
fluree-db-api/src/query/builder.rs:897-905:

let (spec, _) = parse_dataset_spec(json)?;
if let Some(alias) = spec.default_graphs.first() {
    let view = self.fluree.db(alias.identifier.as_str()).await?;
    Ok(result.format_async(view.as_graph_db_ref(), &format_config).await?)
} else {
    Err(ApiError::query("No default graph for formatting"))
}

It loads only the first default-graph view for formatting. Then
format_async builds an IriCompactor from that view's namespaces() map
(fluree-db-api/src/format/iri.rs:92-101):

pub fn decode_sid(&self, sid: &Sid) -> Result<String> {
    ...
    let prefix = self.namespace_codes
        .get(&sid.namespace_code)
        .ok_or(FormatError::UnknownNamespace(sid.namespace_code))?;
    Ok(format!("{}{}", prefix, sid.name))
}

Each ledger numbers its namespaces independently at insert time. A SID from
the named ledger gets decoded against the default ledger's dict — either
erroring (UnknownNamespace) when the code doesn't exist, or silently
mis-decoding
when the code exists but maps to a different prefix.

Compounding the problem: query_connection already built a complete
DataSetDb with all views and namespace dicts, but the dataset gets dropped
between query execution and formatting. The formatter then re-parses the
spec and reloads a single view from scratch via self.fluree.db(...).

Why existing tests don't catch it

The existing multi-ledger tests (e.g.
query_connection_from_combined_datasets_selecting_subgraphs_depth_3,
query_connection_from_named_selecting_subgraphs_depth_3) all use
schema.org + wikidata.org vocabularies across every test ledger. Those
get assigned identical namespace codes in every ledger, so the
single-snapshot lookup happens to land on the right prefix. With
application-specific vocabularies whose registration order differs between
ledgers, the bug surfaces.


Issue 3 — fromNamed-only queries fail two ways

Symptom

POST /v1/fluree/query
{ "fromNamed": { "alias": { "@id": "ledger:main" } }, ... }

Returns 400 "Missing ledger alias". If you bypass the HTTP layer and call
the engine directly via query_from().jsonld().execute_formatted(), you
get a different error: "No default graph for formatting".

Root cause

Two stacked bugs:

  1. HTTP layer. Same get_ledger_id as Issue 1 — rejects because there's
    no from string. Never reaches the engine.

  2. Formatter layer. Even bypassing the HTTP layer, the
    else branch at builder.rs:903-905
    bails out:

    } else {
        Err(ApiError::query("No default graph for formatting"))
    }

The sibling DatasetQueryBuilder::execute_formatted at
builder.rs:562-573 handles
this correctly via dataset.primary() (which falls back to the first named
graph at
view/dataset.rs:121-127).
The connection-scoped path never got that same fallback.


Why these three go together

The connection path was built around single-ledger queries with a string
from. Multi-ledger support (from array, fromNamed, GRAPH alias) was
added in the engine and in parse_dataset_spec / the dataset builder,
where it works correctly. But neither the HTTP route's get_ledger_id nor
FromQueryBuilder::execute_formatted were updated to match.

The dataset builder produces a complete multi-view DataSetDb with policy,
aliases, and namespace dicts all in place. The execute step uses it
correctly. The formatter then throws it away and reloads a single view.
That's the architectural drift these three bugs share.


Suggested fix direction

Independent fixes, smallest to largest:

  1. Issue 1 + Issue 3 layer 1 — HTTP dispatcher. Relax
    get_ledger_id to accept
    array/object from (pick the first identifier for the ledger-id slot, or
    defer the MissingLedger check until requires_dataset_features has run).

  2. Issue 3 layer 2 — formatter fallback. Copy the dataset.primary()
    fallback from DatasetQueryBuilder::execute_formatted into
    FromQueryBuilder::execute_formatted.

  3. Issue 2 — cross-graph IRI decoding. Restructure
    FromQueryBuilder::execute_formatted to keep the DataSetDb alive
    through formatting (don't drop it between query_connection and the
    format step). Then teach format_results_async / IriCompactor to
    accept the full dataset and build a multi-snapshot namespace map.

Note: the recent cross-ledger fix in commit
3a09e9d79 addresses the
analogous "use the wrong ledger's namespace dict" problem in cross-ledger
policy materialization. The query result formatter is the parallel piece
of work.


Versions

  • Reproduced on: release/v4.0.4 (HEAD dff319988)
  • Re-confirmed on: main post-PR release v4.0.4 #1250
  • Toolchain: cargo 1.93.0, rustc 1.93.0

Activity

  1. self-assigned this
    on Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions