Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions packages/neug-query/src/services/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ const DB_QUERY_SAVED = localforage.createInstance({
// QueryService 类 - 将原来的 website 服务逻辑适配到这里,使用 localforage
export class QueryService {
private language: 'cypher' | 'gremlin';
private apiBaseUrl: string;

constructor(language: 'cypher' | 'gremlin' = 'gremlin') {
this.language = language;
// 在开发环境使用相对路径以利用 Vite 代理,生产环境使用环境变量
if (import.meta.env.DEV) {
this.apiBaseUrl = ''; // 使用相对路径,让 Vite 代理处理
} else {
this.apiBaseUrl = import.meta.env.VITE_API_BASE_URL || window.location.origin;
}
}

// 查询图数据 - 基于 website 包的逻辑,去掉 Browser 模式
Expand All @@ -30,19 +37,17 @@ export class QueryService {
language: 'cypher',
};
try {
const response = await fetch(`${window.location.origin}/queryGraphData`, {
const response = await fetch(`${this.apiBaseUrl}/cypherv2`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Type': 'text/plain',
},
body: JSON.stringify(_params),
body: params.script,
});

const result = await response.json();

if (result.success) {
return result.data;
}
return result;

console.warn('查询失败,返回模拟数据');
} catch (error) {
Expand Down Expand Up @@ -667,7 +672,7 @@ export class QueryService {
// 查询图模式
async queryGraphSchema(): Promise<CypherSchemaData> {
try {
const response = await fetch(`${window.location.origin}/queryGraphSchema`);
const response = await fetch(`${this.apiBaseUrl}/schema`);
const result = await response.json();
if (result.success) {
const schema = result.data;
Expand Down
9 changes: 9 additions & 0 deletions packages/neug-query/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_API_BASE_URL?: string
}

interface ImportMeta {
readonly env: ImportMetaEnv
}
21 changes: 20 additions & 1 deletion packages/neug-query/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const isSingle = mode === 'single' && process.env.NODE_ENV === 'production';

export default defineConfig(({ mode }) => {
const isDev = mode === 'development';

const apiBaseUrl = process.env.VITE_API_BASE_URL || 'http://localhost:10001';

return {
plugins: [
react({
Expand Down Expand Up @@ -98,6 +99,24 @@ export default defineConfig(({ mode }) => {
// 监听 workspace 依赖目录
ignored: ['!**/node_modules/@graphscope/**', '!../studio-components/**', '!../studio-query/**'],
},
// 配置代理来解决 CORS 问题
proxy: {
'/cypher': {
target: apiBaseUrl,
changeOrigin: true,
secure: false
},
'/cypherv2': {
target: apiBaseUrl,
changeOrigin: true,
secure: false
},
'/schema': {
target: apiBaseUrl,
changeOrigin: true,
secure: false
},
},
},
define: {
// 为生产环境定义环境变量
Expand Down