Files
jinshen-website/server/api/cms/solution/[id].ts
R2m1liA 50f0779a8e
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m22s
fix: 修正无法正常读取GraphQL的问题
- 将GraphQL移至server/assets,在构建后会被写入server中
- 在server端通过storage读取数据
2025-11-12 19:49:17 +08:00

34 lines
902 B
TypeScript

import { directus } from '~~/server/utils/directus';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
export default defineEventHandler(async (event) => {
const id = getRouterParam(event, 'id');
if (!id)
throw createError({
statusCode: 400,
statusMessage: 'Solution ID is required',
});
const query = await loadAssetAsString(
'assets:server',
'graphql/solution.graphql'
);
if (!query)
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
const locale = getHeader(event, 'x-locale') || 'zh-CN';
const data = await directus.query<{ solutions_by_id: Solution }>(query, {
id: id,
locale: locale,
});
const solution = data?.solutions_by_id;
if (!solution || solution.status === 'archived') {
throw createError({ statusCode: 404, statusMessage: 'Solution not found' });
}
return solution;
});