Repository navigation
Description
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 nof: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 SPARQLWITHanalog): rejected withInvariantViolation … (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 attachedThe 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 withwalk.graph == None.build_annotation_siblingtherefore emits neitherf:reifiesGraphnor@graph.attach_siblingsthen appends the sibling to the outerinsertarray, outside the wrapper, so it is parsed as a default-graph node. The shorthand itself is resolved only later, inparse_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'sgraph. The sibling therefore inherits it as its template default graph (jsonld.rs:1381) but carries nof:reifiesGraph.EdgeKey::from_reifies_factsthen reportsGraphMismatch(fluree-db-core/src/edge.rs:440), whichenforce_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:1597and:794), recognize a 3-element["graph", iri, pattern]and recurse intopatternwithWalkCtx { graph: Some(iri) }. Seedwalk.graphfrom the wrapper's top-levelgraphat:1813.fromNamedaliases are resolved only injsonld.rs(resolve_graph_selector_value_for_update). The walker needs the resolved IRI, not the alias.
- Until then, reject
@annotationunder 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