refactor: 重构项目的数据获取方法 #72

Manually merged
remilia merged 13 commits from refactor/graphql into master 2025-11-11 17:07:41 +08:00
28 changed files with 402 additions and 304 deletions
Showing only changes of commit a07d77dde7 - Show all commits

View File

@ -1,4 +1,4 @@
import { readItems } from '@directus/sdk';
import GetQuestionList from '@/graphql/questionList.graphql?raw';
export const useQuestionList = () => {
const { $directus } = useNuxtApp();
@ -7,57 +7,8 @@ export const useQuestionList = () => {
const locale = getDirectusLocale();
return useAsyncData(`question-list-${locale}`, async () => {
return await $directus.request(
readItems('questions', {
fields: [
'id',
{
translations: ['*'],
},
{
products: [
'id',
{
products_id: [
'id',
{
product_type: [
'id',
{
translations: ['id', 'name'],
},
],
},
{ translations: ['id', 'name'] },
],
},
],
},
],
deep: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
products: {
products_id: {
product_type: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
},
},
})
);
return await $directus.query<{ questions: Question[] }>(GetQuestionList, {
locale: locale,
});
});
};

View File

@ -0,0 +1,27 @@
query GetQuestionList($locale: String!) {
questions {
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

@ -41,7 +41,7 @@
const { data, pending, error } = await useQuestionList();
const questions = computed(
() => data.value.map((item) => toQuestionListView(item)) ?? null
() => data.value.questions.map((item) => toQuestionListView(item)) ?? null
);
const productTypeOptions = computed(() => {