GraphQL Queries
Hugr automatically generates a comprehensive GraphQL API from your schema definitions, providing powerful query capabilities for data access and manipulation. This section covers all aspects of querying data through the generated GraphQL API.
What Gets Generated
For each data object (table or view) defined in your schema, Hugr automatically creates:
- Basic queries - Select multiple records with filtering, sorting, and pagination
- Primary key queries - Retrieve single records by primary key (
<object>_by_pk) - Unique constraint queries - Retrieve single records by unique fields (
<object>_by_<field>) - Aggregation queries - Single-row aggregations (
<object>_aggregation) - Bucket aggregations - Grouped aggregations (
<object>_bucket_aggregation) - Relation fields - Access related data through foreign keys
- Function fields - Execute functions with automatic argument mapping
Query Organization
All queries are organized in a hierarchical module structure based on the @module directive. Data objects, aggregations, and functions can be nested within modules at any level.
query {
# Root level objects
customers { id name }
customers_aggregation { _rows_count }
customers_by_pk(id: 1) { id name }
# Nested modules - data objects inside modules
crm {
analytics {
# All query types available within modules
top_customers { id total_spent }
top_customers_aggregation { _rows_count }
top_customers_bucket_aggregation {
key { segment }
aggregations { _rows_count }
}
}
}
# Functions organized in modules
function {
services {
weather {
current_weather(lat: 40.7, lon: -74.0) {
temp
}
}
}
}
}
Module structure:
- Defined using
@module(name: "module.submodule")directive - Can nest data objects at any depth
- All query types (
<object>,<object>_by_pk,<object>_aggregation,<object>_bucket_aggregation) available within modules - Functions organized under
functionfield can also be modularized
Core Query Capabilities
1. Function Calls
Execute database functions, HTTP API calls, and custom business logic directly in queries.
2. Basic Queries
Retrieve records by primary key or unique constraints with simple, direct queries.
3. Filtering
Apply complex filters using boolean logic, relation filters, and nested conditions.
4. Sorting & Pagination
Order results and implement pagination with limit/offset and distinct operations.
Learn about Sorting & Pagination →
5. Relations
Query related data through foreign keys with nested queries and aggregations.
6. Function Fields
Embed function calls as fields in data objects with automatic argument mapping.
7. Aggregations
Perform single-row and grouped aggregations with support for nested aggregations.
8. Dynamic Joins
Create ad-hoc joins at query time using the _join field, including cross-source joins.
9. Spatial Queries
Query geographically related data using spatial relationships and operations.
10. H3 Hexagonal Clustering
Aggregate and cluster spatial data using the H3 hexagonal grid system for advanced geospatial analysis.
11. Vector Similarity Search
Perform semantic search using vector embeddings with support for multiple distance metrics and hybrid search.
12. Generated Fields & Transformations
Use automatically generated fields and transformation arguments for Timestamp, JSON, and Geometry data types.
Learn about Generated Fields →
13. Cube & Hypertable Queries
Query OLAP cubes and time-series hypertables with measurement aggregation and time-based bucketing.
Learn about Cube & Hypertable Queries →
Query Arguments Reference
Most query types support these standard arguments:
| Argument | Type | Description |
|---|---|---|
filter | <object>_filter | Filter conditions for records |
order_by | [OrderByField] | Sort order (can reference nested fields) |
limit | Int | Maximum number of records to return |
offset | Int | Number of records to skip |
distinct_on | [String] | Fields to use for DISTINCT |
args | <input_type> | Arguments for parameterized views |
inner | Boolean | Use INNER join (excludes parent records without matches, default: false) |
Nested Query Arguments
For relation fields (subqueries), additional arguments control post-join behavior:
| Argument | Type | Description |
|---|---|---|
nested_order_by | [OrderByField] | Sort order applied after join |
nested_limit | Int | Limit applied after join |
nested_offset | Int | Offset applied after join |
Common Patterns
Basic Data Retrieval
query {
customers(
filter: { country: { eq: "USA" } }
order_by: [{ field: "name", direction: ASC }]
limit: 10
) {
id
name
email
}
}
Data with Relations
query {
customers {
id
name
orders(
filter: { status: { eq: "pending" } }
limit: 5
) {
id
total
}
}
}
Aggregations
query {
orders_bucket_aggregation {
key {
status
}
aggregations {
_rows_count
total {
sum
avg
}
}
}
}
Function Calls
query {
function {
weather {
current_weather(lat: 40.7, lon: -74.0) {
temp
humidity
}
}
}
}
Performance Best Practices
- Use filters early - Apply filters at the highest level possible to reduce data volume
- Limit nested queries - Always use
limitornested_limitfor one-to-many relations - Leverage indexes - Ensure indexes exist on filtered and sorted fields
- Aggregate when possible - Use aggregation queries instead of fetching raw data
- Batch related data - Use subqueries instead of separate queries to avoid N+1 problems
Next Steps
Start with Function Calls to learn how to execute functions, or jump to Basic Queries for fundamental data retrieval patterns.