From 7a9512203b3a49b682a6024970343298d72025e5 Mon Sep 17 00:00:00 2001 From: yuriassuncx Date: Wed, 5 Feb 2025 11:50:59 -0300 Subject: [PATCH] feat: add shop loader and GraphQL query for shop information --- shopify/loaders/shop.ts | 33 +++++++++++++++ shopify/manifest.gen.ts | 6 ++- shopify/utils/storefront/queries.ts | 66 ++++++++++++++++++++++++++--- 3 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 shopify/loaders/shop.ts diff --git a/shopify/loaders/shop.ts b/shopify/loaders/shop.ts new file mode 100644 index 000000000..60b2bb3bf --- /dev/null +++ b/shopify/loaders/shop.ts @@ -0,0 +1,33 @@ +import { AppContext } from "../mod.ts"; +import { GetShopInfo } from "../utils/storefront/queries.ts"; +import { + Shop, + ShopMetafieldsArgs, +} from "../utils/storefront/storefront.graphql.gen.ts"; +import { Metafield } from "../utils/types.ts"; + +export interface Props { + /** + * @title Metafields + * @description search for metafields + */ + metafields?: Metafield[]; +} + +const loader = async ( + props: Props, + _req: Request, + ctx: AppContext, +): Promise => { + const { storefront } = ctx; + const { metafields = [] } = props; + + const shop = await storefront.query<{ shop: Shop }, ShopMetafieldsArgs>({ + variables: { identifiers: metafields }, + ...GetShopInfo, + }).then((data) => data.shop); + + return shop; +}; + +export default loader; diff --git a/shopify/manifest.gen.ts b/shopify/manifest.gen.ts index 62ed27d96..fc64c5355 100644 --- a/shopify/manifest.gen.ts +++ b/shopify/manifest.gen.ts @@ -15,7 +15,8 @@ import * as $$$1 from "./loaders/ProductList.ts"; import * as $$$2 from "./loaders/ProductListingPage.ts"; import * as $$$5 from "./loaders/proxy.ts"; import * as $$$3 from "./loaders/RelatedProducts.ts"; -import * as $$$6 from "./loaders/user.ts"; +import * as $$$6 from "./loaders/shop.ts"; +import * as $$$7 from "./loaders/user.ts"; const manifest = { "loaders": { @@ -25,7 +26,8 @@ const manifest = { "shopify/loaders/ProductListingPage.ts": $$$2, "shopify/loaders/proxy.ts": $$$5, "shopify/loaders/RelatedProducts.ts": $$$3, - "shopify/loaders/user.ts": $$$6, + "shopify/loaders/shop.ts": $$$6, + "shopify/loaders/user.ts": $$$7, }, "handlers": { "shopify/handlers/sitemap.ts": $$$$0, diff --git a/shopify/utils/storefront/queries.ts b/shopify/utils/storefront/queries.ts index ccf6ce04f..1e7b8cdbc 100644 --- a/shopify/utils/storefront/queries.ts +++ b/shopify/utils/storefront/queries.ts @@ -261,10 +261,10 @@ const Customer = gql` export const CreateCart = { query: gql`mutation CreateCart { - payload: cartCreate { - cart { id } - } -}`, + payload: cartCreate { + cart { id } + } + }`, }; export const GetCart = { @@ -276,8 +276,8 @@ export const GetProduct = { fragments: [Product, ProductVariant, Collection], query: gql`query GetProduct($handle: String, $identifiers: [HasMetafieldsIdentifier!]!) { - product(handle: $handle) { ...Product } - }`, + product(handle: $handle) { ...Product } + }`, }; export const ListProducts = { @@ -386,6 +386,60 @@ export const ProductRecommendations = { }`, }; +export const GetShopInfo = { + query: gql`query GetShopInfo($identifiers: [HasMetafieldsIdentifier!]!) { + shop { + name + description + privacyPolicy { + title + body + } + refundPolicy { + title + body + } + shippingPolicy { + title + body + } + subscriptionPolicy { + title + body + } + termsOfService { + title + body + } + metafields(identifiers: $identifiers) { + description + key + namespace + type + value + reference { + ... on MediaImage { + image { + url + } + } + } + references(first: 250) { + edges { + node { + ... on MediaImage { + image { + url + } + } + } + } + } + } + } + }`, +}; + export const FetchCustomerInfo = { fragments: [Customer], query: gql`query FetchCustomerInfo($customerAccessToken: String!) {