Changed to query page by id

This commit is contained in:
cond0r 2021-02-26 09:12:44 +02:00
parent 1c7ac7d795
commit 590bff576c
5 changed files with 21 additions and 19 deletions

View File

@ -8,7 +8,6 @@ import {
} from '../const'
if (!API_URL) {
console.log(process.env)
throw new Error(
`The environment variable NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN is missing and it's required to access your store`
)

View File

@ -25,12 +25,14 @@ const getAllPages = async (options?: {
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { locale = 'en-US' } = config
const { data } = await config.fetch(getAllPagesQuery, { variables })
const pages = data.pages?.edges?.map(
({ node: { title: name, handle, ...node } }: PageEdge) => ({
...node,
url: `/${handle}`,
url: `/${locale}/${handle}`,
name,
})
)

View File

@ -2,34 +2,33 @@ import { getConfig, ShopifyConfig } from '../api'
import getPageQuery from '../utils/queries/get-page-query'
import { Page } from './get-all-pages'
type Variables = {
slug: string
type PageVariables = {
id: string
}
type ReturnType = {
page: Page
}
export type GetPageResult<T extends { page?: any } = { page?: Page }> = T
const getPage = async (options: {
variables: Variables
variables: PageVariables
config: ShopifyConfig
preview?: boolean
}): Promise<ReturnType> => {
}): Promise<GetPageResult> => {
let { config, variables } = options ?? {}
config = getConfig(config)
const { locale = 'en-US' } = config
const { data } = await config.fetch(getPageQuery, {
variables,
})
const { pageByHandle: page } = data
const page = data.node
return {
page: page
? {
...page,
name: page.title,
url: page?.handle,
url: `/${locale}/${page.handle}`,
}
: null,
}

View File

@ -23,6 +23,7 @@ export type SearchProductsData = {
products: Product[]
found: boolean
}
export const handler: SWRHook<
SearchProductsData,
SearchProductsInput,

View File

@ -1,12 +1,13 @@
export const getPageQuery = /* GraphQL */ `
query getPageBySlug($slug: String!) {
pageByHandle(handle: $slug) {
query($id: ID!) {
node(id: $id) {
id
title
handle
body
bodySummary
url
... on Page {
title
handle
body
bodySummary
}
}
}
`