Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit f0918f5

Browse files
committed
Merge branch 'staging' into develop
2 parents f2049a6 + 026c6a1 commit f0918f5

22 files changed

+979
-211
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {initializeApollo} from '../connector'
2+
import queryPostsArchive from '../posts/queryPostsArchive'
3+
import {postTypes} from './postTypes'
4+
5+
/**
6+
* Retrieve post archive.
7+
*
8+
* @author WebDevStudios
9+
* @param {string} postType WP post type.
10+
* @param {string} orderBy Order by: field.
11+
* @param {string} order Order by: direction.
12+
* @param {string} cursor Start/end cursor for pagination.
13+
* @param {bool} getNext Whether to retrieve next set of posts (true) or previous set (false).
14+
* @param {number} perPage Number of posts per page.
15+
* @return {Object} Object containing Apollo client instance and post archive data or error object.
16+
*/
17+
export default async function getPostTypeArchive(
18+
postType,
19+
orderBy = 'DATE',
20+
order = 'DESC',
21+
cursor = null,
22+
getNext = true,
23+
perPage = 10
24+
) {
25+
// Define single post query based on post type.
26+
const postTypeQuery = {
27+
post: queryPostsArchive
28+
}
29+
30+
// Retrieve post type query.
31+
const query = postTypeQuery?.[postType] ?? null
32+
33+
// Get/create Apollo instance.
34+
const apolloClient = initializeApollo()
35+
36+
// Set up return object.
37+
const response = {
38+
apolloClient,
39+
error: false,
40+
errorMessage: null
41+
}
42+
43+
// If no query is set for given post type, return error message.
44+
if (!query) {
45+
return {
46+
apolloClient,
47+
error: true,
48+
errorMessage: `Post type \`${postType}\` archives are not supported.`
49+
}
50+
}
51+
52+
// Determine query variables.
53+
const variables = {
54+
first: getNext ? perPage : null, // Only used for retrieving next set.
55+
last: getNext ? null : perPage, // Only used for retrieving previous set.
56+
after: getNext ? cursor : null, // Only used for retrieving next set.
57+
before: getNext ? null : cursor, // Only used for retrieving previous set.
58+
orderBy,
59+
order
60+
}
61+
62+
// Execute query.
63+
response.posts = await apolloClient
64+
.query({query, variables})
65+
.then((posts) => {
66+
const pluralType = postTypes[postType] ?? postType
67+
68+
// Set error props if data not found.
69+
if (!posts?.data?.[pluralType]?.edges) {
70+
response.error = true
71+
response.errorMessage = `An error occurred while trying to retrieve data for ${pluralType} archive.`
72+
73+
return null
74+
}
75+
76+
// Flatten posts array to include inner node post data.
77+
return posts.data[pluralType].edges.map((post) => post.node)
78+
})
79+
.catch((error) => {
80+
response.error = true
81+
response.errorMessage = error.message
82+
83+
return null
84+
})
85+
86+
return response
87+
}

api/wordpress/_global/getPostTypeById.js

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import queryPostById from '../posts/queryPostById'
22
import {initializeApollo} from '../connector'
33
import queryPageById from '../pages/queryPageById'
44
import {isHierarchicalPostType} from './postTypes'
5+
import formatBlockData from '@/functions/formatBlockData'
56

67
/**
78
* Retrieve single post by specified identifier.
@@ -10,7 +11,7 @@ import {isHierarchicalPostType} from './postTypes'
1011
* @param {string} postType WP post type.
1112
* @param {Number|string} id Post identifier.
1213
* @param {string} idType Type of ID.
13-
* @return {Object} Post data or error object.
14+
* @return {Object} Object containing Apollo client instance and post data or error object.
1415
*/
1516
export default async function getPostTypeById(postType, id, idType = 'SLUG') {
1617
// Define single post query based on post type.
@@ -28,34 +29,59 @@ export default async function getPostTypeById(postType, id, idType = 'SLUG') {
2829
// Retrieve post type query.
2930
const query = postTypeQuery?.[postType] ?? null
3031

32+
// Get/create Apollo instance.
33+
const apolloClient = initializeApollo()
34+
35+
// Set up return object.
36+
const response = {
37+
apolloClient,
38+
error: false,
39+
errorMessage: null
40+
}
41+
3142
// If no query is set for given post type, return error message.
3243
if (!query) {
3344
return {
34-
isError: true,
35-
message: `Post type \`${postType}\` is not supported.`
45+
apolloClient,
46+
error: true,
47+
errorMessage: `Post type \`${postType}\` is not supported.`
3648
}
3749
}
3850

39-
// Get/create Apollo instance.
40-
const apolloClient = initializeApollo()
41-
4251
// Execute query.
43-
const post = await apolloClient
52+
response.post = await apolloClient
4453
.query({query, variables: {id, idType}})
45-
.then((post) => post?.data?.[postType] ?? null)
46-
.catch((error) => {
47-
return {
48-
isError: true,
49-
message: error.message
54+
.then((post) => {
55+
// Set error props if data not found.
56+
if (!post?.data?.[postType]) {
57+
response.error = true
58+
response.errorMessage = `An error occurred while trying to retrieve data for ${postType} "${id}."`
59+
60+
return null
5061
}
62+
63+
return post.data[postType]
5164
})
65+
.then(async (post) => {
66+
// Handle blocks.
67+
if (!post || !post?.blocksJSON) {
68+
return post
69+
}
5270

53-
if (!post) {
54-
return {
55-
isError: true,
56-
message: `An error occurred while trying to retrieve data for ${postType} "${id}."`
57-
}
58-
}
71+
const newPost = {...post}
72+
73+
newPost.blocks = await formatBlockData(
74+
JSON.parse(newPost.blocksJSON) ?? []
75+
)
76+
77+
return newPost
78+
})
79+
.catch((error) => {
80+
response.error = true
81+
response.errorMessage = error.message
82+
83+
return null
84+
})
5985

60-
return post
86+
return response
6187
}

api/wordpress/_global/getPostTypeStaticPaths.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export default async function getPostTypeStaticPaths(postType) {
2727
const query = gql`
2828
query GET_SLUGS {
2929
${pluralName}(first: 10000) {
30-
nodes {
31-
${pathField}
30+
edges {
31+
node {
32+
${pathField}
33+
}
3234
}
3335
}
3436
}
@@ -41,14 +43,11 @@ export default async function getPostTypeStaticPaths(postType) {
4143
const posts = await apolloClient.query({query})
4244

4345
// Process paths.
44-
const paths = !posts?.data?.[pluralName]?.nodes
46+
const paths = !posts?.data?.[pluralName]?.edges
4547
? []
46-
: posts.data[pluralName].nodes.map((post) => {
47-
// Use path field as-is for non-hierarchical post types.
48-
// Trim leading and trailing slashes then split into array on inner slashes for hierarchical post types.
49-
const slug = !isHierarchical
50-
? post[pathField]
51-
: post[pathField].replace(/^\/|\/$/g, '').split('/')
48+
: posts.data[pluralName].edges.map((post) => {
49+
// Trim leading and trailing slashes then split into array on inner slashes.
50+
const slug = post.node[pathField].replace(/^\/|\/$/g, '').split('/')
5251

5352
return {
5453
params: {
@@ -59,6 +58,6 @@ export default async function getPostTypeStaticPaths(postType) {
5958

6059
return {
6160
paths,
62-
fallback: false
61+
fallback: 'blocking'
6362
}
6463
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import getPostTypeById from './getPostTypeById'
2+
import {addApolloState} from '../connector'
3+
import getPostTypeArchive from './getPostTypeArchive'
4+
5+
/**
6+
* Retrieve static props by post type.
7+
*
8+
* @param {string} params Post params (e.g., slug).
9+
* @param {string} postType Post Type.
10+
* @param {boolean} preview Whether requesting preview of post.
11+
* @param {?Object} previewData Post preview data.
12+
* @return {Object} Object containing post props and revalidate setting.
13+
*/
14+
export default async function getPostTypeStaticProps(
15+
params,
16+
postType
17+
// preview = false, // TODO - add preview handling.
18+
// previewData = null
19+
) {
20+
// Check for dynamic archive display.
21+
if (!Object.keys(params).length) {
22+
const {apolloClient, posts, error, errorMessage} = await getPostTypeArchive(
23+
postType
24+
)
25+
26+
// Merge in query results as Apollo state.
27+
return addApolloState(apolloClient, {
28+
props: {
29+
posts,
30+
error,
31+
errorMessage,
32+
archive: true
33+
},
34+
revalidate: 60 * 5
35+
})
36+
}
37+
38+
// Handle catch-all routes.
39+
const slug = Array.isArray(params.slug) ? params.slug.join('/') : params.slug
40+
41+
// Retrieve post data.
42+
const {apolloClient, post, error, errorMessage} = await getPostTypeById(
43+
postType,
44+
slug
45+
)
46+
47+
// Merge in query results as Apollo state.
48+
return addApolloState(apolloClient, {
49+
props: {
50+
post,
51+
error,
52+
errorMessage
53+
},
54+
revalidate: 60 * 5
55+
})
56+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Query partial: retrieve author post fields.
2+
const authorPostFields = `
3+
author {
4+
node {
5+
slug
6+
nickname
7+
}
8+
}
9+
`
10+
export default authorPostFields
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Query partial: retrieve categories post fields.
2+
const categoriesPostFields = `
3+
categories {
4+
edges {
5+
node {
6+
slug
7+
name
8+
}
9+
}
10+
}
11+
`
12+
export default categoriesPostFields
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Query partial: retrieve featured image post fields.
2+
const featuredImagePostFields = `
3+
featuredImage {
4+
node {
5+
altText
6+
sourceUrl(size: $imageSize)
7+
}
8+
}
9+
`
10+
11+
export default featuredImagePostFields
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Query partial: retrieve global post fields.
2+
const globalPostFields = `
3+
databaseId
4+
date
5+
slug
6+
uri
7+
title
8+
`
9+
export default globalPostFields
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Query partial: retrieve SEO post fields.
2+
const seoPostFields = `
3+
seo {
4+
canonical
5+
title
6+
metaDesc
7+
metaRobotsNofollow
8+
metaRobotsNoindex
9+
opengraphImage {
10+
sourceUrl
11+
}
12+
}
13+
`
14+
15+
export default seoPostFields
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Query partial: retrieve tags post fields.
2+
const tagsPostFields = `
3+
tags {
4+
edges {
5+
node {
6+
name
7+
slug
8+
}
9+
}
10+
}
11+
`
12+
export default tagsPostFields

0 commit comments

Comments
 (0)