fix: 修正GraphQL无法正常读取的问题
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m8s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m8s
- 将graphql查询文件移至server/assets,他们在构建后会以二进制形式写入server的存储中 - 在server端通过storage读取数据
This commit is contained in:
@ -1,12 +1,14 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
import { directus } from '~~/server/utils/directus';
|
||||||
import { readFileSync } from 'node:fs';
|
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const query = readFileSync(
|
const query = await loadAssetAsString(
|
||||||
path.resolve('graphql/companyProfile.graphql'),
|
'assets:server',
|
||||||
'utf-8'
|
'graphql/companyProfile.graphql'
|
||||||
);
|
);
|
||||||
|
if (!query)
|
||||||
|
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
const data = await directus.query<{ company_profile: CompanyProfile }>(
|
const data = await directus.query<{ company_profile: CompanyProfile }>(
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
import { directus } from '~~/server/utils/directus';
|
||||||
import { readFileSync } from 'node:fs';
|
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const query = readFileSync(
|
const query = await loadAssetAsString(
|
||||||
path.resolve('graphql/contactInfo.graphql'),
|
'assets:server',
|
||||||
'utf-8'
|
'graphql/contactInfo.graphql'
|
||||||
);
|
);
|
||||||
|
if (!query)
|
||||||
|
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
const data = await directus.query<{ contact_info: ContactInfo }>(query, {
|
const data = await directus.query<{ contact_info: ContactInfo }>(query, {
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
import { directus } from '~~/server/utils/directus';
|
||||||
import { readFileSync } from 'node:fs';
|
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const query = readFileSync(
|
const query = await loadAssetAsString(
|
||||||
path.resolve('graphql/documentList.graphql'),
|
'assets:server',
|
||||||
'utf-8'
|
'graphql/documentList.graphql'
|
||||||
);
|
);
|
||||||
|
if (!query)
|
||||||
|
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
const data = await directus.query<{ product_documents: ProductDocument[] }>(
|
const data = await directus.query<{ product_documents: ProductDocument[] }>(
|
||||||
|
|||||||
@ -1,9 +1,14 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
import { directus } from '~~/server/utils/directus';
|
||||||
import { readFileSync } from 'node:fs';
|
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const query = readFileSync(path.resolve('graphql/homepage.graphql'), 'utf-8');
|
const query = await loadAssetAsString(
|
||||||
|
'assets:server',
|
||||||
|
'graphql/homepage.graphql'
|
||||||
|
);
|
||||||
|
if (!query)
|
||||||
|
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
const data = await directus.query<{ homepage: Homepage }>(query, {
|
const data = await directus.query<{ homepage: Homepage }>(query, {
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
import { directus } from '~~/server/utils/directus';
|
||||||
import { readFileSync } from 'node:fs';
|
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const id = getRouterParam(event, 'id');
|
const id = getRouterParam(event, 'id');
|
||||||
@ -10,7 +9,13 @@ export default defineEventHandler(async (event) => {
|
|||||||
statusMessage: 'Product ID is required',
|
statusMessage: 'Product ID is required',
|
||||||
});
|
});
|
||||||
|
|
||||||
const query = readFileSync(path.resolve('graphql/product.graphql'), 'utf-8');
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/product.graphql'
|
||||||
|
);
|
||||||
|
if (!query)
|
||||||
|
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
const data = await directus.query<{ products_by_id: Product }>(query, {
|
const data = await directus.query<{ products_by_id: Product }>(query, {
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
import { directus } from '~~/server/utils/directus';
|
||||||
import { readFileSync } from 'node:fs';
|
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const query = readFileSync(
|
const query = await loadAssetAsString(
|
||||||
path.resolve('graphql/productList.graphql'),
|
'assets:server',
|
||||||
'utf-8'
|
'graphql/productList.graphql'
|
||||||
);
|
);
|
||||||
|
if (!query)
|
||||||
|
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
const data = await directus.query<{ products: Product[] }>(query, {
|
const data = await directus.query<{ products: Product[] }>(query, {
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
import { directus } from '~~/server/utils/directus';
|
||||||
import { readFileSync } from 'node:fs';
|
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const query = readFileSync(
|
const query = await loadAssetAsString(
|
||||||
path.resolve('graphql/questionList.graphql'),
|
'assets:server',
|
||||||
'utf-8'
|
'graphql/questionList.graphql'
|
||||||
);
|
);
|
||||||
|
if (!query)
|
||||||
|
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
const data = await directus.query<{ questions: Question[] }>(query, {
|
const data = await directus.query<{ questions: Question[] }>(query, {
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
import { directus } from '~~/server/utils/directus';
|
||||||
import { readFileSync } from 'node:fs';
|
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const id = getRouterParam(event, 'id');
|
const id = getRouterParam(event, 'id');
|
||||||
@ -10,7 +9,13 @@ export default defineEventHandler(async (event) => {
|
|||||||
statusMessage: 'Solution ID is required',
|
statusMessage: 'Solution ID is required',
|
||||||
});
|
});
|
||||||
|
|
||||||
const query = readFileSync(path.resolve('graphql/solution.graphql'), 'utf-8');
|
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 locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
const data = await directus.query<{ solutions_by_id: Solution }>(query, {
|
const data = await directus.query<{ solutions_by_id: Solution }>(query, {
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
import { directus } from '~~/server/utils/directus';
|
||||||
import { readFileSync } from 'node:fs';
|
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const query = readFileSync(
|
const query = await loadAssetAsString(
|
||||||
path.resolve('graphql/solutionList.graphql'),
|
'assets:server',
|
||||||
'utf-8'
|
'graphql/solutionList.graphql'
|
||||||
);
|
);
|
||||||
|
if (!query)
|
||||||
|
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
const data = await directus.query<{ solutions: Solution[] }>(query, {
|
const data = await directus.query<{ solutions: Solution[] }>(query, {
|
||||||
|
|||||||
24
server/utils/serverAssets.ts
Normal file
24
server/utils/serverAssets.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* 将存储中的资源作为字符串加载
|
||||||
|
*
|
||||||
|
* @param storage - 存储名称
|
||||||
|
* @param path - 资源路径
|
||||||
|
*
|
||||||
|
* ---
|
||||||
|
* @example
|
||||||
|
* const content = await loadAssetAsString('myStorage', 'path/to/asset.txt');
|
||||||
|
* // typeof content === 'string'
|
||||||
|
*/
|
||||||
|
export async function loadAssetAsString(
|
||||||
|
storage: string,
|
||||||
|
path: string
|
||||||
|
): Promise<string> {
|
||||||
|
const data = await useStorage(storage).getItem(path);
|
||||||
|
if (data instanceof Uint8Array) {
|
||||||
|
return Buffer.from(data).toString('utf-8');
|
||||||
|
}
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
throw new Error(`Invalid asset type for ${path}`);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user