refactor: 产品页与产品列表的API重构
- 将产品页与产品列表的API由REST重构为GraphQL - 修改Mapper与单元测试
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import { readItem } from '@directus/sdk';
|
||||
import GetProduct from '@/graphql/product.graphql';
|
||||
import { print } from 'graphql';
|
||||
|
||||
export const useProduct = (id: string) => {
|
||||
const { $directus } = useNuxtApp();
|
||||
@ -7,104 +8,111 @@ export const useProduct = (id: string) => {
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`product-${id}-${locale}`, async () => {
|
||||
return await $directus.request(
|
||||
readItem('products', id, {
|
||||
fields: [
|
||||
'id',
|
||||
{ translations: ['id', 'name', 'summary', 'description'] },
|
||||
{
|
||||
images: [
|
||||
'id',
|
||||
{
|
||||
product_images_id: [
|
||||
'id',
|
||||
'image',
|
||||
{ translations: ['id', 'caption'] },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
specs: [
|
||||
'id',
|
||||
{
|
||||
translations: ['*'],
|
||||
},
|
||||
{
|
||||
specs: [
|
||||
'id',
|
||||
{
|
||||
translations: ['id', 'key', 'value'],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
faqs: [
|
||||
'id',
|
||||
{
|
||||
questions_id: [
|
||||
'id',
|
||||
{
|
||||
translations: ['id', 'title', 'content'],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
documents: [
|
||||
'id',
|
||||
{
|
||||
product_documents_id: [
|
||||
'id',
|
||||
{
|
||||
file: ['id', 'filesize', 'filename_download'],
|
||||
},
|
||||
{
|
||||
translations: ['id', 'title'],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
deep: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: locale },
|
||||
},
|
||||
},
|
||||
images: {
|
||||
product_images_id: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: locale },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
faqs: {
|
||||
questions_id: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: locale },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
documents: {
|
||||
documents_id: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: locale },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return await $directus.query<{ products_by_id: Product }>(
|
||||
print(GetProduct),
|
||||
{
|
||||
id: id,
|
||||
locale: locale,
|
||||
}
|
||||
);
|
||||
// return await $directus.request(
|
||||
// readItem('products', id, {
|
||||
// fields: [
|
||||
// 'id',
|
||||
// { translations: ['id', 'name', 'summary', 'description'] },
|
||||
// {
|
||||
// images: [
|
||||
// 'id',
|
||||
// {
|
||||
// product_images_id: [
|
||||
// 'id',
|
||||
// 'image',
|
||||
// { translations: ['id', 'caption'] },
|
||||
// ],
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// {
|
||||
// specs: [
|
||||
// 'id',
|
||||
// {
|
||||
// translations: ['*'],
|
||||
// },
|
||||
// {
|
||||
// specs: [
|
||||
// 'id',
|
||||
// {
|
||||
// translations: ['id', 'key', 'value'],
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// {
|
||||
// faqs: [
|
||||
// 'id',
|
||||
// {
|
||||
// questions_id: [
|
||||
// 'id',
|
||||
// {
|
||||
// translations: ['id', 'title', 'content'],
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// {
|
||||
// documents: [
|
||||
// 'id',
|
||||
// {
|
||||
// product_documents_id: [
|
||||
// 'id',
|
||||
// {
|
||||
// file: ['id', 'filesize', 'filename_download'],
|
||||
// },
|
||||
// {
|
||||
// translations: ['id', 'title'],
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// ],
|
||||
// deep: {
|
||||
// translations: {
|
||||
// _filter: {
|
||||
// languages_code: { _eq: locale },
|
||||
// },
|
||||
// },
|
||||
// images: {
|
||||
// product_images_id: {
|
||||
// translations: {
|
||||
// _filter: {
|
||||
// languages_code: { _eq: locale },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// faqs: {
|
||||
// questions_id: {
|
||||
// translations: {
|
||||
// _filter: {
|
||||
// languages_code: { _eq: locale },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// documents: {
|
||||
// documents_id: {
|
||||
// translations: {
|
||||
// _filter: {
|
||||
// languages_code: { _eq: locale },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
// );
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { readItems } from '@directus/sdk';
|
||||
import GetProductList from '@/graphql/productList.graphql';
|
||||
import { print } from 'graphql';
|
||||
|
||||
export const useProductList = () => {
|
||||
const { $directus } = useNuxtApp();
|
||||
@ -7,37 +8,11 @@ export const useProductList = () => {
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`product-list-${locale}`, async () => {
|
||||
return await $directus.request(
|
||||
readItems('products', {
|
||||
fields: [
|
||||
'id',
|
||||
{ translations: ['id', 'name', 'summary'] },
|
||||
'cover',
|
||||
{
|
||||
product_type: [
|
||||
'id',
|
||||
{
|
||||
translations: ['id', 'name'],
|
||||
},
|
||||
'sort',
|
||||
],
|
||||
},
|
||||
],
|
||||
deep: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: locale },
|
||||
},
|
||||
},
|
||||
product_type: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: locale },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return await $directus.query<{ products: Product[] }>(
|
||||
print(GetProductList),
|
||||
{
|
||||
locale: locale,
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user