Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(magento): pdp in uses only gql #688

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 20 additions & 51 deletions magento/loaders/product/detailsPageGQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
import { formatUrlSuffix, getCustomFields } from "../../utils/utilsGraphQL.ts";
import { GetCompleteProduct } from "../../utils/clientGraphql/queries.ts";
import { toProductGraphQL } from "../../utils/transform.ts";
import { URL_KEY } from "../../utils/constants.ts";
import stringifySearchCriteria from "../../utils/stringifySearchCriteria.ts";

export interface Props {
slug: RequestURLParam;
Expand Down Expand Up @@ -49,74 +47,45 @@ async function loader(
site,
minInstallmentValue,
maxInstallments,
currencyCode,
storeId,
clientAdmin,
} = ctx;
const STALE = enableCache ? DecoStale : undefined;
const customAttributes = getCustomFields(customFields, ctx.customAttributes);
const defaultPath = useSuffix ? formatUrlSuffix(site) : undefined;

const getProductSku = async () => {
const searchCriteria = {
filterGroups: [
{
filters: [{ field: URL_KEY, value: slug }],
},
],
};

const queryParams = {
site,
currencyCode,
storeId,
...stringifySearchCriteria(searchCriteria),
};

const itemSku = await clientAdmin["GET /rest/:site/V1/products"](
{
...queryParams,
const { products } = await clientGraphql.query<
ProductDetailsGraphQL,
ProductDetailsInputs
>(
{
variables: {
filter: { url_key: { eq: slug } },
},
STALE,
).then((res) => res.json());
if (!itemSku.items.length) return null;
return itemSku.items[0].sku;
};

const getFullProduct = async (sku: string) =>
await clientGraphql.query<ProductDetailsGraphQL, ProductDetailsInputs>(
{
variables: {
filter: { sku: { eq: sku } },
},
...GetCompleteProduct(customAttributes, isBreadcrumbProductName),
},
STALE,
);
...GetCompleteProduct(customAttributes, isBreadcrumbProductName),
},
STALE,
);

const sku = await getProductSku();
const product = products.items.find((p) => p.url_key === slug);

if (!sku) {
console.log("product not found");
if (!product) {
return null;
}
const { products } = await getFullProduct(sku);

const productCanonicalUrl = new URL(
(defaultPath ?? "") + products.items[0]?.canonical_url ?? "",
(defaultPath ?? "") + product?.canonical_url ?? "",
url.origin,
);

const productListElement = {
"@type": "ListItem",
item: productCanonicalUrl.href,
position: 1,
name: products.items[0]?.name ?? "",
name: product?.name ?? "",
} as ListItem;

const itemListElement: ListItem[] = isBreadcrumbProductName
? [productListElement]
: products.items[0].categories?.map(
: product.categories?.map(
({ position, url_path, url_key, name }) => ({
"@type": "ListItem",
item:
Expand All @@ -133,7 +102,7 @@ async function loader(
itemListElement,
numberOfItems: itemListElement.length,
},
product: toProductGraphQL(products.items[0], {
product: toProductGraphQL(product, {
originURL: url,
imagesQtd: 999,
customAttributes,
Expand All @@ -142,9 +111,9 @@ async function loader(
defaultPath,
}),
seo: {
title: products.items[0].meta_title?.trim() ??
products.items[0].name.trim() ?? "",
description: products.items[0].meta_description?.trim() ?? "",
title: product.meta_title?.trim() ??
product.name.trim() ?? "",
description: product.meta_description?.trim() ?? "",
canonical: productCanonicalUrl.href,
},
};
Expand Down
2 changes: 1 addition & 1 deletion magento/utils/clientGraphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export interface ProductSearchInputs {

export interface ProductDetailsInputs {
filter: {
sku: {
url_key: {
eq: string;
};
};
Expand Down