|
1 | 1 | import { ObjectQL } from '@objectql/core'; |
2 | 2 | import { SqlDriver } from '@objectql/driver-sql'; |
3 | 3 | import { ObjectLoader, loadModules } from '@objectql/platform-node'; |
4 | | -import { createNodeHandler } from '@objectql/server'; |
| 4 | +import { createNodeHandler, createGraphQLHandler } from '@objectql/server'; |
5 | 5 | import { createServer } from 'http'; |
6 | 6 | import * as path from 'path'; |
7 | 7 | import * as fs from 'fs'; |
8 | 8 | import chalk from 'chalk'; |
9 | 9 |
|
10 | | -const CONSOLE_HTML = ` |
| 10 | +const SCALAR_HTML = ` |
11 | 11 | <!DOCTYPE html> |
12 | 12 | <html> |
13 | 13 | <head> |
14 | | - <title>ObjectQL API Reference (Scalar)</title> |
| 14 | + <title>ObjectQL API Reference</title> |
15 | 15 | <meta charset="utf-8" /> |
16 | 16 | <meta name="viewport" content="width=device-width, initial-scale=1" /> |
17 | 17 | <style> |
@@ -122,20 +122,26 @@ export async function serve(options: { |
122 | 122 |
|
123 | 123 | // 3. Create Handler |
124 | 124 | const internalHandler = createNodeHandler(app); |
| 125 | + const graphqlHandler = createGraphQLHandler(app); |
125 | 126 |
|
126 | 127 | // 4. Start Server |
127 | 128 | const server = createServer(async (req, res) => { |
128 | | - // Serve Swagger UI |
129 | | - if (req.method === 'GET' && (req.url === '/swagger' || req.url === '/swagger/')) { |
| 129 | + // Serve API Docs at Root (Default) |
| 130 | + if (req.method === 'GET' && (req.url === '/' || req.url === '/docs' || req.url === '/docs/')) { |
130 | 131 | res.writeHead(200, { 'Content-Type': 'text/html' }); |
131 | | - res.end(CONSOLE_HTML); |
| 132 | + res.end(SCALAR_HTML); |
132 | 133 | return; |
133 | 134 | } |
134 | 135 |
|
135 | | - // Redirect / to /swagger for better DX |
136 | | - if (req.method === 'GET' && req.url === '/') { |
137 | | - res.writeHead(302, { 'Location': '/swagger' }); |
138 | | - res.end(); |
| 136 | + // GraphQL Endpoint & Playground (Keep for compatibility) |
| 137 | + if (req.url === '/graphql' || req.url === '/graphql/') { |
| 138 | + await graphqlHandler(req, res); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + // Keep /api/graphql as alias for compatibility |
| 143 | + if (req.url?.startsWith('/api/graphql')) { |
| 144 | + await graphqlHandler(req, res); |
139 | 145 | return; |
140 | 146 | } |
141 | 147 |
|
@@ -169,10 +175,10 @@ export async function serve(options: { |
169 | 175 | server.listen(port, () => { |
170 | 176 | server.removeListener('error', onError); |
171 | 177 | console.log(chalk.green(`\n🚀 Server ready at http://localhost:${port}`)); |
172 | | - console.log(chalk.green(`📚 Swagger UI: http://localhost:${port}/swagger`)); |
173 | | - console.log(chalk.blue(`📖 OpenAPI Spec: http://localhost:${port}/openapi.json`)); |
| 178 | + console.log(chalk.blue(`📖 API Docs (Scalar): http://localhost:${port}/`)); |
| 179 | + |
174 | 180 | console.log(chalk.gray('\nTry a curl command:')); |
175 | | - console.log(`curl -X POST http://localhost:${port} -H "Content-Type: application/json" -d '{"op": "find", "object": "YourObject", "args": {}}'`); |
| 181 | + console.log(`curl -X POST http://localhost:${port} -H "Content-Type: application/json" -d '{"op": "find", "object": "tasks", "args": {}}'`); |
176 | 182 | }); |
177 | 183 | }; |
178 | 184 |
|
|
0 commit comments