Query Best Practices
Query Best Practices
This guide provides best practices, performance optimization strategies, and recommendations for querying data in ObjectQL across different query interfaces (JSON-DSL, REST, GraphQL).
1. Overview of Query Approaches
ObjectQL provides three distinct query interfaces, each optimized for different scenarios:
| Approach | Best For | Complexity | Performance | AI-Friendly |
|---|---|---|---|---|
| JSON-DSL (Core) | Server-side logic, AI agents | Medium | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| REST API | Simple CRUD, mobile apps | Low | ⭐⭐⭐⭐ | ⭐⭐⭐ |
| GraphQL | Complex data graphs, modern SPAs | High | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
Rating scale: ⭐ = lowest, ⭐⭐⭐⭐⭐ = highest
2. JSON-DSL Query Protocol (Recommended Default)
What It Is
The JSON-DSL is ObjectQL's core query language - a structured JSON representation that serves as an Abstract Syntax Tree (AST) for data operations.
When to Use
✅ Perfect for:
- Server-side business logic and hooks
- AI-generated queries (hallucination-proof)
- Cross-driver compatibility (SQL, MongoDB, Remote)
- Complex filtering with nested logic
- Programmatic query construction
Basic Syntax
Optimization Strategies
✅ DO: Use Field Projection
Bad:
Good:
Impact: Reduces payload size by 60-80% for objects with many fields.
✅ DO: Use Indexed Fields in Filters
Bad:
Good:
Impact: Can improve query speed by 10-100x depending on dataset size.
✅ DO: Limit and Paginate Large Result Sets
Bad:
Good:
Impact: Prevents memory exhaustion and ensures consistent response times.
✅ DO: Use Expand Instead of Multiple Queries
Bad:
Good:
Impact: Reduces latency by 50-90% by eliminating N+1 query problem.
3. REST API Interface
What It Is
A traditional REST-style HTTP API following standard conventions (GET, POST, PUT, DELETE).
When to Use
✅ Perfect for:
- Simple CRUD operations
- Mobile apps with limited query needs
- Third-party integrations expecting REST
- Quick prototypes and MVPs
- Developers familiar with REST conventions
Basic Usage
Optimization Strategies
✅ DO: Use Query String Compression for Complex Filters
Standard:
Optimized (URL-encoded JSON):
✅ DO: Leverage HTTP Caching
Impact: Can eliminate 70-90% of repeated queries for read-heavy endpoints.
❌ DON'T: Over-fetch Data
Bad:
Good:
4. GraphQL Interface
What It Is
A flexible query language that allows clients to request exactly the data they need, including nested relationships, in a single request.
When to Use
✅ Perfect for:
- Modern SPAs with complex data requirements
- Multi-table data fetching in one request
- Real-time applications (with subscriptions)
- Developer tools with introspection needs
- Mobile apps with bandwidth constraints
Basic Usage
Optimization Strategies
✅ DO: Request Only Needed Fields
Bad:
Good:
Impact: Reduces payload size by 70-90% for wide tables.
✅ DO: Use Fragments for Reusable Field Sets
Bad (Repetitive):
Good (DRY):
Impact: Improves maintainability and reduces duplication.
✅ DO: Batch Multiple Queries
Bad (Multiple HTTP Requests):
Good (Single Request):
Impact: Reduces latency by 60-80% by eliminating round trips.
✅ DO: Implement DataLoader for Batch Resolution
When building custom resolvers, use DataLoader pattern to batch database queries:
5. Query Approach Comparison
Scenario 1: Simple CRUD Operation
Use Case: Create a new user account
Recommendation: REST API
Why: Simplest approach, standard conventions, no overhead.
Scenario 2: Complex Dashboard with Multiple Data Sources
Use Case: Dashboard showing tasks, projects, and team members with relationships
Recommendation: GraphQL
Why: Single request, precise field selection, handles nested data elegantly.
Scenario 3: Server-Side Business Logic
Use Case: Automated workflow to assign tasks based on workload
Recommendation: JSON-DSL
Why: Type-safe, driver-agnostic, programmatic composition.
Scenario 4: AI-Generated Query
Use Case: LLM generates query from natural language: "Show me overdue high-priority tasks"
Recommendation: JSON-DSL
Why: Structured format prevents hallucination, validates automatically.
Why NOT SQL strings:
Example of AI hallucination:
6. Advanced Optimization Techniques
6.1 Use Aggregation for Analytics
Bad (Application-level aggregation):
Good (Database-level aggregation):
Impact: 100-1000x faster for large datasets.
6.2 Use Distinct for Unique Values
Bad:
Good:
Impact: Reduces data transfer by 90%+ for high-duplication fields.
6.3 Use Proper Indexing
Impact: Queries with indexed filters are 10-100x faster.
6.4 Avoid OR Filters When Possible
Bad (OR requires multiple index scans):
Good (IN uses single index scan):
Impact: 2-5x faster for large tables.
6.5 Use Cursor-Based Pagination for Large Datasets
Bad (Offset pagination gets slower with large offsets):
Good (Cursor pagination using last ID):
Impact: Consistent performance regardless of dataset size.
7. Performance Best Practices Summary
| Practice | Impact | Difficulty |
|---|---|---|
| Use field projection | High | Easy |
| Add indexes to filtered/sorted fields | Very High | Medium |
| Use aggregation for analytics | Very High | Easy |
| Eliminate N+1 queries with expand | Very High | Easy |
| Implement pagination | High | Easy |
| Use cursor-based pagination for large sets | High | Medium |
Use in operator instead of multiple or | Medium | Easy |
| Batch queries in GraphQL | High | Easy |
Use distinct for unique values | High | Easy |
| Enable HTTP caching for REST | High | Medium |
8. Choosing the Right Approach: Decision Tree
9. Migration Path
If you're currently using one approach and want to switch:
REST → JSON-DSL
Before:
After:
JSON-DSL → GraphQL
Before:
After:
10. Conclusion
Key Takeaways:
-
JSON-DSL is the universal core - use it for server-side logic, AI integration, and cross-driver compatibility.
-
GraphQL excels at complex data requirements with nested relationships and is ideal for modern frontends.
-
REST is perfect for simple CRUD operations and third-party integrations.
-
Optimization matters more than the interface - focus on indexing, field projection, and pagination regardless of which approach you use.
-
You can mix approaches - use GraphQL for the frontend dashboard and JSON-DSL for backend workflows.
Recommended Default Stack:
- Server-side: JSON-DSL (type-safe, driver-agnostic)
- Client-side (complex): GraphQL (efficient, flexible)
- Client-side (simple): REST (fast, familiar)
- AI Integration: JSON-DSL (hallucination-proof)
11. Further Reading
- Query Language Specification - Complete JSON-DSL reference
- Querying Guide - Step-by-step query examples
- GraphQL API Documentation - GraphQL setup and usage
- REST API Documentation - REST endpoint reference
Need Help?