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

Commit a428135

Browse files
authored
Merge pull request #43 from WebDevStudios/feature/33-basic-post-page-queries-refactor
Feature/33 basic post page queries refactor
2 parents f2049a6 + 69f5951 commit a428135

15 files changed

+162
-182
lines changed

api/wordpress/_global/getPostTypeById.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {isHierarchicalPostType} from './postTypes'
1010
* @param {string} postType WP post type.
1111
* @param {Number|string} id Post identifier.
1212
* @param {string} idType Type of ID.
13-
* @return {Object} Post data or error object.
13+
* @return {Object} Object containing Apollo client instance and post data or error object.
1414
*/
1515
export default async function getPostTypeById(postType, id, idType = 'SLUG') {
1616
// Define single post query based on post type.
@@ -57,5 +57,8 @@ export default async function getPostTypeById(postType, id, idType = 'SLUG') {
5757
}
5858
}
5959

60-
return post
60+
return {
61+
apolloClient,
62+
post
63+
}
6164
}

api/wordpress/_global/getPostTypeStaticPaths.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ export default async function getPostTypeStaticPaths(postType) {
4444
const paths = !posts?.data?.[pluralName]?.nodes
4545
? []
4646
: 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('/')
47+
// Trim leading and trailing slashes then split into array on inner slashes.
48+
const slug = post[pathField].replace(/^\/|\/$/g, '').split('/')
5249

5350
return {
5451
params: {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import getPostTypeById from './getPostTypeById'
2+
import {addApolloState} from '../connector'
3+
4+
/**
5+
* Retrieve static props by post type.
6+
*
7+
* @param {string} params Post params (e.g., slug).
8+
* @param {string} postType Post Type.
9+
* @param {boolean} preview Whether requesting preview of post.
10+
* @param {?Object} previewData Post preview data.
11+
* @return {Object} Object containing post props and revalidate setting.
12+
*/
13+
export default async function getPostTypeStaticProps(
14+
params,
15+
postType
16+
// preview = false, // TODO - add preview handling.
17+
// previewData = null
18+
) {
19+
// Handle catch-all routes.
20+
const slug = Array.isArray(params.slug) ? params.slug.join('/') : params.slug
21+
22+
// Retrieve post data.
23+
const {apolloClient, post} = await getPostTypeById(postType, slug)
24+
25+
// Merge in query results as Apollo state.
26+
return addApolloState(apolloClient, {
27+
props: {
28+
post
29+
},
30+
revalidate: 60 * 5
31+
})
32+
}
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Query partial: retrieve global post fields.
2+
const globalPostFields = `
3+
databaseId
4+
date
5+
slug
6+
title
7+
`
8+
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

api/wordpress/pages/queryPageById.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
1+
import globalPostFields from '../_partials/globalPostFields'
2+
import seoPostFields from '../_partials/seoPostFields'
3+
import authorPostFields from '../_partials/authorPostFields'
4+
import featuredImagePostFields from '../_partials/featuredImagePostFields'
5+
16
const {gql} = require('@apollo/client')
27

8+
// Fragment: retrieve single page fields.
9+
const singlePageFragment = gql`
10+
fragment SinglePageFields on Page {
11+
${globalPostFields}
12+
blocksJSON
13+
excerpt
14+
${seoPostFields}
15+
${authorPostFields}
16+
${featuredImagePostFields}
17+
}
18+
`
19+
320
// Query: retrieve page by specified identifier.
421
const queryPageById = gql`
5-
query GET_PAGE_BY_SLUG($id: ID!, $idType: PageIdType = URI) {
22+
query GET_PAGE_BY_SLUG(
23+
$id: ID!
24+
$idType: PageIdType = URI
25+
$imageSize: MediaItemSizeEnum = LARGE
26+
) {
627
page(id: $id, idType: $idType) {
7-
blocksJSON
8-
databaseId
9-
date
10-
slug
11-
title
12-
excerpt
13-
seo {
14-
canonical
15-
title
16-
metaDesc
17-
metaRobotsNofollow
18-
metaRobotsNoindex
19-
opengraphImage {
20-
sourceUrl
21-
}
22-
}
23-
author {
24-
node {
25-
slug
26-
nickname
27-
}
28-
}
29-
featuredImage {
30-
node {
31-
altText
32-
sourceUrl(size: LARGE)
33-
}
34-
}
28+
...SinglePageFields
3529
}
3630
}
31+
${singlePageFragment}
3732
`
3833

3934
export default queryPageById

0 commit comments

Comments
 (0)