fix: 修正无法正常读取GraphQL的问题
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m22s

- 将GraphQL移至server/assets,在构建后会被写入server中
- 在server端通过storage读取数据
This commit is contained in:
2025-11-12 19:49:17 +08:00
parent 5ee6005ad1
commit 50f0779a8e
19 changed files with 90 additions and 39 deletions

View File

@ -1,12 +1,14 @@
import { directus } from '~~/server/utils/directus';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
export default defineEventHandler(async (event) => {
const query = readFileSync(
path.resolve('graphql/companyProfile.graphql'),
'utf-8'
const query = await loadAssetAsString(
'assets:server',
'graphql/companyProfile.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<{ company_profile: CompanyProfile }>(

View File

@ -1,12 +1,14 @@
import { directus } from '~~/server/utils/directus';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
export default defineEventHandler(async (event) => {
const query = readFileSync(
path.resolve('graphql/contactInfo.graphql'),
'utf-8'
const query = await loadAssetAsString(
'assets:server',
'graphql/contactInfo.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<{ contact_info: ContactInfo }>(query, {

View File

@ -1,12 +1,14 @@
import { directus } from '~~/server/utils/directus';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
export default defineEventHandler(async (event) => {
const query = readFileSync(
path.resolve('graphql/documentList.graphql'),
'utf-8'
const query = await loadAssetAsString(
'assets:server',
'graphql/documentList.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<{ product_documents: ProductDocument[] }>(

View File

@ -1,9 +1,14 @@
import { directus } from '~~/server/utils/directus';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
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 data = await directus.query<{ homepage: Homepage }>(query, {

View File

@ -1,6 +1,5 @@
import { directus } from '~~/server/utils/directus';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
export default defineEventHandler(async (event) => {
const id = getRouterParam(event, 'id');
@ -10,7 +9,13 @@ export default defineEventHandler(async (event) => {
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 data = await directus.query<{ products_by_id: Product }>(query, {

View File

@ -1,12 +1,14 @@
import { directus } from '~~/server/utils/directus';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
export default defineEventHandler(async (event) => {
const query = readFileSync(
path.resolve('graphql/productList.graphql'),
'utf-8'
const query = await loadAssetAsString(
'assets:server',
'graphql/productList.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<{ products: Product[] }>(query, {

View File

@ -1,12 +1,14 @@
import { directus } from '~~/server/utils/directus';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
export default defineEventHandler(async (event) => {
const query = readFileSync(
path.resolve('graphql/questionList.graphql'),
'utf-8'
const query = await loadAssetAsString(
'assets:server',
'graphql/questionList.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<{ questions: Question[] }>(query, {

View File

@ -1,6 +1,5 @@
import { directus } from '~~/server/utils/directus';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
export default defineEventHandler(async (event) => {
const id = getRouterParam(event, 'id');
@ -10,7 +9,13 @@ export default defineEventHandler(async (event) => {
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 data = await directus.query<{ solutions_by_id: Solution }>(query, {

View File

@ -1,12 +1,14 @@
import { directus } from '~~/server/utils/directus';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { loadAssetAsString } from '~~/server/utils/serverAssets';
export default defineEventHandler(async (event) => {
const query = readFileSync(
path.resolve('graphql/solutionList.graphql'),
'utf-8'
const query = await loadAssetAsString(
'assets:server',
'graphql/solutionList.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: Solution[] }>(query, {

View File

@ -0,0 +1,9 @@
query GetCompanyProfile($locale: String!) {
company_profile {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
content
}
}
}

View File

@ -0,0 +1,9 @@
query GetContactInfo($locale: String!) {
contact_info {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
content
}
}
}

View File

@ -0,0 +1,31 @@
query GetDocumentList($locale: String!) {
product_documents(filter: { status: { _eq: "published" } }) {
id
file {
id
filesize
filename_download
}
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
}
products {
id
products_id {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
product_type {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
}
}
}
}
}

View File

@ -0,0 +1,33 @@
query GetHomepage($locale: String!) {
homepage {
id
carousel {
id
directus_files_id {
id
}
}
recommend_products {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
summary
}
cover {
id
}
}
recommend_solutions {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
summary
}
cover {
id
}
}
}
}

View File

@ -0,0 +1,65 @@
query GetProduct($id: ID!, $locale: String!) {
products_by_id(id: $id) {
id
status
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
summary
description
}
images {
id
product_images_id {
id
image {
id
}
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
caption
}
}
}
specs {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
specs {
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
key
value
}
}
}
faqs {
id
questions_id(filter: { status: { _eq: "published" } }) {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
content
}
}
}
documents {
id
product_documents_id(filter: { status: { _eq: "published" } }) {
id
file {
id
filesize
filename_download
}
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
}
}
}
}
}

View File

@ -0,0 +1,22 @@
query GetProductList($locale: String!) {
products(filter: { status: { _eq: "in-production" } }) {
id
status
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
summary
}
cover {
id
}
product_type {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
sort
}
}
}

View File

@ -0,0 +1,27 @@
query GetQuestionList($locale: String!) {
questions(filter: { status: { _eq: "published" } }) {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
content
}
products {
id
products_id {
id
product_type {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
}
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
}
}
}
}

View File

@ -0,0 +1,12 @@
query GetSolution($id: ID!, $locale: String!) {
solutions_by_id(id: $id) {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
summary
content
}
create_at
}
}

View File

@ -0,0 +1,21 @@
query GetSolutionList($locale: String!) {
solutions(filter: { status: { _eq: "published" } }) {
id
cover {
id
}
type {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
sort
}
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
summary
}
}
}

View 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}`);
}