|
| 1 | +--- |
| 2 | +title: Execute WOQL JSON-LD Queries Directly |
| 3 | +nextjs: |
| 4 | + metadata: |
| 5 | + title: Execute WOQL JSON-LD Queries Directly |
| 6 | + description: How to run raw WOQL JSON-LD queries with the JavaScript client, enabling access to new server features before client library support |
| 7 | + keywords: WOQL, JSON-LD, AST, raw queries, client, JavaScript |
| 8 | + openGraph: |
| 9 | + images: https://assets.terminusdb.com/docs/technical-documentation-terminuscms-og.png |
| 10 | + alternates: |
| 11 | + canonical: https://terminusdb.org/docs/woql-json-ld-queries/ |
| 12 | +media: [] |
| 13 | +--- |
| 14 | + |
| 15 | +This guide shows how to execute WOQL queries using raw JSON-LD format with the JavaScript client, enabling you to use new server features even before they have corresponding helper methods in the client library. |
| 16 | + |
| 17 | +## Why Use JSON-LD Queries? |
| 18 | + |
| 19 | +While the WOQL JavaScript API provides convenient builder methods like `WOQL.triple()` and `WOQL.and()`, there are scenarios where using raw JSON-LD queries is useful: |
| 20 | + |
| 21 | +- **Early Adoption**: Use new server features before the client library adds helper methods |
| 22 | +- **Debugging**: Inspect the exact query structure being sent to the server |
| 23 | +- **Testing**: Validate server behavior with precise JSON-LD queries |
| 24 | +- **Migration**: Port queries from other clients or documentation examples |
| 25 | +- **Advanced Features**: Access experimental or specialized functionality |
| 26 | + |
| 27 | +## The WOQL JSON-LD Format |
| 28 | + |
| 29 | +Internally, all WOQL queries are represented as JSON-LD (JSON for Linked Data) before being sent to the TerminusDB server. The client's builder methods simply provide a convenient way to construct this JSON-LD, such as for the Javascript and Python clients. |
| 30 | + |
| 31 | +### Example: Simple Triple Query |
| 32 | + |
| 33 | +**WOQL Builder Syntax:** |
| 34 | +```javascript |
| 35 | +WOQL.triple('v:Person', 'rdf:type', '@schema:Person') |
| 36 | +``` |
| 37 | + |
| 38 | +**Equivalent JSON-LD:** |
| 39 | +```json |
| 40 | +{ |
| 41 | + "@type": "Triple", |
| 42 | + "subject": { |
| 43 | + "@type": "NodeValue", |
| 44 | + "variable": "Person" |
| 45 | + }, |
| 46 | + "predicate": { |
| 47 | + "@type": "NodeValue", |
| 48 | + "node": "rdf:type" |
| 49 | + }, |
| 50 | + "object": { |
| 51 | + "@type": "NodeValue", |
| 52 | + "node": "@schema:Person" |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Executing JSON-LD Queries |
| 58 | + |
| 59 | +The JavaScript client's `query()` method accepts both `WOQLQuery` objects and raw JSON-LD objects. |
| 60 | + |
| 61 | +### Method 1: Direct JSON-LD with client.query() |
| 62 | + |
| 63 | +Pass the JSON-LD object directly to `client.query()`: |
| 64 | + |
| 65 | +```javascript |
| 66 | +const { WOQLClient } = require('@terminusdb/terminusdb-client'); |
| 67 | + |
| 68 | +const client = new WOQLClient('http://127.0.0.1:6363', { |
| 69 | + user: 'admin', |
| 70 | + organization: 'admin', |
| 71 | + key: 'root' |
| 72 | +}); |
| 73 | + |
| 74 | +// Raw JSON-LD query |
| 75 | +const jsonQuery = { |
| 76 | + "@type": "And", |
| 77 | + "and": [ |
| 78 | + { |
| 79 | + "@type": "Triple", |
| 80 | + "subject": { "@type": "NodeValue", "variable": "Person" }, |
| 81 | + "predicate": { "@type": "NodeValue", "node": "rdf:type" }, |
| 82 | + "object": { "@type": "NodeValue", "node": "@schema:Person" } |
| 83 | + }, |
| 84 | + { |
| 85 | + "@type": "Triple", |
| 86 | + "subject": { "@type": "NodeValue", "variable": "Person" }, |
| 87 | + "predicate": { "@type": "NodeValue", "node": "@schema:name" }, |
| 88 | + "object": { "@type": "NodeValue", "variable": "Name" } |
| 89 | + } |
| 90 | + ] |
| 91 | +}; |
| 92 | + |
| 93 | +const result = await client.query(jsonQuery); |
| 94 | +console.log(result.bindings); |
| 95 | +``` |
| 96 | + |
| 97 | +### Method 2: Using WOQLQuery().json() |
| 98 | + |
| 99 | +Convert between WOQL builder syntax and JSON-LD using the `json()` method: |
| 100 | + |
| 101 | +```javascript |
| 102 | +const { WOQL } = require('@terminusdb/terminusdb-client'); |
| 103 | + |
| 104 | +// Convert WOQL to JSON-LD |
| 105 | +const woqlQuery = WOQL.triple('v:Person', 'rdf:type', '@schema:Person'); |
| 106 | +const jsonLD = woqlQuery.json(); |
| 107 | +console.log(JSON.stringify(jsonLD, null, 2)); |
| 108 | + |
| 109 | +// Convert JSON-LD to WOQL |
| 110 | +const fromJSON = new WOQLQuery().json(jsonLD); |
| 111 | +const result = await client.query(fromJSON); |
| 112 | +``` |
| 113 | + |
| 114 | +## Real-World Example: RandomKey Before Client Support |
| 115 | + |
| 116 | +Before the `random_idgen()` method was added to the JavaScript client, you could still use the `RandomKey` feature by passing the JSON-LD directly: |
| 117 | + |
| 118 | +```javascript |
| 119 | +// Using RandomKey with raw JSON-LD (works even without client helper) |
| 120 | +const randomKeyQuery = { |
| 121 | + "@type": "RandomKey", |
| 122 | + "base": { |
| 123 | + "@type": "DataValue", |
| 124 | + "data": { "@type": "xsd:string", "@value": "Person/" } |
| 125 | + }, |
| 126 | + "uri": { |
| 127 | + "@type": "NodeValue", |
| 128 | + "variable": "person_id" |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +const result = await client.query(randomKeyQuery); |
| 133 | +console.log(result.bindings[0].person_id); |
| 134 | +// Output: "Person/aB3dEf9GhI2jK4lM" (random ID generated) |
| 135 | +``` |
| 136 | + |
| 137 | +This is equivalent to using the client helper (once it's available): |
| 138 | + |
| 139 | +```javascript |
| 140 | +const result = await client.query( |
| 141 | + WOQL.random_idgen('Person/', 'v:person_id') |
| 142 | +); |
| 143 | +``` |
| 144 | + |
| 145 | +## Mixing JSON-LD with WOQL Builder |
| 146 | + |
| 147 | +You can embed JSON-LD within WOQL builder queries for hybrid approaches: |
| 148 | + |
| 149 | +```javascript |
| 150 | +const query = WOQL.and( |
| 151 | + // Use builder method |
| 152 | + WOQL.triple('v:Person', 'rdf:type', '@schema:Person'), |
| 153 | + |
| 154 | + // Embed raw JSON-LD for new feature |
| 155 | + { |
| 156 | + "@type": "RandomKey", |
| 157 | + "base": { |
| 158 | + "@type": "DataValue", |
| 159 | + "data": { "@type": "xsd:string", "@value": "Person/" } |
| 160 | + }, |
| 161 | + "uri": { "@type": "NodeValue", "variable": "new_id" } |
| 162 | + } |
| 163 | +); |
| 164 | + |
| 165 | +const result = await client.query(query); |
| 166 | +``` |
| 167 | + |
| 168 | +## Common WOQL JSON-LD Patterns |
| 169 | + |
| 170 | +### Data Values |
| 171 | + |
| 172 | +String, number, and other literal values use `DataValue`: |
| 173 | + |
| 174 | +```json |
| 175 | +{ |
| 176 | + "@type": "DataValue", |
| 177 | + "data": { |
| 178 | + "@type": "xsd:string", |
| 179 | + "@value": "Alice" |
| 180 | + } |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +For numbers: |
| 185 | +```json |
| 186 | +{ |
| 187 | + "@type": "DataValue", |
| 188 | + "data": { |
| 189 | + "@type": "xsd:integer", |
| 190 | + "@value": 42 |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +### Node Values (Variables and IRIs) |
| 196 | + |
| 197 | +Variables: |
| 198 | +```json |
| 199 | +{ |
| 200 | + "@type": "NodeValue", |
| 201 | + "variable": "Person" |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +IRIs/Nodes: |
| 206 | +```json |
| 207 | +{ |
| 208 | + "@type": "NodeValue", |
| 209 | + "node": "@schema:Person" |
| 210 | +} |
| 211 | +``` |
| 212 | + |
| 213 | +### Compound Queries |
| 214 | + |
| 215 | +And: |
| 216 | +```json |
| 217 | +{ |
| 218 | + "@type": "And", |
| 219 | + "and": [ |
| 220 | + { /* query 1 */ }, |
| 221 | + { /* query 2 */ } |
| 222 | + ] |
| 223 | +} |
| 224 | +``` |
| 225 | + |
| 226 | +Or: |
| 227 | +```json |
| 228 | +{ |
| 229 | + "@type": "Or", |
| 230 | + "or": [ |
| 231 | + { /* query 1 */ }, |
| 232 | + { /* query 2 */ } |
| 233 | + ] |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +Select (variable projection): |
| 238 | +```json |
| 239 | +{ |
| 240 | + "@type": "Select", |
| 241 | + "variables": ["Name", "Age"], |
| 242 | + "query": { /* subquery */ } |
| 243 | +} |
| 244 | +``` |
| 245 | + |
| 246 | +## Converting Existing Queries to JSON-LD |
| 247 | + |
| 248 | +To see the JSON-LD for any WOQL query, use the `json()` method: |
| 249 | + |
| 250 | +```javascript |
| 251 | +const woqlQuery = WOQL.select('v:Name', 'v:Age').and( |
| 252 | + WOQL.triple('v:Person', 'rdf:type', '@schema:Person'), |
| 253 | + WOQL.triple('v:Person', '@schema:name', 'v:Name'), |
| 254 | + WOQL.triple('v:Person', '@schema:age', 'v:Age') |
| 255 | +); |
| 256 | + |
| 257 | +// View the JSON-LD |
| 258 | +console.log(JSON.stringify(woqlQuery.json(), null, 2)); |
| 259 | +``` |
| 260 | + |
| 261 | +This outputs the complete JSON-LD structure you can use directly with `client.query()`. |
| 262 | + |
| 263 | +## Tips and Best Practices |
| 264 | + |
| 265 | +### 1. Start with Builder, Convert to JSON-LD |
| 266 | + |
| 267 | +When learning the JSON-LD format, start with the builder syntax and use `json()` to see the structure: |
| 268 | + |
| 269 | +```javascript |
| 270 | +const builderQuery = WOQL.limit(10).triple('v:X', 'v:Y', 'v:Z'); |
| 271 | +console.log(JSON.stringify(builderQuery.json(), null, 2)); |
| 272 | +``` |
| 273 | + |
| 274 | +### 2. Validate JSON-LD Structure |
| 275 | + |
| 276 | +Use the WOQL schema definition to validate your JSON-LD: |
| 277 | +- Server schema: `/path/to/terminusdb/src/terminus-schema/woql.json` |
| 278 | +- Ensures correct `@type` values and required fields |
| 279 | + |
| 280 | +### 3. Handle Variable Names |
| 281 | + |
| 282 | +Variables in JSON-LD don't use the `v:` prefix - just the name: |
| 283 | + |
| 284 | +```json |
| 285 | +{ |
| 286 | + "@type": "NodeValue", |
| 287 | + "variable": "Person" // Not "v:Person" |
| 288 | +} |
| 289 | +``` |
| 290 | + |
| 291 | +But in the builder syntax, you use `'v:Person'` or the `vars()` helper. |
| 292 | + |
| 293 | +### 4. Use for Integration Tests |
| 294 | + |
| 295 | +JSON-LD queries are excellent for integration tests as they're explicit and version-independent: |
| 296 | + |
| 297 | +```javascript |
| 298 | +describe('RandomKey functionality', () => { |
| 299 | + it('generates unique IDs', async () => { |
| 300 | + const query = { |
| 301 | + "@type": "RandomKey", |
| 302 | + "base": { |
| 303 | + "@type": "DataValue", |
| 304 | + "data": { "@type": "xsd:string", "@value": "Test/" } |
| 305 | + }, |
| 306 | + "uri": { "@type": "NodeValue", "variable": "id" } |
| 307 | + }; |
| 308 | + |
| 309 | + const result = await client.query(query); |
| 310 | + expect(result.bindings[0].id).to.include('Test/'); |
| 311 | + }); |
| 312 | +}); |
| 313 | +``` |
| 314 | + |
| 315 | +## Error Handling |
| 316 | + |
| 317 | +When using raw JSON-LD, be aware of common errors: |
| 318 | + |
| 319 | +### Missing Required Fields |
| 320 | + |
| 321 | +```json |
| 322 | +{ |
| 323 | + "@type": "Triple" |
| 324 | + // Error: Missing subject, predicate, object |
| 325 | +} |
| 326 | +``` |
| 327 | + |
| 328 | +### Incorrect Types |
| 329 | + |
| 330 | +```json |
| 331 | +{ |
| 332 | + "@type": "TriplePattern", // Error: Should be "Triple" |
| 333 | + // ... |
| 334 | +} |
| 335 | +``` |
| 336 | + |
| 337 | +### Invalid Variable/Node Specification |
| 338 | + |
| 339 | +```json |
| 340 | +{ |
| 341 | + "@type": "NodeValue", |
| 342 | + "var": "Person" // Error: Should be "variable" not "var" |
| 343 | +} |
| 344 | +``` |
| 345 | + |
| 346 | +The server will return detailed error messages indicating which field is problematic. |
| 347 | + |
| 348 | +## Finding JSON-LD Examples |
| 349 | + |
| 350 | +Several sources provide JSON-LD query examples: |
| 351 | + |
| 352 | +1. **Client Test Suites**: Look at test files in `terminusdb-client-js/test/woqlJson/` |
| 353 | +2. **Server Tests**: Check `terminusdb/tests/test/` for integration test examples |
| 354 | +3. **WOQL Schema**: Review `terminusdb/src/terminus-schema/woql.json` for all query types |
| 355 | +4. **Use json() Method**: Convert any builder query to see its JSON-LD structure |
| 356 | + |
| 357 | +## Related Documentation |
| 358 | + |
| 359 | +- [WOQL Basics](/docs/woql-basics/) - Learn the WOQL builder API |
| 360 | +- [WOQL Explanation](/docs/woql-explanation/) - Understanding WOQL and JSON-LD |
| 361 | +- [JavaScript Client Reference](/docs/javascript/) - Complete API reference |
| 362 | +- [WOQL Schema Reference](/docs/woql-class-reference-guide/) - WOQL JSON-LD specification |
| 363 | + |
| 364 | +## Summary |
| 365 | + |
| 366 | +Raw JSON-LD queries provide a powerful way to: |
| 367 | +- Access new server features immediately |
| 368 | +- Debug and understand query structures |
| 369 | +- Create integration tests |
| 370 | +- Port queries between different clients |
| 371 | + |
| 372 | +While the WOQL builder API is more convenient for everyday use, understanding and using JSON-LD directly gives you full control and enables early adoption of new TerminusDB features. |
0 commit comments