A comprehensive Star Wars database implementation using SharpGraph, based on the official GraphQL Star Wars schema from graphql.org.
This example demonstrates a complete GraphQL database with:
- 30+ Characters (Humans and Droids)
- 3 Films (Episodes IV, V, VI - Original Trilogy)
- 9 Planets (Tatooine, Alderaan, Yavin IV, etc.)
- 8 Species (Human, Droid, Wookiee, etc.)
- 7 Starships (X-wing, Millennium Falcon, TIE Fighter, etc.)
- 5 Vehicles (AT-AT, AT-ST, Speeder Bike, etc.)
- Complex Relationships between all entities
type Character {
id: ID!
name: String!
appearsIn: [String]!
characterType: String! # "Human" or "Droid"
# Human-specific
homePlanet: Planet
height: Float
mass: Float
hairColor: String
skinColor: String
eyeColor: String
birthYear: String
# Droid-specific
primaryFunction: String
# Relationships
friends: [Character]
films: [Film]
starships: [Starship]
vehicles: [Vehicle]
}type Film {
id: ID!
title: String!
episodeId: Int!
openingCrawl: String!
director: String!
producer: String!
releaseDate: String!
# Relationships
characters: [Character]
planets: [Planet]
starships: [Starship]
vehicles: [Vehicle]
species: [Species]
}type Planet {
id: ID!
name: String!
diameter: String
rotationPeriod: String
orbitalPeriod: String
gravity: String
population: String
climate: String
terrain: String
surfaceWater: String
# Relationships
residents: [Character]
films: [Film]
}type Starship {
id: ID!
name: String!
model: String
starshipClass: String
manufacturer: String
costInCredits: String
length: String
crew: String
passengers: String
maxAtmospheringSpeed: String
hyperdriveRating: String
MGLT: String
cargoCapacity: String
consumables: String
# Relationships
pilots: [Character]
films: [Film]
}type Vehicle {
id: ID!
name: String!
model: String
vehicleClass: String
manufacturer: String
costInCredits: String
length: String
crew: String
passengers: String
maxAtmospheringSpeed: String
cargoCapacity: String
consumables: String
# Relationships
pilots: [Character]
films: [Film]
}type Species {
id: ID!
name: String!
classification: String
designation: String
averageHeight: String
averageLifespan: String
eyeColors: String
hairColors: String
skinColors: String
language: String
# Relationships
homePlanet: Planet
people: [Character]
films: [Film]
}using SharpGraph.Examples;
// Initialize the database (auto-populates with Star Wars data)
var db = new StarWarsDatabase("starwars_db");
// Execute queries
var result = db.Query(@"
{
character(id: ""luke"") {
name
height
friends {
name
}
}
}
");
Console.WriteLine(result.RootElement.GetRawText());cd examples
dotnet run --project StarWarsDemo.csproj{
character(id: "luke") {
name
height
mass
hairColor
eyeColor
birthYear
friends {
name
characterType
}
}
}Response:
{
"data": {
"character": {
"name": "Luke Skywalker",
"height": 172,
"mass": 77,
"hairColor": "blond",
"eyeColor": "blue",
"birthYear": "19BBY",
"friends": [
{ "name": "Han Solo", "characterType": "Human" },
{ "name": "Leia Organa", "characterType": "Human" },
{ "name": "C-3PO", "characterType": "Droid" },
{ "name": "R2-D2", "characterType": "Droid" }
]
}
}
}{
character(id: "r2d2") {
name
characterType
primaryFunction
height
mass
eyeColor
appearsIn
}
}Response:
{
"data": {
"character": {
"name": "R2-D2",
"characterType": "Droid",
"primaryFunction": "Astromech",
"height": 96,
"mass": 32,
"eyeColor": "red",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"]
}
}
}{
films {
title
episodeId
director
releaseDate
openingCrawl
}
}Response:
{
"data": {
"films": [
{
"title": "A New Hope",
"episodeId": 4,
"director": "George Lucas",
"releaseDate": "1977-05-25",
"openingCrawl": "It is a period of civil war..."
},
{
"title": "The Empire Strikes Back",
"episodeId": 5,
"director": "Irvin Kershner",
"releaseDate": "1980-05-17",
"openingCrawl": "It is a dark time for the Rebellion..."
},
{
"title": "Return of the Jedi",
"episodeId": 6,
"director": "Richard Marquand",
"releaseDate": "1983-05-25",
"openingCrawl": "Luke Skywalker has returned..."
}
]
}
}{
planets {
name
climate
terrain
population
diameter
}
}Response:
{
"data": {
"planets": [
{
"name": "Tatooine",
"climate": "arid",
"terrain": "desert",
"population": "200000",
"diameter": "10465"
},
{
"name": "Alderaan",
"climate": "temperate",
"terrain": "grasslands, mountains",
"population": "2000000000",
"diameter": "12500"
}
]
}
}{
starships {
name
model
starshipClass
manufacturer
length
crew
hyperdriveRating
}
}Response:
{
"data": {
"starships": [
{
"name": "X-wing",
"model": "T-65 X-wing",
"starshipClass": "Starfighter",
"manufacturer": "Incom Corporation",
"length": "12.5",
"crew": "1",
"hyperdriveRating": "1.0"
},
{
"name": "Millennium Falcon",
"model": "YT-1300 light freighter",
"starshipClass": "Light freighter",
"manufacturer": "Corellian Engineering Corporation",
"length": "34.37",
"crew": "4",
"hyperdriveRating": "0.5"
}
]
}
}{
character(id: "vader") {
name
characterType
height
mass
eyeColor
birthYear
appearsIn
}
}Response:
{
"data": {
"character": {
"name": "Darth Vader",
"characterType": "Human",
"height": 202,
"mass": 136,
"eyeColor": "yellow",
"birthYear": "41.9BBY",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"]
}
}
}{
character(id: "han") {
name
characterType
height
mass
hairColor
eyeColor
birthYear
friends {
name
characterType
}
}
}Response:
{
"data": {
"character": {
"name": "Han Solo",
"characterType": "Human",
"height": 180,
"mass": 80,
"hairColor": "brown",
"eyeColor": "brown",
"birthYear": "29BBY",
"friends": [
{ "name": "Luke Skywalker", "characterType": "Human" },
{ "name": "Leia Organa", "characterType": "Human" },
{ "name": "R2-D2", "characterType": "Droid" },
{ "name": "Chewbacca", "characterType": "Human" }
]
}
}
}mutation {
createCharacter(input: {
name: "Rey"
characterType: "Human"
appearsIn: ["NEWHOPE"]
height: 170
mass: 54
hairColor: "brown"
eyeColor: "hazel"
birthYear: "15ABY"
}) {
id
name
characterType
height
}
}Response:
{
"data": {
"createCharacter": {
"id": "auto_1760976789123",
"name": "Rey",
"characterType": "Human",
"height": 170
}
}
}{
characters {
name
characterType
appearsIn
}
}{
species {
name
classification
designation
averageHeight
language
}
}- Characters (polymorphic Human/Droid)
- Films
- Planets
- Species
- Starships
- Vehicles
- Many-to-Many: Characters ↔ Friends
- Many-to-Many: Characters ↔ Films
- Many-to-Many: Characters ↔ Starships
- Many-to-One: Character → Home Planet
- One-to-Many: Planet → Residents
- Characters can be Humans or Droids
- Different fields based on type
characterTypediscriminator field
- Detailed physical attributes
- Historical data (birth years, release dates)
- Technical specifications (starship specs)
- Queries (single and list)
- Mutations (create new entities)
- Nested field resolution
- Relationship traversal
After initialization, you'll find:
starwars_db/
├── Character.tbl (~30+ characters)
├── Film.tbl (3 films)
├── Planet.tbl (9 planets)
├── Species.tbl (8 species)
├── Starship.tbl (7 starships)
└── Vehicle.tbl (5 vehicles)
- Luke Skywalker
- Leia Organa
- Han Solo
- Obi-Wan Kenobi
- Yoda
- Chewbacca
- C-3PO
- R2-D2
- IG-88
- R5-D4
- Darth Vader
- Emperor Palpatine
- Jabba the Hutt
- Boba Fett
- Greedo
- Wedge Antilles
- Lando Calrissian
- Wicket W. Warrick
- Admiral Ackbar
- Mon Mothma
- Uses MessagePack for binary serialization
- Schema-optimized storage (40% smaller than JSON)
- Automatic type mapping from GraphQL to storage
- Automatic foreign key lookups
- Lazy loading of related entities
- Recursive field projection
- Validation errors in
errorsarray - Partial data with errors support
- GraphQL-compliant error format
The database uses:
- In-memory caching (MemTable)
- Page-based storage (4KB pages)
- Binary serialization (MessagePack)
- Schema-optimized records (no field name repetition)
For production use with large datasets:
- Add B-tree indexes on foreign keys
- Implement batch loading for N+1 queries
- Use pagination for large result sets
Based on the official Star Wars GraphQL schema example from GraphQL.org.
Data sourced from the Star Wars universe, created by George Lucas.
Example code is part of SharpGraph project. Star Wars characters, names, and references are trademarks of Lucasfilm Ltd.
🌟 May the Force be with you! 🌟