---
title: Your First Knowledge Graph
description: Build a complete knowledge graph step-by-step in 15 minutes
---

# Your First Knowledge Graph

In this tutorial, you'll build a knowledge graph representing people, companies, and their relationships. By the end, you'll know how to create, query, and visualize RDF data.

## What You'll Build

A knowledge graph with:
- 3 people (Alice, Bob, Carol)
- 2 companies (TechCorp, DataInc)
- Relationships (works for, knows, manages)

## Step 1: Setup

Create a new project:

```bash
mkdir my-knowledge-graph
cd my-knowledge-graph
npm init -y
npm install @unrdf/core @unrdf/oxigraph
```

Create `index.mjs`:

```javascript
import { createStore, DataFactory } from '@unrdf/oxigraph'

const { namedNode, literal, quad } = DataFactory
const store = createStore()

console.log('Knowledge graph initialized!')
```

Test it:

```bash
node index.mjs
# → Knowledge graph initialized!
```

✅ **Checkpoint**: You have UNRDF installed and running.

## Step 2: Add People

Add three people to your graph:

```javascript
const ex = (name) => namedNode(`http://example.org/${name}`)
const foaf = (prop) => namedNode(`http://xmlns.com/foaf/0.1/${prop}`)

// Alice
store.add(quad(
  ex('alice'),
  foaf('name'),
  literal('Alice Smith')
))

store.add(quad(
  ex('alice'),
  foaf('mbox'),
  namedNode('mailto:alice@example.org')
))

// Bob
store.add(quad(
  ex('bob'),
  foaf('name'),
  literal('Bob Johnson')
))

// Carol
store.add(quad(
  ex('carol'),
  foaf('name'),
  literal('Carol Williams')
))

console.log(`Graph size: ${store.size} triples`)
```

✅ **Checkpoint**: You can add data to the graph.

## Step 3: Add Companies

```javascript
const rdf = (prop) => namedNode(`http://www.w3.org/1999/02/22-rdf-syntax-ns#${prop}`)

// TechCorp
store.add(quad(
  ex('techcorp'),
  rdf('type'),
  ex('Company')
))

store.add(quad(
  ex('techcorp'),
  ex('name'),
  literal('TechCorp')
))

// DataInc
store.add(quad(
  ex('datainc'),
  rdf('type'),
  ex('Company')
))

store.add(quad(
  ex('datainc'),
  ex('name'),
  literal('DataInc')
))
```

## Step 4: Add Relationships

Connect people to companies:

```javascript
// Alice works for TechCorp
store.add(quad(
  ex('alice'),
  ex('worksFor'),
  ex('techcorp')
))

// Bob works for TechCorp
store.add(quad(
  ex('bob'),
  ex('worksFor'),
  ex('techcorp')
))

// Carol works for DataInc
store.add(quad(
  ex('carol'),
  ex('worksFor'),
  ex('datainc')
))

// Alice knows Bob
store.add(quad(
  ex('alice'),
  foaf('knows'),
  ex('bob')
))

// Alice manages Carol
store.add(quad(
  ex('alice'),
  ex('manages'),
  ex('carol')
))
```

✅ **Checkpoint**: Your graph has entities and relationships.

## Step 5: Query the Graph

Find all people who work for TechCorp:

```javascript
const query = `
  PREFIX ex: <http://example.org/>
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>

  SELECT ?name WHERE {
    ?person ex:worksFor ex:techcorp .
    ?person foaf:name ?name .
  }
`

const results = store.query(query)

console.log('\\nTechCorp employees:')
for (const binding of results) {
  console.log('- ', binding.get('name').value)
}
```

Output:
```
TechCorp employees:
-  Alice Smith
-  Bob Johnson
```

## Step 6: Traverse Relationships

Find Alice's colleagues (people who work at the same company):

```javascript
const colleaguesQuery = `
  PREFIX ex: <http://example.org/>
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>

  SELECT ?colleagueName WHERE {
    ex:alice ex:worksFor ?company .
    ?colleague ex:worksFor ?company .
    ?colleague foaf:name ?colleagueName .
    FILTER(?colleague != ex:alice)
  }
`

const colleagues = store.query(colleaguesQuery)

console.log("\\nAlice's colleagues:")
for (const binding of colleagues) {
  console.log('- ', binding.get('colleagueName').value)
}
```

✅ **Checkpoint**: You can query relationships.

## Step 7: Save Your Graph

Export to Turtle format:

```javascript
import { writeFile } from 'node:fs/promises'

const turtle = store.dump({ format: 'text/turtle' })
await writeFile('knowledge-graph.ttl', turtle)

console.log('\\nSaved to knowledge-graph.ttl')
```

## Complete Code

<details>
<summary>**View full code**</summary>

```javascript
import { createStore, DataFactory } from '@unrdf/oxigraph'
import { writeFile } from 'node:fs/promises'

const { namedNode, literal, quad } = DataFactory
const store = createStore()

// Helper functions
const ex = (name) => namedNode(`http://example.org/${name}`)
const foaf = (prop) => namedNode(`http://xmlns.com/foaf/0.1/${prop}`)
const rdf = (prop) => namedNode(`http://www.w3.org/1999/02/22-rdf-syntax-ns#${prop}`)

// Add people
store.add(quad(ex('alice'), foaf('name'), literal('Alice Smith')))
store.add(quad(ex('alice'), foaf('mbox'), namedNode('mailto:alice@example.org')))
store.add(quad(ex('bob'), foaf('name'), literal('Bob Johnson')))
store.add(quad(ex('carol'), foaf('name'), literal('Carol Williams')))

// Add companies
store.add(quad(ex('techcorp'), rdf('type'), ex('Company')))
store.add(quad(ex('techcorp'), ex('name'), literal('TechCorp')))
store.add(quad(ex('datainc'), rdf('type'), ex('Company')))
store.add(quad(ex('datainc'), ex('name'), literal('DataInc')))

// Add relationships
store.add(quad(ex('alice'), ex('worksFor'), ex('techcorp')))
store.add(quad(ex('bob'), ex('worksFor'), ex('techcorp')))
store.add(quad(ex('carol'), ex('worksFor'), ex('datainc')))
store.add(quad(ex('alice'), foaf('knows'), ex('bob')))
store.add(quad(ex('alice'), ex('manages'), ex('carol')))

console.log(`Graph size: ${store.size} triples`)

// Query
const query = `
  PREFIX ex: <http://example.org/>
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  SELECT ?name WHERE {
    ?person ex:worksFor ex:techcorp .
    ?person foaf:name ?name .
  }
`

console.log('\\nTechCorp employees:')
for (const binding of store.query(query)) {
  console.log('- ', binding.get('name').value)
}

// Save
const turtle = store.dump({ format: 'text/turtle' })
await writeFile('knowledge-graph.ttl', turtle)
console.log('\\nSaved to knowledge-graph.ttl')
```

</details>

## What You Learned

- ✅ Creating RDF triples with subjects, predicates, objects
- ✅ Using standard vocabularies (FOAF, RDF)
- ✅ Querying with SPARQL
- ✅ Traversing relationships
- ✅ Saving graphs to files

## Next Steps

- 📚 [Tutorial: Using Hooks](/tutorials/using-hooks) - Add reactivity
- 🛠️ [How-To: Query RDF Data](/how-to/query-rdf) - Advanced queries
- 💡 [Explanation: RDF Fundamentals](/explanation/rdf-fundamentals) - Deeper concepts
