Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize GraphQl Query #1225

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 30 additions & 41 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const siteConfig = require('./site-config');
const { createFilePath } = require('gatsby-source-filesystem');
const appInsights = require('applicationinsights');
const makePluginData = require('./src/helpers/plugin-data');
Expand Down Expand Up @@ -98,14 +97,10 @@ exports.createPages = async ({ graphql, actions }) => {
}
frontmatter {
index
redirects
experts
consulting
}
parent {
... on File {
name
relativeDirectory
}
}
}
Expand All @@ -114,45 +109,34 @@ exports.createPages = async ({ graphql, actions }) => {
filter: { frontmatter: { type: { in: ["rule"] } } }
) {
nodes {
excerpt(format: HTML, pruneLength: 500)
fields {
slug
}
frontmatter {
uri
title
archivedreason
related
redirects
archivedreason
guid
}
htmlAst
}
}
}
`);

const categoryTemplate = require.resolve('./src/templates/category.js');
const ruleTemplate = require.resolve('./src/templates/rule.js');

result.data.categories.nodes.forEach((node) => {
// Find any categories that can't resolve a rule
node.frontmatter.index.forEach((inCat) => {
var match = false;

result.data.rules.nodes.forEach((rulenode) => {
if (rulenode.frontmatter.uri == inCat) {
match = true;
}
if (rulenode.frontmatter.redirects) {
rulenode.frontmatter.redirects.forEach((redirect) => {
if (redirect == inCat) {
match = true;
}
});
}
});

if (match == false) {
// eslint-disable-next-line no-console
console.log(node.parent.name + ' cannot find rule ' + inCat);
let indexedRule = [];
result.data.rules.nodes.forEach((rule) => {
if (
node.frontmatter.index.includes(rule.frontmatter.uri) &&
!rule.frontmatter.archivedreason
) {
indexedRule.push(rule);
}
});

Expand All @@ -162,15 +146,15 @@ exports.createPages = async ({ graphql, actions }) => {
component: categoryTemplate,
context: {
slug: node.fields.slug,
index: node.frontmatter.index,
redirects: node.frontmatter.redirects,
pageRules: indexedRule,
},
});
});

const orphanedRules = [];

result.data.rules.nodes.forEach((node) => {
// Find any rules missing a category
var match = false;
let match = false;
if (!node.frontmatter.archivedreason) {
result.data.categories.nodes.forEach((catNode) => {
catNode.frontmatter.index.forEach((inCat) => {
Expand All @@ -182,13 +166,9 @@ exports.createPages = async ({ graphql, actions }) => {
} else {
match = true;
}

if (match == false) {
// eslint-disable-next-line no-console
console.log(
'https://www.ssw.com.au/rules/' +
node.frontmatter.uri +
' is missing a category'
);
orphanedRules.push(node.frontmatter.uri);
}

// Create the page for the rule
Expand All @@ -206,11 +186,20 @@ exports.createPages = async ({ graphql, actions }) => {
});
});

const profilePage = require.resolve('./src/pages/profile.js');
// const profilePage = require.resolve('./src/pages/profile.js');
// createPage({
// path: `${siteConfig.pathPrefix}/people/`,
// matchPath: `${siteConfig.pathPrefix}/people/:gitHubUsername`,
// component: profilePage,
// });

const orphanedPage = require.resolve('./src/templates/orphaned.js');
createPage({
path: `${siteConfig.pathPrefix}/people/`,
matchPath: `${siteConfig.pathPrefix}/people/:gitHubUsername`,
component: profilePage,
path: '/orphaned/',
component: orphanedPage,
context: {
index: orphanedRules,
},
});
};

Expand Down
9 changes: 0 additions & 9 deletions src/pages/archived.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,11 @@ function ArchivedWithQuery(props) {
}
) {
nodes {
html
frontmatter {
type
title
index
}
parent {
... on File {
name
}
}
}
}
topCategories: allMarkdownRemark(
Expand All @@ -190,7 +184,6 @@ function ArchivedWithQuery(props) {
}
) {
nodes {
html
frontmatter {
type
title
Expand All @@ -211,7 +204,6 @@ function ArchivedWithQuery(props) {
}
) {
nodes {
html
frontmatter {
type
title
Expand All @@ -221,7 +213,6 @@ function ArchivedWithQuery(props) {
parent {
... on File {
name
relativeDirectory
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import { config } from '@fortawesome/fontawesome-svg-core';
config.autoAddCss = false;

const Index = ({ data, location }) => {
const notArchivedRules = data.rules.nodes.filter(
(r) => !r.frontmatter.archivedreason
);

return (
<div className="w-full">
<Breadcrumb isHomePage={true} />
Expand Down Expand Up @@ -54,7 +50,7 @@ const Index = ({ data, location }) => {
<TopCategory
topcategory={cat}
categories={data.categories}
rules={notArchivedRules}
rules={data.rules.nodes}
></TopCategory>
</section>
);
Expand Down Expand Up @@ -122,11 +118,6 @@ function IndexWithQuery(props) {
title
index
}
parent {
... on File {
name
}
}
}
}
topCategories: allMarkdownRemark(
Expand Down Expand Up @@ -162,7 +153,6 @@ function IndexWithQuery(props) {
type
title
index
archivedreason
}
parent {
... on File {
Expand All @@ -173,7 +163,9 @@ function IndexWithQuery(props) {
}
}
rules: allMarkdownRemark(
filter: { frontmatter: { type: { eq: "rule" } } }
filter: {
frontmatter: { archivedreason: { eq: null }, type: { eq: "rule" } }
}
) {
nodes {
frontmatter {
Expand Down
3 changes: 0 additions & 3 deletions src/pages/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,6 @@ function ProfileWithQuery(props) {
title
uri
guid
authors {
title
}
}
htmlAst
}
Expand Down
28 changes: 4 additions & 24 deletions src/templates/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const appInsights = new ApplicationInsights({

appInsights.loadAppInsights();

export default function Category({ data }) {
export default function Category({ data, pageContext }) {
const linkRef = useRef();
const category = data.markdownRemark;

Expand All @@ -48,13 +48,7 @@ export default function Category({ data }) {
greyBox: GreyBox,
};

var rules = data.rule.nodes
.filter((r) => {
return !r.frontmatter.archivedreason;
})
.filter((r) => {
return category.frontmatter.index.includes(r.frontmatter.uri);
});
const rules = pageContext.pageRules;

return (
<div>
Expand Down Expand Up @@ -156,7 +150,7 @@ export default function Category({ data }) {
<div className="category-rule p-0 sm:p-[2.2rem]">
<ol className="rule-number list-none sm:list-decimal">
{category.frontmatter.index.map((r, i) => {
var rule = rules.find((rr) => rr.frontmatter.uri == r);
let rule = rules.find((rr) => rr.frontmatter.uri == r);
if (!rule) {
return;
}
Expand Down Expand Up @@ -311,7 +305,7 @@ Category.propTypes = {
};

export const query = graphql`
query ($slug: String!, $index: [String]!) {
query ($slug: String!) {
markdownRemark(fields: { slug: { eq: $slug } }) {
htmlAst
frontmatter {
Expand All @@ -330,19 +324,5 @@ export const query = graphql`
}
}
}
rule: allMarkdownRemark(filter: { frontmatter: { uri: { in: $index } } }) {
nodes {
excerpt(format: HTML, pruneLength: 500)
frontmatter {
uri
archivedreason
title
guid
consulting
experts
}
htmlAst
}
}
}
`;
Loading
Loading