refactor: 问题列表的API重构

This commit is contained in:
2025-11-11 16:49:58 +08:00
parent 3e7b195002
commit a07d77dde7
3 changed files with 32 additions and 54 deletions

View File

@ -1,4 +1,4 @@
import { readItems } from '@directus/sdk'; import GetQuestionList from '@/graphql/questionList.graphql?raw';
export const useQuestionList = () => { export const useQuestionList = () => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -7,57 +7,8 @@ export const useQuestionList = () => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`question-list-${locale}`, async () => { return useAsyncData(`question-list-${locale}`, async () => {
return await $directus.request( return await $directus.query<{ questions: Question[] }>(GetQuestionList, {
readItems('questions', { locale: locale,
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 },
},
},
},
},
},
})
);
}); });
}; };

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 { data, pending, error } = await useQuestionList();
const questions = computed( const questions = computed(
() => data.value.map((item) => toQuestionListView(item)) ?? null () => data.value.questions.map((item) => toQuestionListView(item)) ?? null
); );
const productTypeOptions = computed(() => { const productTypeOptions = computed(() => {