Skip to content

Commit

Permalink
Update examples with skeleton changes (#1667)
Browse files Browse the repository at this point in the history
  • Loading branch information
frandiox authored Jan 22, 2024
1 parent 9b5e6ea commit ec9cb2f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 172 deletions.
9 changes: 6 additions & 3 deletions examples/third-party-queries-caching/remix.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// Enhance TypeScript's built-in typings.
import '@total-typescript/ts-reset';

import type {Storefront, HydrogenCart} from '@shopify/hydrogen';
import type {AppSession} from './server';
import type {Storefront, CustomerClient, HydrogenCart} from '@shopify/hydrogen';
import type {AppSession} from '~/lib/session';
import {createRickAndMortyClient} from './app/lib/createRickAndMortyClient.server';

declare global {
/**
* A global `process` object is only available during build to access NODE_ENV
* A global `process` object is only available during build to access NODE_ENV.
*/
const process: {env: {NODE_ENV: 'production' | 'development'}};

Expand All @@ -24,6 +24,8 @@ declare global {
PRIVATE_STOREFRONT_API_TOKEN: string;
PUBLIC_STORE_DOMAIN: string;
PUBLIC_STOREFRONT_ID: string;
PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID: string;
PUBLIC_CUSTOMER_ACCOUNT_API_URL: string;
}
}

Expand All @@ -35,6 +37,7 @@ declare module '@shopify/remix-oxygen' {
env: Env;
cart: HydrogenCart;
storefront: Storefront;
customerAccount: CustomerClient;
rickAndMorty: ReturnType<typeof createRickAndMortyClient>;
session: AppSession;
waitUntil: ExecutionContext['waitUntil'];
Expand Down
186 changes: 17 additions & 169 deletions examples/third-party-queries-caching/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {
createCartHandler,
createStorefrontClient,
storefrontRedirect,
type HydrogenSession,
createCustomerClient,
} from '@shopify/hydrogen';
import {
createRequestHandler,
getStorefrontHeaders,
createCookieSessionStorage,
type SessionStorage,
type Session,
type AppLoadContext,
} from '@shopify/remix-oxygen';
import {AppSession} from '~/lib/session';
import {CART_QUERY_FRAGMENT} from '~/lib/fragments';

// 1. Import the Rick and Morty client.
import {createRickAndMortyClient} from './app/lib/createRickAndMortyClient.server';
Expand Down Expand Up @@ -57,6 +57,17 @@ export default {
storefrontHeaders: getStorefrontHeaders(request),
});

/**
* Create a client for Customer Account API.
*/
const customerAccount = createCustomerClient({
waitUntil,
request,
session,
customerAccountId: env.PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID,
customerAccountUrl: env.PUBLIC_CUSTOMER_ACCOUNT_API_URL,
});

/**
* 2. Create a Rick and Morty client.
*/
Expand All @@ -83,9 +94,10 @@ export default {
const handleRequest = createRequestHandler({
build: remixBuild,
mode: process.env.NODE_ENV,
getLoadContext: () => ({
getLoadContext: (): AppLoadContext => ({
session,
storefront,
customerAccount,
cart,
env,
waitUntil,
Expand All @@ -112,167 +124,3 @@ export default {
}
},
};

/**
* This is a custom session implementation for your Hydrogen shop.
* Feel free to customize it to your needs, add helper methods, or
* swap out the cookie-based implementation with something else!
*/
export class AppSession implements HydrogenSession {
#sessionStorage;
#session;

constructor(sessionStorage: SessionStorage, session: Session) {
this.#sessionStorage = sessionStorage;
this.#session = session;
}

static async init(request: Request, secrets: string[]) {
const storage = createCookieSessionStorage({
cookie: {
name: 'session',
httpOnly: true,
path: '/',
sameSite: 'lax',
secrets,
},
});

const session = await storage
.getSession(request.headers.get('Cookie'))
.catch(() => storage.getSession());

return new this(storage, session);
}

get has() {
return this.#session.has;
}

get get() {
return this.#session.get;
}

get flash() {
return this.#session.flash;
}

get unset() {
return this.#session.unset;
}

get set() {
return this.#session.set;
}

destroy() {
return this.#sessionStorage.destroySession(this.#session);
}

commit() {
return this.#sessionStorage.commitSession(this.#session);
}
}

// NOTE: https://shopify.dev/docs/api/storefront/latest/queries/cart
const CART_QUERY_FRAGMENT = `#graphql
fragment Money on MoneyV2 {
currencyCode
amount
}
fragment CartLine on CartLine {
id
quantity
attributes {
key
value
}
cost {
totalAmount {
...Money
}
amountPerQuantity {
...Money
}
compareAtAmountPerQuantity {
...Money
}
}
merchandise {
... on ProductVariant {
id
availableForSale
compareAtPrice {
...Money
}
price {
...Money
}
requiresShipping
title
image {
id
url
altText
width
height
}
product {
handle
title
id
}
selectedOptions {
name
value
}
}
}
}
fragment CartApiQuery on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
id
email
firstName
lastName
displayName
}
email
phone
}
lines(first: $numCartLines) {
nodes {
...CartLine
}
}
cost {
subtotalAmount {
...Money
}
totalAmount {
...Money
}
totalDutyAmount {
...Money
}
totalTaxAmount {
...Money
}
}
note
attributes {
key
value
}
discountCodes {
code
applicable
}
}
` as const;

0 comments on commit ec9cb2f

Please sign in to comment.