Skip to content

Commit

Permalink
Parameterize all queries with channel (#1045)
Browse files Browse the repository at this point in the history
Fixes #1028
  • Loading branch information
typeofweb authored Nov 27, 2023
1 parent 8d34c87 commit dbc8277
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@
"pnpm": ">=8.9.0",
"node": ">=18"
}
}
}
5 changes: 3 additions & 2 deletions src/app/(main)/categories/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { type ResolvingMetadata, type Metadata } from "next";
import { ProductListByCategoryDocument } from "@/gql/graphql";
import { executeGraphQL } from "@/lib/graphql";
import { ProductList } from "@/ui/components/ProductList";
import { DEFAULT_CHANNEL } from "@/checkout/lib/regions";

export const generateMetadata = async (
{ params }: { params: { slug: string } },
parent: ResolvingMetadata,
): Promise<Metadata> => {
const { category } = await executeGraphQL(ProductListByCategoryDocument, {
variables: { slug: params.slug },
variables: { slug: params.slug, channel: DEFAULT_CHANNEL },
revalidate: 60,
});

Expand All @@ -21,7 +22,7 @@ export const generateMetadata = async (

export default async function Page({ params }: { params: { slug: string } }) {
const { category } = await executeGraphQL(ProductListByCategoryDocument, {
variables: { slug: params.slug },
variables: { slug: params.slug, channel: DEFAULT_CHANNEL },
revalidate: 60,
});

Expand Down
5 changes: 3 additions & 2 deletions src/app/(main)/collections/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { type ResolvingMetadata, type Metadata } from "next";
import { ProductListByCollectionDocument } from "@/gql/graphql";
import { executeGraphQL } from "@/lib/graphql";
import { ProductList } from "@/ui/components/ProductList";
import { DEFAULT_CHANNEL } from "@/checkout/lib/regions";

export const generateMetadata = async (
{ params }: { params: { slug: string } },
parent: ResolvingMetadata,
): Promise<Metadata> => {
const { collection } = await executeGraphQL(ProductListByCollectionDocument, {
variables: { slug: params.slug },
variables: { slug: params.slug, channel: DEFAULT_CHANNEL },
revalidate: 60,
});

Expand All @@ -22,7 +23,7 @@ export const generateMetadata = async (

export default async function Page({ params }: { params: { slug: string } }) {
const { collection } = await executeGraphQL(ProductListByCollectionDocument, {
variables: { slug: params.slug },
variables: { slug: params.slug, channel: DEFAULT_CHANNEL },
revalidate: 60,
});

Expand Down
2 changes: 2 additions & 0 deletions src/app/(main)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEFAULT_CHANNEL } from "@/checkout/lib/regions";
import { ProductListByCollectionDocument } from "@/gql/graphql";
import { executeGraphQL } from "@/lib/graphql";
import { ProductList } from "@/ui/components/ProductList";
Expand All @@ -12,6 +13,7 @@ export default async function Page() {
const data = await executeGraphQL(ProductListByCollectionDocument, {
variables: {
slug: "featured-products",
channel: DEFAULT_CHANNEL,
},
revalidate: 60,
});
Expand Down
5 changes: 4 additions & 1 deletion src/app/(main)/products/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { formatMoney, formatMoneyRange } from "@/lib/utils";
import { CheckoutAddLineDocument, ProductDetailsDocument, ProductListDocument } from "@/gql/graphql";
import * as Checkout from "@/lib/checkout";
import { AvailabilityMessage } from "@/ui/components/AvailabilityMessage";
import { DEFAULT_CHANNEL } from "@/checkout/lib/regions";

const shouldUseHttps =
process.env.NEXT_PUBLIC_STOREFRONT_URL?.startsWith("https") || !!process.env.NEXT_PUBLIC_VERCEL_URL;
Expand All @@ -31,6 +32,7 @@ export async function generateMetadata(
const { product } = await executeGraphQL(ProductDetailsDocument, {
variables: {
slug: decodeURIComponent(params.slug),
channel: DEFAULT_CHANNEL,
},
revalidate: 60,
});
Expand Down Expand Up @@ -70,7 +72,7 @@ export async function generateMetadata(
export async function generateStaticParams() {
const { products } = await executeGraphQL(ProductListDocument, {
revalidate: 60,
variables: { first: 20 },
variables: { first: 20, channel: DEFAULT_CHANNEL },
withAuth: false,
});

Expand All @@ -86,6 +88,7 @@ export default async function Page(props: { params: { slug: string }; searchPara
const { product } = await executeGraphQL(ProductDetailsDocument, {
variables: {
slug: decodeURIComponent(params.slug),
channel: DEFAULT_CHANNEL,
},
revalidate: 60,
});
Expand Down
2 changes: 2 additions & 0 deletions src/app/(main)/products/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ProductListPaginatedDocument } from "@/gql/graphql";
import { executeGraphQL } from "@/lib/graphql";
import { Pagination } from "@/ui/components/Pagination";
import { ProductList } from "@/ui/components/ProductList";
import { DEFAULT_CHANNEL } from "@/checkout/lib/regions";
import { ProductsPerPage } from "@/app/config";

export const metadata = {
Expand All @@ -23,6 +24,7 @@ export default async function Page({ searchParams }: Props) {
variables: {
first: ProductsPerPage,
after: cursor,
channel: DEFAULT_CHANNEL,
},
revalidate: 60,
});
Expand Down
2 changes: 2 additions & 0 deletions src/app/(main)/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { OrderDirection, ProductOrderField, SearchProductsDocument } from "@/gql
import { executeGraphQL } from "@/lib/graphql";
import { Pagination } from "@/ui/components/Pagination";
import { ProductList } from "@/ui/components/ProductList";
import { DEFAULT_CHANNEL } from "@/checkout/lib/regions";
import { ProductsPerPage } from "@/app/config";

export const metadata = {
Expand Down Expand Up @@ -37,6 +38,7 @@ export default async function Page({ searchParams }: Props) {
after: cursor,
sortBy: ProductOrderField.Rating,
sortDirection: OrderDirection.Asc,
channel: DEFAULT_CHANNEL,
},
revalidate: 60,
});
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/CheckoutCreate.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mutation CheckoutCreate {
checkoutCreate(input: { channel: "default-channel", lines: [] }) {
mutation CheckoutCreate($channel: String!) {
checkoutCreate(input: { channel: $channel, lines: [] }) {
checkout {
id
email
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/MenuGetBySlug.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fragment MenuItem on MenuItem {
url
}

query MenuGetBySlug($slug: String!) {
menu(slug: $slug, channel: "default-channel") {
query MenuGetBySlug($slug: String!, $channel: String!) {
menu(slug: $slug, channel: $channel) {
items {
...MenuItem
children {
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/ProductDetails.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
query ProductDetails($slug: String!) {
product(slug: $slug, channel: "default-channel") {
query ProductDetails($slug: String!, $channel: String!) {
product(slug: $slug, channel: $channel) {
id
name
slug
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/ProductList.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
query ProductList($first: Int = 9) {
products(first: $first, channel: "default-channel") {
query ProductList($first: Int = 9, $channel: String!) {
products(first: $first, channel: $channel) {
edges {
node {
...ProductListItem
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/ProductListByCategory.graphql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
query ProductListByCategory($slug: String!) {
query ProductListByCategory($slug: String!, $channel: String!) {
category(slug: $slug) {
name
description
seoDescription
seoTitle
products(first: 100, channel: "default-channel") {
products(first: 100, channel: $channel) {
edges {
node {
...ProductListItem
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/ProductListByCollection.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
query ProductListByCollection($slug: String!) {
collection(slug: $slug, channel: "default-channel") {
query ProductListByCollection($slug: String!, $channel: String!) {
collection(slug: $slug, channel: $channel) {
name
description
seoDescription
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/ProductListPaginated.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
query ProductListPaginated($first: Int!, $after: String) {
products(first: $first, channel: "default-channel", after: $after) {
query ProductListPaginated($first: Int!, $after: String, $channel: String!) {
products(first: $first, after: $after, channel: $channel) {
totalCount
edges {
node {
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/SearchProducts.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ query SearchProducts(
$sortDirection: OrderDirection!
$first: Int!
$after: String
$channel: String!
) {
products(
first: $first
after: $after
channel: "default-channel"
channel: $channel
sortBy: { field: $sortBy, direction: $sortDirection }
filter: { search: $search }
) {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEFAULT_CHANNEL } from "@/checkout/lib/regions";
import { CheckoutCreateDocument, CheckoutFindDocument } from "@/gql/graphql";
import { executeGraphQL } from "@/lib/graphql";

Expand Down Expand Up @@ -26,4 +27,5 @@ export async function findOrCreate(checkoutId?: string) {
return checkout || (await create()).checkoutCreate?.checkout;
}

export const create = () => executeGraphQL(CheckoutCreateDocument, { cache: "no-cache" });
export const create = () =>
executeGraphQL(CheckoutCreateDocument, { cache: "no-cache", variables: { channel: DEFAULT_CHANNEL } });
3 changes: 2 additions & 1 deletion src/ui/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Link from "next/link";
import { MenuGetBySlugDocument } from "@/gql/graphql";
import { executeGraphQL } from "@/lib/graphql";
import { DEFAULT_CHANNEL } from "@/checkout/lib/regions";

export async function Footer() {
const footerLinks = await executeGraphQL(MenuGetBySlugDocument, {
variables: { slug: "footer" },
variables: { slug: "footer", channel: DEFAULT_CHANNEL },
revalidate: 60 * 60 * 24,
});
const currentYear = new Date().getFullYear();
Expand Down
3 changes: 2 additions & 1 deletion src/ui/components/nav/components/NavLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Link from "next/link";
import { NavLink } from "./NavLink";
import { executeGraphQL } from "@/lib/graphql";
import { MenuGetBySlugDocument } from "@/gql/graphql";
import { DEFAULT_CHANNEL } from "@/checkout/lib/regions";

export const NavLinks = async () => {
const navLinks = await executeGraphQL(MenuGetBySlugDocument, {
variables: { slug: "navbar" },
variables: { slug: "navbar", channel: DEFAULT_CHANNEL },
revalidate: 60 * 60 * 24,
});

Expand Down
3 changes: 1 addition & 2 deletions src/ui/components/nav/components/UserMenu/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export function UserMenu({ user }: Props) {
<div className="flex flex-col px-1 py-1">
<Menu.Item>
{({ active }) => (

<form action={logout}>
<button
type="submit"
Expand All @@ -66,6 +65,6 @@ export function UserMenu({ user }: Props) {
</div>
</Menu.Items>
</Transition>
</Menu >
</Menu>
);
}

0 comments on commit dbc8277

Please sign in to comment.