GraphQL API
GraphQL API
ObjectQL provides a GraphQL interface for flexible, efficient queries with complex multi-table relationships. GraphQL allows clients to request exactly the data they need in a single request, making it ideal for modern frontends with complex data requirements.
Overview
The GraphQL API provides:
- Strongly-typed schema automatically generated from ObjectQL metadata
- Single endpoint for all queries and mutations
- Efficient data fetching with precise field selection
- Real-time introspection for developer tools
- Standards-compliant GraphQL implementation
Endpoint
Both GET and POST methods are supported:
- POST: Send queries in request body (recommended for most cases)
- GET: Send queries via URL parameters (useful for simple queries and caching)
Getting Started
Installation
The GraphQL adapter is included in @objectql/server:
Basic Query Example
Response:
Schema Generation
The GraphQL schema is automatically generated from your ObjectQL metadata. Each object definition creates:
- Output Type: For query results (e.g.,
User,Task) - Input Type: For mutations (e.g.,
UserInput,TaskInput) - Query Fields: For fetching data (e.g.,
user(id),userList()) - Mutation Fields: For creating/updating/deleting data
Example Object Definition
Generated GraphQL Types:
Queries
Fetch Single Record
Query a single record by ID:
Fetch Multiple Records
Query multiple records with optional filtering and pagination:
Available Arguments:
limit(Int): Maximum number of records to returnskip(Int): Number of records to skip (for pagination)filters(String): JSON-encoded filter expressionfields(List): Specific fields to includesort(String): JSON-encoded sort specification
Advanced Filtering
Use the filters argument with JSON-encoded filter expressions:
Sorting
Use the sort argument with JSON-encoded sort specification:
Field Selection
GraphQL's field selection naturally limits the data returned:
Mutations
Create Record
Response:
Update Record
Delete Record
Response:
Variables
GraphQL variables provide a cleaner way to pass dynamic values:
Query with Variables
Variables:
Request (POST):
Mutation with Variables
Variables:
GET Requests
For simple queries, you can use GET requests with URL parameters:
With variables:
Note: GET requests are useful for:
- Simple queries that can be cached
- Direct browser testing
- Debugging and development
For complex queries or mutations, use POST requests.
Error Handling
GraphQL Errors
Errors follow the GraphQL specification:
Validation Errors
Not Found
Type Mapping
ObjectQL field types are mapped to GraphQL types:
| ObjectQL Type | GraphQL Type | Notes |
|---|---|---|
text, textarea, email, url, phone | String | Text fields |
number, currency, percent | Float | Numeric fields |
auto_number | Int | Integer fields |
boolean | Boolean | True/false |
date, datetime, time | String | ISO 8601 format |
select | String | String enum values |
lookup, master_detail | String | Reference by ID |
file, image | String | File metadata as JSON |
object, json | String | JSON as string |
Required Fields
Fields marked as required: true in ObjectQL become non-nullable (!) in GraphQL:
Introspection
GraphQL provides built-in introspection for schema discovery:
Get All Types
Get Type Details
Available Operations
Client Integration
JavaScript/TypeScript
Apollo Client
React with Apollo
Development Tools
GraphQL Playground
ObjectQL doesn't include GraphQL Playground by default, but you can easily add it:
Postman
Import the GraphQL schema into Postman for testing:
- Create a new GraphQL request
- Point to
/api/graphql - Use the introspection feature to load the schema
Best Practices
1. Request Only What You Need
❌ Don't request all fields:
✅ Do request specific fields:
2. Use Variables for Dynamic Values
❌ Don't embed values in queries:
✅ Do use variables:
3. Use Fragments for Reusability
4. Implement Pagination
5. Handle Errors Gracefully
Comparison with Other APIs
GraphQL vs REST
| Feature | GraphQL | REST |
|---|---|---|
| Endpoint | Single endpoint | Multiple endpoints |
| Data Fetching | Precise field selection | Fixed response structure |
| Multiple Resources | Single request | Multiple requests |
| Over-fetching | No | Common |
| Under-fetching | No | Common |
| Versioning | Schema evolution | URL versioning |
| Caching | More complex | Simple (HTTP) |
GraphQL vs JSON-RPC
| Feature | GraphQL | JSON-RPC |
|---|---|---|
| Type System | Strongly typed | Flexible |
| Introspection | Built-in | Not available |
| Field Selection | Granular | All or custom |
| Developer Tools | Excellent | Limited |
| Learning Curve | Moderate | Low |
| Flexibility | High | Very High |
When to Use GraphQL
Use GraphQL when:
- Building complex UIs with nested data requirements
- Client needs flexibility in data fetching
- You want strong typing and introspection
- Reducing network requests is critical
- Working with modern frontend frameworks (React, Vue, Angular)
Use REST when:
- Simple CRUD operations
- Caching is critical
- Working with legacy systems
- Team is more familiar with REST
Use JSON-RPC when:
- Need maximum flexibility
- Building internal microservices
- Working with AI agents
- Custom operations beyond CRUD
Limitations
Current Limitations
- No Subscriptions: Real-time subscriptions are not yet supported
- No Nested Mutations: Cannot create related records in a single mutation
- Basic Relationships: Relationships are represented as IDs, not nested objects
- No Custom Scalars: Uses built-in GraphQL scalars only
- No Directives: Custom directives not supported
Planned Features
- Nested Relationships: Query related objects without separate requests
- Subscriptions: Real-time updates via WebSocket
- Custom Scalars: Date, DateTime, JSON scalars
- Relay Connections: Standardized pagination
- Field Resolvers: Custom field resolution logic
- DataLoader Integration: Batch and cache database queries
Performance Considerations
Query Complexity
ObjectQL GraphQL doesn't currently limit query complexity. For production:
- Implement Rate Limiting: Limit requests per user/IP
- Set Depth Limits: Prevent deeply nested queries
- Monitor Performance: Track slow queries
- Add Caching: Use Redis or similar for frequently accessed data
Database Optimization
- Add Indexes: Index fields used in filters and sorts
- Use Pagination: Always limit result sets
- Optimize Filters: Use indexed fields in filter conditions
Security
Authentication
GraphQL uses the same authentication as other ObjectQL APIs:
Authorization
ObjectQL's permission system works with GraphQL:
- Object-level permissions
- Field-level permissions
- Record-level permissions
Best Practices
- Always Authenticate: Require authentication for mutations
- Validate Input: ObjectQL validates based on schema
- Rate Limit: Prevent abuse
- Sanitize Errors: Don't expose internal details in production
- Use HTTPS: Always in production
Troubleshooting
Common Issues
Query Returns Null
Check that:
- Object exists in metadata
- ID is correct
- User has permission
- Record exists in database
Type Errors
Ensure:
- Variable types match schema
- Required fields are provided
- Field names are correct
Performance Issues
Solutions:
- Limit result sets with pagination
- Request only needed fields
- Add database indexes
- Use caching
Examples
Complete CRUD Example
Further Reading
Last Updated: January 2026
API Version: 1.0.0