SharpGraph supports all GraphQL relationship patterns:
type User {
posts: [Post] # User has many posts
}
type Post {
author: User # Post belongs to one user
}type User {
friends: [User] # Users can have many friends
}type Category {
parent: Category # Category has one parent
children: [Category] # Category has many children
}Automatic Relationship Detection:
The schema parser automatically detects relationships and creates foreign key fields:
type Post {
author: User # Creates: authorId: ID
tags: [Tag] # Creates: tagsIds: [ID]
}Automatic Foreign Key Generation:
{
"Post": [
{
"id": "post1",
"title": "Hello World",
"authorId": "user1", // Many-to-one
"tagsIds": ["tag1", "tag2"] // Many-to-many
}
]
}Lazy Loading with Relationship Traversal:
{
posts {
title
author { # Automatically resolved via authorId
name
email
}
tags { # Automatically resolved via tagsIds
name
}
}
}Resolution Process:
- Query parser detects relationship fields
- Executor loads primary records
- Collects foreign key IDs
- Batch loads related records
- Projects requested fields
Resolution Algorithm:
- Parse query - identify relationship fields
- Load primary records - execute base query
- Collect foreign keys - extract IDs from primary records
- Batch load related - single query for all related records
- Map relationships - connect records via foreign keys
- Project fields - return only requested fields
Optimization Strategies:
- Foreign key batching reduces N+1 queries
- Index usage for efficient lookups
- Field projection minimizes data transfer
- Lazy loading prevents over-fetching