Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/sparql-client. jsr.io/@okikio/sparql
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137/** * Basic Query Example - Updated for narrative.ttl ontology */
import { node, select, variable, type ExecutionConfig,} from '../mod.ts'
import { RDFS } from '../namespaces.ts'
const NARRATIVE = 'http://knowledge.graph/ontology/narrative#'
const config: ExecutionConfig = { endpoint: 'http://localhost:9999/blazegraph/sparql', timeoutMs: 30000,}
// ============================================================================// Example 1: Find Creators (People)// ============================================================================
async function findCreators() { console.log('\n=== Finding creators ===\n')
const person = node('person', 'narrative:Person') .with.prop('narrative:knownAs', variable('name'))
const query = select(['?person', '?name']) .prefix('narrative', NARRATIVE) .where(person) .orderBy('?name') .limit(10)
console.log(query.build().value)
try { const result = await query.execute(config) console.log('Results:', result.results.bindings) } catch (e) { console.error('Query failed:', e) }}
// ============================================================================// Example 2: Find Publishers (Organizations)// ============================================================================
async function findPublishers() { console.log('\n=== Finding publishers ===\n')
const org = node('org', 'narrative:Org') .with.prop('rdfs:label', variable('name')) .and.prop('narrative:orgType', variable('type'))
const query = select(['?name', '?type']) .prefix('narrative', NARRATIVE) .prefix('rdfs', RDFS._namespace) .where(org) .orderBy('?name') .limit(10)
console.log(query.build().value)
try { const result = await query.execute(config) console.log('Results:', result.results.bindings) } catch (e) { console.error('Query failed:', e) }}
// ============================================================================// Example 3: Find Comics (StoryExpressions) with Series info// ============================================================================
async function findComics() { console.log('\n=== Finding comics ===\n')
const issue = node('issue', 'narrative:StoryExpression') .with.prop('narrative:issueNumber', variable('number')) .and.prop('narrative:coverDate', variable('date'))
const query = select(['?issue', '?number', '?date']) .prefix('narrative', NARRATIVE) .where(issue) .orderBy('?date', 'DESC') .limit(10)
console.log(query.build().value)
try { const result = await query.execute(config) console.log('Results:', result.results.bindings) } catch (e) { console.error('Query failed:', e) }}
// ============================================================================// Example 4: Find Characters// ============================================================================
async function findCharacters() { console.log('\n=== Finding characters ===\n')
const character = node('char', 'narrative:Character') .with.prop('narrative:characterName', variable('name'))
const query = select(['?char', '?name']) .prefix('narrative', NARRATIVE) .where(character) .orderBy('?name') .limit(20)
console.log(query.build().value)
try { const result = await query.execute(config) console.log('Results:', result.results.bindings) } catch (e) { console.error('Query failed:', e) }}
// ============================================================================// Run examples// ============================================================================
if (import.meta.main) { await findCreators() await findPublishers() await findComics() await findCharacters()}