@graphcommerce/graphcms-ui 9.0.0-canary.98 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +0,0 @@
1
- query PagesStaticPaths($urlStartsWith: String!, $first: Int = 1000) {
2
- pages(where: { url_starts_with: $urlStartsWith }, first: $first) {
3
- url
4
- metaRobots
5
- }
6
- }
package/graphql/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './HygraphAllPages.gql'
2
- export * from './HygraphPage.gql'
3
- export * from './HygraphPages.gql'
4
- export * from './PageLink.gql'
5
- export * from './PagesStaticPaths.gql'
@@ -1,43 +0,0 @@
1
- import type { ApolloClient, NormalizedCacheObject, ApolloQueryResult } from '@apollo/client'
2
- import { cacheFirst } from '@graphcommerce/graphql'
3
- import { HygraphAllPagesDocument, HygraphAllPagesQuery } from '../graphql'
4
-
5
- type Urls = { url: string }
6
-
7
- export async function getAllHygraphPages(
8
- client: ApolloClient<NormalizedCacheObject>,
9
- options: { pageSize?: number } = {},
10
- ) {
11
- const { pageSize = 100 } = options
12
-
13
- const query = client.query({
14
- query: HygraphAllPagesDocument,
15
- variables: { first: pageSize },
16
- fetchPolicy: cacheFirst(client),
17
- })
18
- const pages: Promise<ApolloQueryResult<HygraphAllPagesQuery>>[] = [query]
19
-
20
- const { data } = await query
21
- const totalPages = Math.ceil(data.pagesConnection.aggregate.count / pageSize) ?? 1
22
-
23
- if (totalPages > 1) {
24
- for (let i = 2; i <= totalPages; i++) {
25
- pages.push(
26
- client.query({
27
- query: HygraphAllPagesDocument,
28
- variables: { first: pageSize, skip: pageSize * (i - 1) },
29
- fetchPolicy: cacheFirst(client),
30
- }),
31
- )
32
- }
33
- }
34
-
35
- const paths: Urls[] = (await Promise.all(pages))
36
- .map((q) => q.data.pages)
37
- .flat(1)
38
- .map((page) => ({
39
- url: page.url,
40
- }))
41
-
42
- return paths
43
- }
@@ -1,43 +0,0 @@
1
- import { ApolloClient, NormalizedCacheObject, ApolloQueryResult } from '@apollo/client'
2
- import type { PageWhereInput } from '@graphcommerce/graphql-mesh'
3
- import { GetStaticPathsResult } from 'next'
4
- import {
5
- HygraphStaticPathsDocument,
6
- HygraphStaticPathsQuery,
7
- } from '../graphql/HygraphStaticPaths.gql'
8
-
9
- type Return = GetStaticPathsResult<{ url: string }>
10
-
11
- export async function getHygraphStaticPaths(
12
- client: ApolloClient<NormalizedCacheObject>,
13
- locale: string,
14
- options: { pageSize?: number; filter?: PageWhereInput } = {},
15
- ) {
16
- const { pageSize = 100, filter = {} } = options
17
- const query = client.query({
18
- query: HygraphStaticPathsDocument,
19
- variables: { pageSize, where: filter },
20
- })
21
- const pages: Promise<ApolloQueryResult<HygraphStaticPathsQuery>>[] = [query]
22
-
23
- const { data } = await query
24
- const totalPages = Math.ceil(data.pagesConnection.aggregate.count / pageSize) ?? 1
25
-
26
- if (totalPages > 1) {
27
- for (let i = 2; i <= totalPages; i++) {
28
- pages.push(
29
- client.query({
30
- query: HygraphStaticPathsDocument,
31
- variables: { pageSize, skip: pageSize * (i - 1), where: filter },
32
- }),
33
- )
34
- }
35
- }
36
-
37
- const paths: Return['paths'] = (await Promise.all(pages))
38
- .map((q) => q.data.pages)
39
- .flat(1)
40
- .map((page) => ({ params: page, locale }))
41
-
42
- return paths
43
- }
@@ -1,48 +0,0 @@
1
- import { ApolloClient, NormalizedCacheObject } from '@graphcommerce/graphql'
2
- import { HygraphPagesQuery, HygraphPagesDocument } from '../graphql'
3
- import { getAllHygraphPages } from './getAllHygraphPages'
4
-
5
- /**
6
- * Fetch the page content for the given urls.
7
- *
8
- * - Uses an early bailout to check to reduce hygraph calls.
9
- * - Implements an alias sytem to merge the content of multiple pages.
10
- */
11
- async function pageContent(
12
- client: ApolloClient<NormalizedCacheObject>,
13
- url: string,
14
- cached: boolean,
15
- ): Promise<{ data: HygraphPagesQuery }> {
16
- /**
17
- * Some routes are very generic and wil be requested very often, like 'product/global'. To reduce
18
- * the amount of requests to Hygraph we can cache the result of the query if requested.
19
- *
20
- * This only works in a persistent nodejs environment and doesn't work in a serverless
21
- * environment, because those instances get discarded.
22
- *
23
- * This comes with a downside, if the page is updated the cache will not be invalidated, resulting
24
- * in stale data.
25
- *
26
- * Todo: Implement next.js 13 fetch revalidation:
27
- * https://beta.nextjs.org/docs/data-fetching/fetching#revalidating-data
28
- */
29
- const alwaysCache = process.env.NODE_ENV !== 'development' ? 'cache-first' : undefined
30
- const fetchPolicy = cached ? alwaysCache : undefined
31
-
32
- const allRoutes = await getAllHygraphPages(client)
33
- // Only do the query when there the page is found in the allRoutes
34
- const found = allRoutes.some((page) => page.url === url)
35
-
36
- return found
37
- ? client.query({ query: HygraphPagesDocument, variables: { url }, fetchPolicy })
38
- : Promise.resolve({ data: { pages: [] } })
39
- }
40
-
41
- export async function hygraphPageContent(
42
- client: ApolloClient<NormalizedCacheObject>,
43
- url: string,
44
- additionalProperties?: Promise<object> | object,
45
- cached = false,
46
- ): Promise<{ data: HygraphPagesQuery }> {
47
- return pageContent(client, url, cached)
48
- }
package/lib/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './hygraphPageContent'
2
- export * from './getHygraphPaths'
@@ -1,80 +0,0 @@
1
- import {
2
- usePreviewModeForm,
3
- type PreviewModeToolbarProps,
4
- SelectElement,
5
- previewModeDefaults,
6
- } from '@graphcommerce/ecommerce-ui'
7
- import { TypedDocumentNode, gql, useQuery } from '@graphcommerce/graphql'
8
- import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
9
- import { filterNonNullableKeys } from '@graphcommerce/next-ui'
10
- import React, { useMemo } from 'react'
11
-
12
- export const config: PluginConfig = {
13
- type: 'component',
14
- module: '@graphcommerce/ecommerce-ui',
15
- }
16
-
17
- const ContentStages = gql`
18
- query {
19
- __type(name: "Stage") {
20
- name
21
- enumValues {
22
- name
23
- description
24
- }
25
- }
26
- }
27
- ` as TypedDocumentNode<{
28
- __type: {
29
- name: 'Stage'
30
- enumValues: {
31
- name: string
32
- description: string
33
- }[]
34
- }
35
- }>
36
-
37
- const HygraphConfig = React.memo(() => {
38
- const form = usePreviewModeForm()
39
- const { control } = form
40
-
41
- const contentStages = useQuery(ContentStages)
42
-
43
- const defaultValue = previewModeDefaults().hygraphStage ?? 'PUBLISHED'
44
-
45
- return useMemo(
46
- () => (
47
- <SelectElement
48
- control={control}
49
- name='previewData.hygraphStage'
50
- defaultValue={defaultValue}
51
- color='secondary'
52
- select
53
- label='Hygraph Stage'
54
- size='small'
55
- sx={{ width: '150px' }}
56
- SelectProps={{ MenuProps: { style: { zIndex: 20000 } } }}
57
- onChange={() => {}}
58
- options={
59
- contentStages.loading
60
- ? [{ id: defaultValue, label: defaultValue }]
61
- : filterNonNullableKeys(contentStages.data?.__type.enumValues).map(({ name }) => ({
62
- id: name,
63
- label: name,
64
- }))
65
- }
66
- />
67
- ),
68
- [contentStages.data?.__type.enumValues, contentStages.loading, control],
69
- )
70
- })
71
-
72
- export const PreviewModeToolbar = (props: PluginProps<PreviewModeToolbarProps>) => {
73
- const { Prev, ...rest } = props
74
- return (
75
- <>
76
- <Prev {...rest} />
77
- <HygraphConfig />
78
- </>
79
- )
80
- }
@@ -1,35 +0,0 @@
1
- import { graphqlConfig as graphqlConfigType, setContext } from '@graphcommerce/graphql'
2
- import type { FunctionPlugin, PluginConfig } from '@graphcommerce/next-config'
3
-
4
- export const config: PluginConfig = {
5
- type: 'function',
6
- module: '@graphcommerce/graphql',
7
- }
8
-
9
- declare module '@graphcommerce/graphql/config' {
10
- interface PreviewData {
11
- hygraphStage?: string
12
- }
13
- }
14
-
15
- export const graphqlConfig: FunctionPlugin<typeof graphqlConfigType> = (prev, config) => {
16
- const results = prev(config)
17
-
18
- const locales = config.storefront.hygraphLocales
19
-
20
- if (!locales) return prev(config)
21
-
22
- const hygraphLink = setContext((_, context) => {
23
- if (!context.headers) context.headers = {}
24
- context.headers['gcms-locales'] = locales.join(',')
25
-
26
- const stage = config.previewData?.hygraphStage ?? 'DRAFT'
27
- if (config.preview) {
28
- context.headers['gcms-stage'] = stage
29
- }
30
-
31
- return context
32
- })
33
-
34
- return { ...results, links: [...results.links, hygraphLink] }
35
- }
@@ -1,12 +0,0 @@
1
- import { previewModeDefaults as base } from '@graphcommerce/ecommerce-ui'
2
- import type { FunctionPlugin, PluginConfig } from '@graphcommerce/next-config'
3
-
4
- export const config: PluginConfig = {
5
- type: 'function',
6
- module: '@graphcommerce/ecommerce-ui',
7
- }
8
-
9
- export const previewModeDefaults: FunctionPlugin<typeof base> = (prev, ...args) => ({
10
- ...prev(...args),
11
- hygraphStage: 'DRAFT',
12
- })