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

Commit 8403cbe

Browse files
committed
Merge branch 'staging' into develop
2 parents 0059664 + 9a2b957 commit 8403cbe

File tree

12 files changed

+104
-76
lines changed

12 files changed

+104
-76
lines changed

api/apolloConfig.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {useMemo} from 'react'
2+
import merge from 'deepmerge'
3+
import isEqual from 'lodash/isEqual'
4+
import {initializeWpApollo} from './wordpress/connector'
5+
6+
// Set global state name.
7+
export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__'
8+
9+
/**
10+
* Init Apollo and merge with initial state.
11+
*
12+
* @author WebDevStudios
13+
* @param {Object} apolloClient Apollo client instance.
14+
* @param {mixed} initialState The initial state of things.
15+
* @return {Object} Apollo client instance.
16+
*/
17+
export function initializeApollo(apolloClient, initialState = null) {
18+
// If a page has Next.js data fetching methods that
19+
// use Apollo Client, the initial state gets hydrated here.
20+
if (initialState) {
21+
// Get existing cache, loaded during client side data fetching.
22+
const existingCache = apolloClient.extract()
23+
24+
// Merge the existing cache into data passed from getStaticProps()/getServerSideProps().
25+
const data = merge(initialState, existingCache, {
26+
// Combine arrays using object equality (like in sets).
27+
arrayMerge: (destinationArray, sourceArray) => [
28+
...sourceArray,
29+
...destinationArray.filter((d) =>
30+
sourceArray.every((s) => !isEqual(d, s))
31+
)
32+
]
33+
})
34+
35+
// Restore the cache with the merged data.
36+
apolloClient.cache.restore(data)
37+
}
38+
39+
return apolloClient
40+
}
41+
42+
/**
43+
* Pass down Apollo state to page props.
44+
*
45+
* @author WebDevStudios
46+
* @param {object} client Apollo Client props.
47+
* @param {object} pageProps Props from getStaticProps().
48+
*/
49+
export function addApolloState(client, pageProps) {
50+
if (pageProps?.props) {
51+
pageProps.props[APOLLO_STATE_PROP_NAME] = client.cache.extract()
52+
}
53+
54+
return pageProps
55+
}
56+
57+
/**
58+
* Only update when the cache value has changed.
59+
*
60+
* @author WebDevStudios
61+
* @param {object} pageProps Props from getStaticProps().
62+
*/
63+
export function useApollo(pageProps) {
64+
const state = pageProps[APOLLO_STATE_PROP_NAME]
65+
// Use WP Apollo instance by default.
66+
const store = useMemo(() => initializeWpApollo(state), [state])
67+
return store
68+
}

api/wordpress/_global/getPostTypeArchive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {initializeApollo} from '../connector'
1+
import {initializeWpApollo} from '../connector'
22
import queryPostsArchive from '../posts/queryPostsArchive'
33
import {postTypes} from './postTypes'
44

@@ -31,7 +31,7 @@ export default async function getPostTypeArchive(
3131
const query = postTypeQuery?.[postType] ?? null
3232

3333
// Get/create Apollo instance.
34-
const apolloClient = initializeApollo()
34+
const apolloClient = initializeWpApollo()
3535

3636
// Set up return object.
3737
const response = {

api/wordpress/_global/getPostTypeById.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import queryPostById from '../posts/queryPostById'
2-
import {initializeApollo} from '../connector'
2+
import {initializeWpApollo} from '../connector'
33
import queryPageById from '../pages/queryPageById'
44
import {isHierarchicalPostType} from './postTypes'
55
import formatBlockData from '@/functions/formatBlockData'
@@ -30,7 +30,7 @@ export default async function getPostTypeById(postType, id, idType = 'SLUG') {
3030
const query = postTypeQuery?.[postType] ?? null
3131

3232
// Get/create Apollo instance.
33-
const apolloClient = initializeApollo()
33+
const apolloClient = initializeWpApollo()
3434

3535
// Set up return object.
3636
const response = {

api/wordpress/_global/getPostTypeStaticPaths.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {gql} from '@apollo/client'
22
import {isValidPostType, postTypes, isHierarchicalPostType} from './postTypes'
3-
import {initializeApollo} from '../connector'
3+
import {initializeWpApollo} from '../connector'
44

55
/**
66
* Retrieve static paths by post type.
@@ -37,7 +37,7 @@ export default async function getPostTypeStaticPaths(postType) {
3737
`
3838

3939
// Get/create Apollo instance.
40-
const apolloClient = initializeApollo()
40+
const apolloClient = initializeWpApollo()
4141

4242
// Execute query.
4343
const posts = await apolloClient.query({query})

api/wordpress/_global/getPostTypeStaticProps.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import getPostTypeById from './getPostTypeById'
2-
import {addApolloState} from '../connector'
32
import getPostTypeArchive from './getPostTypeArchive'
3+
import {addApolloState} from '@/api/apolloConfig'
44

55
/**
66
* Retrieve static props by post type.
77
*
8+
* @author WebDevStudios
89
* @param {string} params Post params (e.g., slug).
910
* @param {string} postType Post Type.
1011
* @param {boolean} preview Whether requesting preview of post.

api/wordpress/connector.js

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import {useMemo} from 'react'
21
import {ApolloClient, HttpLink, InMemoryCache} from '@apollo/client'
3-
import merge from 'deepmerge'
4-
import isEqual from 'lodash/isEqual'
52
import getEnvVar from '@/functions/getEnvVar'
3+
import {initializeApollo} from '../apolloConfig'
64

75
// Define env vars.
86
export const wpApiUrlBase = getEnvVar('WORDPRESS_API_URL')
97

10-
// Set global state name.
11-
export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__'
12-
13-
let apolloClient
8+
let wpApolloClient
149

1510
/**
16-
* Create an basic Apollo client.
11+
* Create a basic Apollo client for connecting to WP.
1712
*
1813
* @see https://www.apollographql.com/docs/react/api/core/ApolloClient/
14+
*
15+
* @author WebDevStudios
16+
* @return {Object} Apollo client instance.
1917
*/
20-
export function createApolloClient() {
18+
export function createWpApolloClient() {
2119
return new ApolloClient({
2220
ssrMode: false,
2321
link: new HttpLink({
@@ -29,65 +27,23 @@ export function createApolloClient() {
2927
}
3028

3129
/**
32-
* Init Apollo and merge with initial state.
30+
* Init Apollo for WP and merge with initial state.
3331
*
34-
* @param {mixed} initialState The initial state of things.
32+
* @author WebDevStudios
33+
* @param {mixed} initialState Initial Apollo state.
34+
* @return {object} WP Apollo client instance.
3535
*/
36-
export function initializeApollo(initialState = null) {
36+
export function initializeWpApollo(initialState = null) {
3737
// Only run one instance of the Apollo client.
38-
const _apolloClient = apolloClient ?? createApolloClient()
39-
40-
// If a page has Next.js data fetching methods that
41-
// use Apollo Client, the initial state gets hydrated here.
42-
if (initialState) {
43-
// Get existing cache, loaded during client side data fetching.
44-
const existingCache = _apolloClient.extract()
45-
46-
// Merge the existing cache into data passed from getStaticProps()/getServerSideProps().
47-
const data = merge(initialState, existingCache, {
48-
// Combine arrays using object equality (like in sets).
49-
arrayMerge: (destinationArray, sourceArray) => [
50-
...sourceArray,
51-
...destinationArray.filter((d) =>
52-
sourceArray.every((s) => !isEqual(d, s))
53-
)
54-
]
55-
})
38+
const _apolloClient = wpApolloClient ?? createWpApolloClient()
5639

57-
// Restore the cache with the merged data.
58-
_apolloClient.cache.restore(data)
59-
}
40+
const newApolloClient = initializeApollo(_apolloClient, initialState)
6041

6142
// For SSG and SSR always create a new Apollo Client.
62-
if (typeof window === 'undefined') return _apolloClient
43+
if (typeof window === 'undefined') return newApolloClient
6344

6445
// Create the Apollo Client once in the client.
65-
if (!apolloClient) apolloClient = _apolloClient
46+
if (!wpApolloClient) wpApolloClient = newApolloClient
6647

67-
return _apolloClient
68-
}
69-
70-
/**
71-
* Pass down Apollo state to page props.
72-
*
73-
* @param {object} client Apollo Client props.
74-
* @param {object} pageProps Props from getStaticProps().
75-
*/
76-
export function addApolloState(client, pageProps) {
77-
if (pageProps?.props) {
78-
pageProps.props[APOLLO_STATE_PROP_NAME] = client.cache.extract()
79-
}
80-
81-
return pageProps
82-
}
83-
84-
/**
85-
* Only update when the cache value has changed.
86-
*
87-
* @param {object} pageProps Props from getStaticProps().
88-
*/
89-
export function useApollo(pageProps) {
90-
const state = pageProps[APOLLO_STATE_PROP_NAME]
91-
const store = useMemo(() => initializeApollo(state), [state])
92-
return store
48+
return newApolloClient
9349
}

api/wordpress/gravityForms/getFormById.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import {initializeApollo} from '../connector'
1+
import {initializeWpApollo} from '../connector'
22
import queryFormById from './queryFormById'
33

44
/**
55
* Retrieve single form by ID.
66
*
7+
* @author WebDevStudios
78
* @param {string} id Form ID.
89
* @return {Object} Post data or error object.
910
*/
@@ -12,7 +13,7 @@ export default async function getFormById(id) {
1213
const formId = Buffer.from(`GravityFormsForm:${id}`).toString('base64')
1314

1415
// Get/create Apollo instance.
15-
const apolloClient = initializeApollo()
16+
const apolloClient = initializeWpApollo()
1617

1718
// Execute query.
1819
const form = await apolloClient

api/wordpress/gravityForms/queryFormById.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {gql} from '@apollo/client'
44
/**
55
* Partial: retrieve basic data on all form fields.
66
*
7+
* @author WebDevStudios
78
* @return {string} Form fields query partial.
89
*/
910
function getFormFieldsPartial() {

functions/formatBlockData.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import getFormById from '@/api/wordpress/gravityForms/getFormById'
33
/**
44
* Format and retrieve expanded block data.
55
*
6+
* @author WebDevStudios
67
* @param {Array} blocks Basic block data.
78
* @return {Array} Formatted block data.
89
*/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"eslint-config-prettier": "^7.1.0",
5656
"eslint-plugin-import": "^2.22.1",
5757
"eslint-plugin-jsx-a11y": "^6.4.1",
58-
"eslint-plugin-prettier": "^3.3.0",
58+
"eslint-plugin-prettier": "^3.3.1",
5959
"eslint-plugin-react": "^7.22.0",
6060
"eslint-plugin-react-hooks": "^4.2.0",
6161
"graphql": "^15.4.0",

0 commit comments

Comments
 (0)