|
| 1 | +# ObjectQL Integration Guide |
| 2 | + |
| 3 | +This guide explains how to integrate Object UI with ObjectQL API backends to create data-driven applications. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +ObjectQL is a metadata-driven backend platform that provides automatic CRUD APIs based on object definitions. The `@object-ui/data-objectql` package provides a seamless bridge between Object UI components and ObjectQL APIs. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +``` |
| 12 | +┌─────────────────────────────────────────────────────────────┐ |
| 13 | +│ Object UI Components │ |
| 14 | +│ (Forms, Tables, Cards, Dashboards, etc.) │ |
| 15 | +└──────────────────┬──────────────────────────────────────────┘ |
| 16 | + │ |
| 17 | + │ Uses DataSource Interface |
| 18 | + │ |
| 19 | +┌──────────────────▼──────────────────────────────────────────┐ |
| 20 | +│ @object-ui/data-objectql │ |
| 21 | +│ │ |
| 22 | +│ • ObjectQLDataSource (API Adapter) │ |
| 23 | +│ • React Hooks (useObjectQL, useObjectQLQuery, etc.) │ |
| 24 | +│ • Query Parameter Conversion │ |
| 25 | +│ • Error Handling & Type Safety │ |
| 26 | +└──────────────────┬──────────────────────────────────────────┘ |
| 27 | + │ |
| 28 | + │ HTTP/REST Calls |
| 29 | + │ |
| 30 | +┌──────────────────▼──────────────────────────────────────────┐ |
| 31 | +│ ObjectQL API Server │ |
| 32 | +│ │ |
| 33 | +│ • Object Definitions (Metadata) │ |
| 34 | +│ • Automatic CRUD Endpoints │ |
| 35 | +│ • Business Logic & Validation │ |
| 36 | +│ • Database Abstraction │ |
| 37 | +└──────────────────────────────────────────────────────────────┘ |
| 38 | +``` |
| 39 | + |
| 40 | +## Quick Start |
| 41 | + |
| 42 | +### Installation |
| 43 | + |
| 44 | +```bash |
| 45 | +npm install @object-ui/react @object-ui/components @object-ui/data-objectql |
| 46 | +``` |
| 47 | + |
| 48 | +### Basic Setup |
| 49 | + |
| 50 | +```tsx |
| 51 | +import React from 'react'; |
| 52 | +import { SchemaRenderer } from '@object-ui/react'; |
| 53 | +import { registerDefaultRenderers } from '@object-ui/components'; |
| 54 | +import { ObjectQLDataSource } from '@object-ui/data-objectql'; |
| 55 | + |
| 56 | +// Register Object UI components |
| 57 | +registerDefaultRenderers(); |
| 58 | + |
| 59 | +// Create ObjectQL data source |
| 60 | +const dataSource = new ObjectQLDataSource({ |
| 61 | + baseUrl: 'https://api.example.com', |
| 62 | + token: 'your-auth-token', // Optional |
| 63 | + spaceId: 'workspace123', // Optional for multi-tenant |
| 64 | +}); |
| 65 | + |
| 66 | +// Define your schema |
| 67 | +const schema = { |
| 68 | + type: 'page', |
| 69 | + title: 'Contacts', |
| 70 | + body: { |
| 71 | + type: 'data-table', |
| 72 | + api: 'contacts', // ObjectQL object name |
| 73 | + columns: [ |
| 74 | + { name: 'name', label: 'Name' }, |
| 75 | + { name: 'email', label: 'Email' }, |
| 76 | + { name: 'phone', label: 'Phone' }, |
| 77 | + { name: 'status', label: 'Status' } |
| 78 | + ] |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +function App() { |
| 83 | + return <SchemaRenderer schema={schema} dataSource={dataSource} />; |
| 84 | +} |
| 85 | + |
| 86 | +export default App; |
| 87 | +``` |
| 88 | + |
| 89 | +## Using React Hooks |
| 90 | + |
| 91 | +### useObjectQLQuery Hook |
| 92 | + |
| 93 | +Fetch data with automatic state management: |
| 94 | + |
| 95 | +```tsx |
| 96 | +import { useObjectQL, useObjectQLQuery } from '@object-ui/data-objectql'; |
| 97 | + |
| 98 | +function ContactList() { |
| 99 | + const dataSource = useObjectQL({ |
| 100 | + config: { baseUrl: 'https://api.example.com' } |
| 101 | + }); |
| 102 | + |
| 103 | + const { data, loading, error, refetch } = useObjectQLQuery( |
| 104 | + dataSource, |
| 105 | + 'contacts', |
| 106 | + { |
| 107 | + $filter: { status: 'active' }, |
| 108 | + $orderby: { created: 'desc' }, |
| 109 | + $top: 20, |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + if (loading) return <div>Loading...</div>; |
| 114 | + if (error) return <div>Error: {error.message}</div>; |
| 115 | + |
| 116 | + return ( |
| 117 | + <div> |
| 118 | + <button onClick={refetch}>Refresh</button> |
| 119 | + <ul> |
| 120 | + {data?.map(contact => ( |
| 121 | + <li key={contact._id}>{contact.name}</li> |
| 122 | + ))} |
| 123 | + </ul> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### useObjectQLMutation Hook |
| 130 | + |
| 131 | +Perform create, update, and delete operations: |
| 132 | + |
| 133 | +```tsx |
| 134 | +import { useObjectQL, useObjectQLMutation } from '@object-ui/data-objectql'; |
| 135 | + |
| 136 | +function ContactForm() { |
| 137 | + const dataSource = useObjectQL({ |
| 138 | + config: { baseUrl: 'https://api.example.com' } |
| 139 | + }); |
| 140 | + |
| 141 | + const { create, update, remove, loading } = useObjectQLMutation( |
| 142 | + dataSource, |
| 143 | + 'contacts' |
| 144 | + ); |
| 145 | + |
| 146 | + const handleCreate = async () => { |
| 147 | + await create({ |
| 148 | + name: 'John Doe', |
| 149 | + email: 'john@example.com', |
| 150 | + status: 'active' |
| 151 | + }); |
| 152 | + }; |
| 153 | + |
| 154 | + return ( |
| 155 | + <div> |
| 156 | + <button onClick={handleCreate} disabled={loading}> |
| 157 | + Create Contact |
| 158 | + </button> |
| 159 | + </div> |
| 160 | + ); |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +## Query Parameters |
| 165 | + |
| 166 | +Object UI uses universal query parameters that are automatically converted to ObjectQL format: |
| 167 | + |
| 168 | +### Field Selection |
| 169 | + |
| 170 | +```typescript |
| 171 | +await dataSource.find('contacts', { |
| 172 | + $select: ['name', 'email', 'account.name'] |
| 173 | +}); |
| 174 | +``` |
| 175 | + |
| 176 | +### Filtering |
| 177 | + |
| 178 | +```typescript |
| 179 | +await dataSource.find('contacts', { |
| 180 | + $filter: { |
| 181 | + status: 'active', |
| 182 | + age: { $gte: 18 } |
| 183 | + } |
| 184 | +}); |
| 185 | +``` |
| 186 | + |
| 187 | +### Sorting & Pagination |
| 188 | + |
| 189 | +```typescript |
| 190 | +await dataSource.find('contacts', { |
| 191 | + $orderby: { created: 'desc' }, |
| 192 | + $skip: 20, |
| 193 | + $top: 10 |
| 194 | +}); |
| 195 | +``` |
| 196 | + |
| 197 | +## Configuration Options |
| 198 | + |
| 199 | +```typescript |
| 200 | +interface ObjectQLConfig { |
| 201 | + baseUrl: string; // Required: ObjectQL server URL |
| 202 | + version?: string; // API version (default: 'v1') |
| 203 | + token?: string; // Authentication token |
| 204 | + spaceId?: string; // Workspace/tenant ID |
| 205 | + headers?: Record<string, string>; |
| 206 | + timeout?: number; // Request timeout (default: 30000ms) |
| 207 | + withCredentials?: boolean; // Include credentials (default: true) |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +## See Also |
| 212 | + |
| 213 | +- [Package README](../../packages/data-objectql/README.md) - Detailed API reference |
| 214 | +- [ObjectQL Documentation](https://www.objectql.com/docs) |
| 215 | +- [Component Library](../api/components.md) |
0 commit comments