Skip to content

JSON-LD update: @annotation inside a ["graph", …] template is written to the default graph, and a top-level graph rejects it #1991

Closed

Description

@bplatz

Summary

In a JSON-LD update, an @annotation on an edge that the ["graph", <iri>, <node>] template shorthand places in a named graph gets the wrong graph identity:

  • insert: the edge lands in the named graph, but the reification (f:reifiesSubject/Predicate/Object) and the annotation's own properties land in the default graph, with no f:reifiesGraph. The commit succeeds and the annotation never shows up on the edge it was written for. It does show up on the default-graph edge with the same subject, predicate and object once that edge exists, so the claim ends up attached to a different fact.
  • delete: the same shorthand retracts f:reifies* facts in the default graph, where they were never asserted. The commit succeeds, and the annotation in the named graph stays attached.
  • top-level "graph" key (the SPARQL WITH analog): rejected with InvariantViolation … (GraphMismatch); an annotation may reify exactly one edge, which doesn't describe the problem.

The per-node form ({"@id": …, "@graph": "<iri>", …}) is correct in all three cases. docs/concepts/edge-annotations.md lists "named-graph edges" as supported on the JSON-LD update surface and points users there because SPARQL UPDATE rejects annotations inside GRAPH { }. Of the three JSON-LD spellings, only one works.

Reproduced on v4.2.2 (ba984c7f5, current main).

Reproduction

fluree init
C='"@context": {"ex": "http://example.org/"}'
Q='SELECT ?role WHERE { GRAPH <http://example.org/g> { <http://example.org/alice> <http://example.org/worksFor> <http://example.org/acme> {| <http://example.org/role> ?role |} } }'

# A. per-node @graph: correct
fluree create a
fluree update a "{ $C, \"insert\": [{\"@id\": \"ex:alice\", \"@graph\": \"http://example.org/g\",
  \"ex:worksFor\": {\"@id\": \"ex:acme\", \"@annotation\": {\"ex:role\": \"Eng\"}}}] }"
fluree query a "$Q"      # Eng
fluree show -l a t:1     # f:reifiesGraph + all f:reifies* flakes carry graph g

# B. ["graph", …] shorthand: annotation lost
fluree create b
fluree update b "{ $C, \"insert\": [[\"graph\", \"http://example.org/g\", {\"@id\": \"ex:alice\",
  \"ex:worksFor\": {\"@id\": \"ex:acme\", \"@annotation\": {\"ex:role\": \"Eng\"}}}]] }"
fluree query b "$Q"      # 0 rows
fluree show -l b t:1     # edge in g; f:reifiesSubject/Predicate/Object + ex:role in the DEFAULT graph, no f:reifiesGraph

# ...and it attaches to an unrelated default-graph edge
fluree insert b '<http://example.org/alice> <http://example.org/worksFor> <http://example.org/acme> .'
fluree query b 'SELECT ?role WHERE { <http://example.org/alice> <http://example.org/worksFor> <http://example.org/acme> {| <http://example.org/role> ?role |} }'
                         # Eng

# C. top-level "graph" key: rejected
fluree create c
fluree update c "{ $C, \"graph\": \"http://example.org/g\", \"insert\": [{\"@id\": \"ex:alice\",
  \"ex:worksFor\": {\"@id\": \"ex:acme\", \"@annotation\": {\"ex:role\": \"Eng\"}}}] }"
                         # InvariantViolation … (GraphMismatch)

Delete side:

fluree create d
fluree update d "{ $C, \"insert\": [{\"@id\": \"ex:alice\", \"@graph\": \"http://example.org/g\",
  \"ex:worksFor\": {\"@id\": \"ex:acme\", \"@annotation\": {\"@id\": \"ex:claim1\", \"ex:role\": \"Eng\"}}}] }"
fluree update d "{ $C, \"delete\": [[\"graph\", \"http://example.org/g\", {\"@id\": \"ex:alice\",
  \"ex:worksFor\": {\"@id\": \"ex:acme\", \"@annotation\": {\"@id\": \"ex:claim1\"}}}]] }"
fluree show -l d t:2     # 3 retractions of f:reifies* in the DEFAULT graph, where none were asserted
fluree query d 'SELECT ?c WHERE { GRAPH <http://example.org/g> { <http://example.org/alice> <http://example.org/worksFor> <http://example.org/acme> ~ ?c } }'
                         # claim1: still attached

The per-node @graph spelling of the same delete retracts all four f:reifies* facts in g, as expected.

Where it goes wrong

@annotation is lowered on the raw JSON before parse_update resolves either graph selector (fluree-db-transact/src/parse/jsonld.rs:147-157). The lowering walker only understands per-node @graph:

  • Shorthand, insert: the array branch of lower_value_with_subject (parse/edge_annotations.rs:1597) recurses into every element. It doesn't recognize ["graph", iri, node], so it lowers the node with walk.graph == None. build_annotation_sibling therefore emits neither f:reifiesGraph nor @graph. attach_siblings then appends the sibling to the outer insert array, outside the wrapper, so it is parsed as a default-graph node. The shorthand itself is resolved only later, in parse_update_templates_with_ctx (jsonld.rs:955).
  • Shorthand, delete: walk_delete_for_annotations (edge_annotations.rs:794) has the same array branch.
  • Top-level graph: the transaction-wrapper branch (edge_annotations.rs:1813) never reads the wrapper's graph. The sibling therefore inherits it as its template default graph (jsonld.rs:1381) but carries no f:reifiesGraph. EdgeKey::from_reifies_facts then reports GraphMismatch (fluree-db-core/src/edge.rs:440), which enforce_single_target_reifiers (stage.rs:1920) turns into the invariant error.

Staging accepts case B because the bundle is internally consistent: a default-graph edge identity stored in the default graph. Nothing checks that the base edge exists there.

SPARQL UPDATE rejects the equivalent shapes on purpose (reject_with_scoped_annotations, lower_sparql_update.rs:658). Its comment describes exactly this failure. The JSON-LD side has no such guard. parse_graph_insert (jsonld.rs:227-253) does repair f:reifiesGraph after the fact for single-graph inserts, which suggests this gap was already known for that path.

Possible fixes

  • In the walker's array branches (edge_annotations.rs:1597 and :794), recognize a 3-element ["graph", iri, pattern] and recurse into pattern with WalkCtx { graph: Some(iri) }. Seed walk.graph from the wrapper's top-level graph at :1813.
    • fromNamed aliases are resolved only in jsonld.rs (resolve_graph_selector_value_for_update). The walker needs the resolved IRI, not the alias.
  • Until then, reject @annotation under either selector, as SPARQL does, instead of committing.
  • Staging could refuse a reifier bundle whose edge is not asserted in the bundle's graph. That would have turned case B into an error.

Found while checking a named-graph modeling pattern (current values annotated with the event that produced them) against the JSON-LD workaround for SPARQL's GRAPH { } annotation limit.

Activity

  1. added
    bug
    P2
    area:transact
    on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2area:transactbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions