mosaicmap/app/api/bff/route.ts
2025-08-18 00:42:54 +08:00

60 lines
1.8 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
export async function POST(request: NextRequest) {
try {
const body = await request.json();
// 确保请求体包含必要的GraphQL字段
if (!body.query) {
return NextResponse.json(
{ error: 'Missing GraphQL query' },
{ status: 400 }
);
}
// 转发到后端GraphQL
const response = await fetch(process.env.NEXT_PUBLIC_GRAPHQL_BACKEND_URL || 'http://localhost:3050/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': request.headers.get('authorization') || '',
},
body: JSON.stringify({
query: body.query,
variables: body.variables || {},
operationName: body.operationName,
}),
});
if (!response.ok) {
throw new Error(`Backend responded with status: ${response.status}`);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('BFF proxy error:', error);
return NextResponse.json(
{
error: 'Internal server error',
message: error instanceof Error ? error.message : 'Unknown error'
},
{ status: 500 }
);
}
}
export async function GET(request: NextRequest) {
return NextResponse.json({
message: 'BFF endpoint - use POST for GraphQL queries',
usage: {
method: 'POST',
body: {
query: 'GraphQL query string',
variables: 'Query variables object (optional)',
operationName: 'Operation name (optional)'
}
}
});
}