@@ -151,25 +151,41 @@ function quadsToJsonLd(quads, baseUri, prefixes = {}) {
151151 nodes . push ( jsonNode ) ;
152152 }
153153
154- // Build result
154+ // Build result - return array if multiple nodes, single object otherwise
155155 const context = buildContext ( prefixes ) ;
156156
157157 if ( nodes . length === 1 ) {
158158 return { '@context' : context , ...nodes [ 0 ] } ;
159159 }
160160
161- return { '@context' : context , '@graph' : nodes } ;
161+ // Multiple nodes: return as array (no @graph)
162+ return nodes . map ( ( node , i ) => i === 0 ? { '@context' : context , ...node } : node ) ;
162163}
163164
164165/**
165166 * Convert JSON-LD to N3.js quads
166167 */
167168function jsonLdToQuads ( jsonLd , baseUri ) {
168169 const quads = [ ] ;
169- const context = jsonLd [ '@context' ] || { } ;
170170
171- // Handle @graph or single object
172- const nodes = jsonLd [ '@graph' ] || [ jsonLd ] ;
171+ // Handle array of JSON-LD objects (e.g., from multiple PATCH operations)
172+ const documents = Array . isArray ( jsonLd ) ? jsonLd : [ jsonLd ] ;
173+
174+ // Merge all contexts and collect all nodes
175+ let mergedContext = { } ;
176+ let nodes = [ ] ;
177+
178+ for ( const doc of documents ) {
179+ if ( doc [ '@context' ] ) {
180+ mergedContext = { ...mergedContext , ...doc [ '@context' ] } ;
181+ }
182+ // Each document with @id is a node (no @graph needed)
183+ if ( doc [ '@id' ] ) {
184+ nodes . push ( doc ) ;
185+ }
186+ }
187+
188+ const context = mergedContext ;
173189
174190 for ( const node of nodes ) {
175191 if ( ! node [ '@id' ] ) continue ;
0 commit comments