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

Commit 2a2cf49

Browse files
committed
Adding query refactoring
1 parent b3cc008 commit 2a2cf49

File tree

7 files changed

+129
-92
lines changed

7 files changed

+129
-92
lines changed

api/wordpress/_global/getMenus.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

api/wordpress/_global/getPostTypeStaticProps.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {algoliaIndexName} from '@/api/algolia/connector'
22
import getPostTypeById from './getPostTypeById'
33
import getPostTypeArchive from './getPostTypeArchive'
44
import {addApolloState} from '@/api/apolloConfig'
5-
import getMenus from './getMenus'
5+
import getMenus from '@/api/wordpress/menus/getMenus'
66
import config from '@/functions/config'
77

88
/**
@@ -21,10 +21,21 @@ export default async function getPostTypeStaticProps(
2121
// preview = false, // TODO - add preview handling.
2222
// previewData = null
2323
) {
24+
// Get WP Nav Menus.
25+
const menus = await getMenus(config.menuLocations)
26+
2427
// Check for dynamic archive display.
2528
if (!Object.keys(params).length) {
2629
const {apolloClient, ...archiveData} = await getPostTypeArchive(postType)
2730

31+
// Add WP Nav Menus to archive.
32+
archiveData.menus = menus
33+
34+
// Add Algolia env vars to archive.
35+
archiveData.algolia = {
36+
indexName: algoliaIndexName
37+
}
38+
2839
// Merge in query results as Apollo state.
2940
return addApolloState(apolloClient, {
3041
props: {
@@ -53,8 +64,7 @@ export default async function getPostTypeStaticProps(
5364
props.error = false
5465
}
5566

56-
// Get WP Nav Menus.
57-
const menus = await getMenus(config.navMenus)
67+
// Set WP Nav Menus.
5868
props.menus = menus
5969

6070
// Add Algolia env vars.

api/wordpress/menus/getMenus.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import filterByLocation from '@/api/wordpress/menus/filterByLocation'
2+
import queryMenus from './queryMenus'
3+
4+
/**
5+
* Get menu data from WPGraphQL.
6+
*
7+
* @author WebDevStudios
8+
* @param {array} locations The menu locations as an array.
9+
* @return {array} Returns array of menu objects.
10+
*/
11+
export default async function getMenus(locations = []) {
12+
if (!locations.length > 0) {
13+
return [] // Exit if empty.
14+
}
15+
16+
// Query WP Menus.
17+
const menus = await queryMenus()
18+
19+
// Filter returned menus by specific menu location.
20+
const filteredMenus = filterByLocation(menus?.data?.menus?.nodes, locations)
21+
22+
return filteredMenus || []
23+
}

api/wordpress/menus/queryMenus.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {gql} from '@apollo/client'
2+
import {initializeWpApollo} from '../connector'
3+
4+
/**
5+
* Query all WP Menus.
6+
*
7+
* @author WebDevStudios
8+
* @return {object} Menu data or error object.
9+
*/
10+
export default async function queryMenus() {
11+
const query = gql`
12+
query menuQuery {
13+
menus {
14+
nodes {
15+
locations
16+
menuItems {
17+
nodes {
18+
id
19+
parentId
20+
label
21+
path
22+
target
23+
title
24+
}
25+
}
26+
}
27+
}
28+
}
29+
`
30+
31+
// Get/create Apollo instance.
32+
const apolloClient = initializeWpApollo()
33+
34+
const menus = await apolloClient
35+
.query({query})
36+
.then((menus) => menus ?? null)
37+
.catch((error) => {
38+
return {
39+
isError: true,
40+
message: error.message
41+
}
42+
})
43+
44+
if (!menus) {
45+
return {
46+
isError: true,
47+
message: `An error occurred while trying to get menu data"`
48+
}
49+
}
50+
51+
return menus
52+
}

components/molecules/Navigation/Navigation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {useRouter} from 'next/router'
55
import styles from './Navigation.module.css'
66

77
/**
8-
* Render the Navigation Component.
8+
* Render the Navigation component.
99
*
1010
* @author WebDevStudios
1111
* @param {object} props Navigation props.

functions/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const config = {
33
siteDescription: 'The description of this website',
44
siteUrl: 'https://website.vercel.app',
55
author: '@yourname',
6-
navMenus: ['primary-menu', 'footer-menu', 'mobile-menu'],
6+
menuLocations: ['primary-menu', 'footer-menu', 'mobile-menu'],
77
social: {
88
github: {
99
label: 'Github',

public/sitemap.xml

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
3-
<url><loc>https://the-nextjs-starter.vercel.app</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
4-
<url><loc>https://the-nextjs-starter.vercel.app/search</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
5-
<url><loc>https://the-nextjs-starter.vercel.app/portfolio/starbucks-coffee-company</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
6-
<url><loc>https://the-nextjs-starter.vercel.app/portfolio/national-domestic-violence-hotline</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
7-
<url><loc>https://the-nextjs-starter.vercel.app/portfolio/campbell-soup-company</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
8-
<url><loc>https://the-nextjs-starter.vercel.app/team/jim-byrom</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
9-
<url><loc>https://the-nextjs-starter.vercel.app/team/greg-rickaby</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
10-
<url><loc>https://the-nextjs-starter.vercel.app/team/cristina-holt</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
11-
<url><loc>https://the-nextjs-starter.vercel.app/team/jodie-riccelli</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
12-
<url><loc>https://the-nextjs-starter.vercel.app/team/brad-williams-2</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
13-
<url><loc>https://the-nextjs-starter.vercel.app/team/april-williams</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
14-
<url><loc>https://the-nextjs-starter.vercel.app/team/lisa-sabin-wilson</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
15-
<url><loc>https://the-nextjs-starter.vercel.app/careers/account-manager</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
16-
<url><loc>https://the-nextjs-starter.vercel.app/events/giving-back-with-another-charity-challenge</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
17-
<url><loc>https://the-nextjs-starter.vercel.app/service/custom-development</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
18-
<url><loc>https://the-nextjs-starter.vercel.app/service/custom-design</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
19-
<url><loc>https://the-nextjs-starter.vercel.app/service/consulting-discovery</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
20-
<url><loc>https://the-nextjs-starter.vercel.app/blog/form-checkbox-test</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
21-
<url><loc>https://the-nextjs-starter.vercel.app/blog/top-blog-posts-from-2020</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
22-
<url><loc>https://the-nextjs-starter.vercel.app/blog/form-input-test</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
23-
<url><loc>https://the-nextjs-starter.vercel.app/blog/wordpress-blocks-backwards-compatibility</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
24-
<url><loc>https://the-nextjs-starter.vercel.app/blog/form-test</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
25-
<url><loc>https://the-nextjs-starter.vercel.app/blog/tis-the-season-for-five-for-the-future</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
26-
<url><loc>https://the-nextjs-starter.vercel.app/blog/company-news-december-2020</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
27-
<url><loc>https://the-nextjs-starter.vercel.app/blog/next-js-wordpress</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
28-
<url><loc>https://the-nextjs-starter.vercel.app/blog/post-with-acf-blocks</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
29-
<url><loc>https://the-nextjs-starter.vercel.app/blog/hello-world</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
30-
<url><loc>https://the-nextjs-starter.vercel.app/testimonial/mark-junkunc</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
31-
<url><loc>https://the-nextjs-starter.vercel.app/testimonial/kevin-mcclelland</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
32-
<url><loc>https://the-nextjs-starter.vercel.app/testimonial/ben-koo</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
33-
<url><loc>https://the-nextjs-starter.vercel.app/testimonials</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
34-
<url><loc>https://the-nextjs-starter.vercel.app/services</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
35-
<url><loc>https://the-nextjs-starter.vercel.app/portfolio</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
36-
<url><loc>https://the-nextjs-starter.vercel.app/team-members</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
37-
<url><loc>https://the-nextjs-starter.vercel.app/about</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
38-
<url><loc>https://the-nextjs-starter.vercel.app/contact</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
39-
<url><loc>https://the-nextjs-starter.vercel.app/post-archive</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
40-
<url><loc>https://the-nextjs-starter.vercel.app/form-example</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
41-
<url><loc>https://the-nextjs-starter.vercel.app/homepage/this-is-a-nested-page</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T15:41:03.805Z</lastmod></url>
3+
<url><loc>https://the-nextjs-starter.vercel.app</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
4+
<url><loc>https://the-nextjs-starter.vercel.app/search</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
5+
<url><loc>https://the-nextjs-starter.vercel.app/events/giving-back-with-another-charity-challenge</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
6+
<url><loc>https://the-nextjs-starter.vercel.app/blog/form-checkbox-test</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
7+
<url><loc>https://the-nextjs-starter.vercel.app/blog/top-blog-posts-from-2020</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
8+
<url><loc>https://the-nextjs-starter.vercel.app/blog/form-input-test</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
9+
<url><loc>https://the-nextjs-starter.vercel.app/blog/wordpress-blocks-backwards-compatibility</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
10+
<url><loc>https://the-nextjs-starter.vercel.app/blog/form-test</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
11+
<url><loc>https://the-nextjs-starter.vercel.app/blog/tis-the-season-for-five-for-the-future</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
12+
<url><loc>https://the-nextjs-starter.vercel.app/blog/company-news-december-2020</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
13+
<url><loc>https://the-nextjs-starter.vercel.app/blog/next-js-wordpress</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
14+
<url><loc>https://the-nextjs-starter.vercel.app/blog/post-with-acf-blocks</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
15+
<url><loc>https://the-nextjs-starter.vercel.app/blog/hello-world</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
16+
<url><loc>https://the-nextjs-starter.vercel.app/service/custom-development</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
17+
<url><loc>https://the-nextjs-starter.vercel.app/service/custom-design</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
18+
<url><loc>https://the-nextjs-starter.vercel.app/service/consulting-discovery</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
19+
<url><loc>https://the-nextjs-starter.vercel.app/team/jim-byrom</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
20+
<url><loc>https://the-nextjs-starter.vercel.app/team/greg-rickaby</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
21+
<url><loc>https://the-nextjs-starter.vercel.app/team/cristina-holt</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
22+
<url><loc>https://the-nextjs-starter.vercel.app/team/jodie-riccelli</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
23+
<url><loc>https://the-nextjs-starter.vercel.app/team/brad-williams-2</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
24+
<url><loc>https://the-nextjs-starter.vercel.app/team/april-williams</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
25+
<url><loc>https://the-nextjs-starter.vercel.app/team/lisa-sabin-wilson</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
26+
<url><loc>https://the-nextjs-starter.vercel.app/careers/account-manager</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
27+
<url><loc>https://the-nextjs-starter.vercel.app/portfolio/starbucks-coffee-company</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
28+
<url><loc>https://the-nextjs-starter.vercel.app/portfolio/national-domestic-violence-hotline</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
29+
<url><loc>https://the-nextjs-starter.vercel.app/portfolio/campbell-soup-company</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
30+
<url><loc>https://the-nextjs-starter.vercel.app/testimonial/mark-junkunc</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
31+
<url><loc>https://the-nextjs-starter.vercel.app/testimonial/kevin-mcclelland</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
32+
<url><loc>https://the-nextjs-starter.vercel.app/testimonial/ben-koo</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
33+
<url><loc>https://the-nextjs-starter.vercel.app/testimonials</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
34+
<url><loc>https://the-nextjs-starter.vercel.app/services</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
35+
<url><loc>https://the-nextjs-starter.vercel.app/portfolio</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
36+
<url><loc>https://the-nextjs-starter.vercel.app/team-members</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
37+
<url><loc>https://the-nextjs-starter.vercel.app/about</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
38+
<url><loc>https://the-nextjs-starter.vercel.app/contact</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
39+
<url><loc>https://the-nextjs-starter.vercel.app/post-archive</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
40+
<url><loc>https://the-nextjs-starter.vercel.app/form-example</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
41+
<url><loc>https://the-nextjs-starter.vercel.app/homepage/this-is-a-nested-page</loc><changefreq>weekly</changefreq><priority>0.7</priority><lastmod>2021-01-15T17:27:27.730Z</lastmod></url>
4242
</urlset>

0 commit comments

Comments
 (0)