refactor: 解决方案页的API重构

This commit is contained in:
2025-11-11 16:43:08 +08:00
parent 706b754905
commit 3e7b195002
3 changed files with 21 additions and 23 deletions

View File

@ -1,4 +1,4 @@
import { readItem } from '@directus/sdk'; import GetSolution from '@/graphql/solution.graphql?raw';
export const useSolution = (id: string) => { export const useSolution = (id: string) => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -6,23 +6,9 @@ export const useSolution = (id: string) => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`solution-${id}-${locale}`, async () => { return useAsyncData(`solution-${id}-${locale}`, async () => {
return await $directus.request( return await $directus.query<{ solutions_by_id: Solution }>(GetSolution, {
readItem('solutions', id, { id: id,
fields: [ locale: locale,
'id', });
{
translations: ['*'],
},
'create_at',
],
deep: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
})
);
}); });
}; };

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

@ -26,16 +26,16 @@
const route = useRoute(); const route = useRoute();
const localePath = useLocalePath(); const localePath = useLocalePath();
// (documentId) //
const id = computed(() => route.params.slug as string); const id = route.params.slug as string;
const { data, pending, error } = await useSolution(id.value); const { data, pending, error } = await useSolution(id);
const solution = computed(() => { const solution = computed(() => {
if (!data.value) { if (!data.value) {
return null; return null;
} }
return toSolutionView(data.value); return toSolutionView(data.value.solutions_by_id);
}); });
const breadcrumbItems = computed(() => [ const breadcrumbItems = computed(() => [