lrj
23 小时以前 7ad9c3c93f0cc103347ae2e2429e0122fb512e24
web/src/config/api.ts
@@ -20,33 +20,46 @@
  // 获取JWT token
  const { getToken } = await import('@/utils/auth');
  const token = getToken();
  // 构建请求头
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  if (token) {
    headers['Authorization'] = `Bearer ${token}`;
  }
  const response = await fetch(API_CONFIG.GRAPHQL_ENDPOINT, {
    method: 'POST',
    headers: headers,
    body: JSON.stringify({
      query,
      variables,
    }),
  })
  // 构建请求体
  const requestBody = JSON.stringify({
    query,
    variables,
  });
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`)
  try {
    // 发送请求
    const response = await fetch(API_CONFIG.GRAPHQL_ENDPOINT, {
      method: 'POST',
      headers,
      body: requestBody,
    });
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const result = await response.json();
    if (result.errors) {
      throw new Error(`GraphQL errors: ${JSON.stringify(result.errors)}`);
    }
    return result;
  } catch (error) {
    console.error('=== GraphQL请求失败 ===');
    console.error('错误详情:', error);
    throw error;
  }
  const result = await response.json()
  if (result.errors) {
    throw new Error(result.errors[0].message)
  }
  return result.data
}
// 通用API请求工具函数