Skip to content

Commit

Permalink
Merge pull request #2308 from graphcommerce-org/feature/GCOM-1165-dra…
Browse files Browse the repository at this point in the history
…ft-mode

Preview mode toolbar
  • Loading branch information
paales authored Jul 4, 2024
2 parents 3fa00b1 + 7357ba1 commit 61639e5
Show file tree
Hide file tree
Showing 91 changed files with 872 additions and 287 deletions.
7 changes: 7 additions & 0 deletions .changeset/sweet-tips-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphcommerce/hygraph-dynamic-rows-ui': patch
'@graphcommerce/graphcms-ui': patch
'@graphcommerce/graphql': patch
---

Added Draft Mode support. When enabled it will be shown.
2 changes: 2 additions & 0 deletions docs/framework/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ Enable customer account deletion through the account section

#### dataLayer: [DatalayerConfig](#DatalayerConfig)

Datalayer config

#### debug: [GraphCommerceDebugConfig](#GraphCommerceDebugConfig)

Debug configuration for GraphCommerce
Expand Down
6 changes: 4 additions & 2 deletions docs/framework/static-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ data needed to render the layout (header, footer, menu etc.)

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
...

const layout = staticClient.query({ query: LayoutDocument, fetchPolicy: 'cache-first' })
const layout = staticClient.query({
query: LayoutDocument,
fetchPolicy: cacheFirst(staticClient),
})

return {
props: {
Expand Down
22 changes: 10 additions & 12 deletions docs/getting-started/pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ AboutUs.pageOptions = pageOptions

export default AboutUs

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
const staticClient = graphqlSsrClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const staticClient = graphqlSsrClient(context)

const conf = client.query({ query: StoreConfigDocument })
const layout = staticClient.query({
Expand Down Expand Up @@ -210,9 +210,9 @@ AboutUs.pageOptions = pageOptions

export default AboutUs

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
const staticClient = graphqlSsrClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const staticClient = graphqlSsrClient(context)

const conf = client.query({ query: StoreConfigDocument })
const page = hygraphPageContent(staticClient, 'about/about-us')
Expand Down Expand Up @@ -259,12 +259,10 @@ export const getStaticPaths: GetPageStaticPaths = (context) => ({
fallback: 'blocking',
})

export const getStaticProps: GetPageStaticProps = async ({
locale,
params,
}) => {
const client = graphqlSharedClient(locale)
const staticClient = graphqlSsrClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const { params } = context
const client = graphqlSharedClient(context)
const staticClient = graphqlSsrClient(context)

const conf = client.query({ query: StoreConfigDocument })
const page = hygraphPageContent(staticClient, `about/${params?.url}`)
Expand Down
4 changes: 3 additions & 1 deletion examples/magento-graphcms/.meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ sources:
endpoint: '{graphCommerce.hygraphEndpoint}'
operationHeaders:
gcms-locales: "{context.headers['gcms-locales']}"
gcms-stage: "{context.headers['gcms-stage']}"
transforms:
- filterSchema:
filters:
Expand All @@ -14,7 +15,7 @@ sources:
# Remove queries: `query { node, row*, asset*, scheduled*, *Version, user* }`
- 'Query.!{node,asset*,scheduled*,*Version,user*}'
# Remove field arguments: `query { anyfield(after,before,last,forceParentLocale,locales) { ... } }`
- '*.*.!{after,before,last,forceParentLocale,locales}'
- '*.*.!{after,before,last,forceParentLocale,locales,stage}'
# Remove type any input or type fields: `input MyInput {}` or `type MyType { anyfield }`
- '*.!{localizations,scheduledIn,documentInStages*,createdAt*,updatedAt*,publishedAt*,createdBy,updatedBy,publishedBy,history,scheduledIn*}'
- prune:
Expand All @@ -31,5 +32,6 @@ sources:
X-ReCaptcha: "{context.headers['x-recaptcha']}"
Preview-Version: "{context.headers['preview-version']}"
Content-Currency: "{context.headers['content-currency']}"
X-Magento-Cache-Id: "{context.headers['x-magento-cache-id']}"
serve:
playground: true
41 changes: 15 additions & 26 deletions examples/magento-graphcms/lib/graphql/graphqlSsrClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
NormalizedCacheObject,
ApolloClient,
ApolloLink,
errorLink,
Expand All @@ -9,14 +8,18 @@ import {
graphqlConfig,
mergeTypePolicies,
FetchPolicy,
DefaultOptions,
PreviewConfig,
} from '@graphcommerce/graphql'
import { MeshApolloLink, getBuiltMesh } from '@graphcommerce/graphql-mesh'
import { storefrontConfig, storefrontConfigDefault } from '@graphcommerce/next-ui'
import { GetStaticPropsContext } from 'next'
import { i18nSsrLoader } from '../i18n/I18nProvider'

function client(locale: string | undefined, fetchPolicy: FetchPolicy = 'no-cache') {
function client(context: GetStaticPropsContext, fetchPolicy: FetchPolicy = 'no-cache') {
const config = graphqlConfig({
storefront: storefrontConfig(locale) ?? storefrontConfigDefault(),
storefront: storefrontConfig(context.locale) ?? storefrontConfigDefault(),
...(context as PreviewConfig),
})

return new ApolloClient({
Expand All @@ -32,36 +35,22 @@ function client(locale: string | undefined, fetchPolicy: FetchPolicy = 'no-cache
}),
ssrMode: true,
name: 'ssr',
defaultOptions: { query: { errorPolicy: 'all', fetchPolicy } },
defaultOptions: {
preview: context as PreviewConfig,
query: { errorPolicy: 'all', fetchPolicy },
} as DefaultOptions,
})
}

const sharedClient: {
[locale: string]: ApolloClient<NormalizedCacheObject>
} = {}

/**
* Any queries made with the graphqlSharedClient will be send to the browser and injected in the
* browser's cache.
*/
export function graphqlSharedClient(locale: string | undefined) {
if (!locale) return client(locale, 'cache-first')

// Create a client if it doesn't exist for the locale.
if (!sharedClient[locale]) sharedClient[locale] = client(locale, 'cache-first')
return sharedClient[locale]
export function graphqlSharedClient(context: GetStaticPropsContext) {
return client(context, 'cache-first')
}

const ssrClient: {
[locale: string]: ApolloClient<NormalizedCacheObject>
} = {}

export function graphqlSsrClient(locale: string | undefined) {
i18nSsrLoader(locale)
if (!locale) return client(locale, 'no-cache')

// Create a client if it doesn't exist for the locale.
if (!ssrClient[locale]) ssrClient[locale] = client(locale, 'no-cache')

return ssrClient[locale]
export function graphqlSsrClient(context: GetStaticPropsContext) {
i18nSsrLoader(context.locale)
return client(context, 'no-cache')
}
12 changes: 8 additions & 4 deletions examples/magento-graphcms/pages/404.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Box, Container, Typography, Link } from '@mui/material'
import React from 'react'
import { LayoutDocument, LayoutNavigation, LayoutNavigationProps } from '../components'
import { graphqlSsrClient, graphqlSharedClient } from '../lib/graphql/graphqlSsrClient'
import { cacheFirst } from '@graphcommerce/graphql'

type Props = Record<string, unknown>
type GetPageStaticProps = GetStaticProps<LayoutNavigationProps, Props>
Expand Down Expand Up @@ -61,11 +62,14 @@ RouteNotFoundPage.pageOptions = {

export default RouteNotFoundPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
const staticClient = graphqlSsrClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const staticClient = graphqlSsrClient(context)
const conf = client.query({ query: StoreConfigDocument })
const layout = staticClient.query({ query: LayoutDocument, fetchPolicy: 'cache-first' })
const layout = staticClient.query({
query: LayoutDocument,
fetchPolicy: cacheFirst(staticClient),
})

return {
props: {
Expand Down
16 changes: 10 additions & 6 deletions examples/magento-graphcms/pages/[...url].tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PageOptions } from '@graphcommerce/framer-next-pages'
import { Asset, hygraphPageContent, HygraphPagesQuery } from '@graphcommerce/graphcms-ui'
import { flushMeasurePerf } from '@graphcommerce/graphql'
import { cacheFirst, flushMeasurePerf } from '@graphcommerce/graphql'
import {
appendSiblingsAsChildren,
CategoryBreadcrumbs,
Expand Down Expand Up @@ -140,26 +140,30 @@ export const getStaticPaths: GetPageStaticPaths = async ({ locales = [] }) => {
// Disable getStaticPaths while in development mode
if (process.env.NODE_ENV === 'development') return { paths: [], fallback: 'blocking' }

const path = (loc: string) => getCategoryStaticPaths(graphqlSsrClient(loc), loc)
const path = (locale: string) => getCategoryStaticPaths(graphqlSsrClient({ locale }), locale)
const paths = (await Promise.all(locales.map(path))).flat(1)
return { paths, fallback: 'blocking' }
}

export const getStaticProps: GetPageStaticProps = async ({ params, locale }) => {
export const getStaticProps: GetPageStaticProps = async (context) => {
const { params, locale } = context
const [url, query] = extractUrlQuery(params)
if (!url || !query) return { notFound: true }

const client = graphqlSharedClient(locale)
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })
const filterTypes = getFilterTypes(client)

const staticClient = graphqlSsrClient(locale)
const staticClient = graphqlSsrClient(context)

const categoryPage = staticClient.query({
query: CategoryPageDocument,
variables: { url },
})
const layout = staticClient.query({ query: LayoutDocument, fetchPolicy: 'cache-first' })
const layout = staticClient.query({
query: LayoutDocument,
fetchPolicy: cacheFirst(staticClient),
})

const productListParams = parseParams(url, query, await filterTypes)
const filteredCategoryUid = productListParams && productListParams.filters.category_uid?.in?.[0]
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/account/addresses/add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ AddNewAddressPage.pageOptions = pageOptions

export default AddNewAddressPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })

return {
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/account/addresses/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ EditAddressPage.pageOptions = pageOptions

export default EditAddressPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })

return {
Expand Down
6 changes: 3 additions & 3 deletions examples/magento-graphcms/pages/account/addresses/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ AccountAddressesPage.pageOptions = pageOptions

export default AccountAddressesPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
const staticClient = graphqlSsrClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const staticClient = graphqlSsrClient(context)
const conf = client.query({ query: StoreConfigDocument })

const countryRegions = staticClient.query({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ AccountAuthenticationPage.pageOptions = pageOptions

export default AccountAuthenticationPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })

return {
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/account/contact/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ AccountContactPage.pageOptions = pageOptions

export default AccountContactPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })

return {
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/account/delete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ AccountDeletePage.pageOptions = pageOptions

export default AccountDeletePage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })
if (
import.meta.graphCommerce.magentoVersion < 246 ||
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/account/forgot-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ AccountForgotPasswordPage.pageOptions = pageOptions

export default AccountForgotPasswordPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })

return {
Expand Down
13 changes: 8 additions & 5 deletions examples/magento-graphcms/pages/account/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PageOptions } from '@graphcommerce/framer-next-pages'
import { useQuery } from '@graphcommerce/graphql'
import { cacheFirst, useQuery } from '@graphcommerce/graphql'
import {
AccountDashboardDocument,
AccountMenu,
Expand Down Expand Up @@ -167,11 +167,14 @@ AccountIndexPage.pageOptions = pageOptions

export default AccountIndexPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const staticClient = graphqlSsrClient(locale)
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const staticClient = graphqlSsrClient(context)
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })
const layout = staticClient.query({ query: LayoutDocument, fetchPolicy: 'cache-first' })
const layout = staticClient.query({
query: LayoutDocument,
fetchPolicy: cacheFirst(staticClient),
})

return {
props: {
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/account/name/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ AccountNamePage.pageOptions = pageOptions

export default AccountNamePage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })

return {
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/account/orders/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ AccountOrdersPage.pageOptions = pageOptions

export default AccountOrdersPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })

return {
Expand Down
6 changes: 3 additions & 3 deletions examples/magento-graphcms/pages/account/orders/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ OrderDetailPage.pageOptions = pageOptions

export default OrderDetailPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
const staticClient = graphqlSsrClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const staticClient = graphqlSsrClient(context)
const config = client.query({ query: StoreConfigDocument })

const countryRegions = staticClient.query({
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/account/reviews/add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ AccountReviewsAddPage.pageOptions = pageOptions

export default AccountReviewsAddPage

export const getStaticProps: GetPageStaticProps = async ({ locale }) => {
const client = graphqlSharedClient(locale)
export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })

return {
Expand Down
Loading

0 comments on commit 61639e5

Please sign in to comment.