Skip to content

Commit

Permalink
CI cleanup npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
benabel committed Jun 10, 2020
1 parent 2656439 commit 68d3fa2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-gpr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10

name: Create Release

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/setup-node@v1
with:
registry-url: "https://registry.npmjs.org"
- run: npm
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ src
.babelrc
.editorconfig
.gitattributes
.prettierrc
.prettierignore
.eslintrc
.eslintignore
index.js
package-scripts.js
example/
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ module.exports = {
},
},
// Optional filter to limit indexed nodes
filter: (node, getNode) =>
node.frontmatter.tags !== 'exempt',
filter: (node, getNode) => node.frontmatter.tags !== "exempt",
},
},
],
Expand Down Expand Up @@ -174,6 +173,7 @@ search = evt => {
## Optimize handling of data models with nested nodes

There are times when you have a data model that has nested nodes. Example resolver configuration in `gatsby-config.js`:

```
resolvers : {
// For any node of BlogPost, list how to resolve the fields' values
Expand All @@ -188,13 +188,15 @@ resolvers : {
}
}
```

The problem with the above resolvers configuration is that it will include all Asset models in the `elasticlunr` index,
potentially bloating the `elasticlunr` index and leading to large bundle sizes and slower page load times.

The solution is to make use of the second paramater passed to each field resolver function called `getNode`. `getNode` is the same function provided by gatsby
to the [setFieldsOnGraphQLNodeType](https://www.gatsbyjs.org/docs/node-apis/#setFieldsOnGraphQLNodeType) node api method and when called
with a data model node id it will return a node with all it's data. The above example of the `BlogPost` model with the nested `featuredImage` property of
type `Asset` then becomes:

```
resolvers : {
// For any node of BlogPost, list how to resolve the fields' values
Expand All @@ -204,20 +206,18 @@ resolvers : {
}
}
```
Now you can use the `featuredImage` data of `BlogPost` model without including all `Asset` models in the `elasticlunr` index [(see PR #3 for more details)](https://github.com/gatsby-contrib/gatsby-plugin-elasticlunr-search/pull/3).

Now you can use the `featuredImage` data of `BlogPost` model without including all `Asset` models in the `elasticlunr` index [(see PR #3 for more details)](https://github.com/gatsby-contrib/gatsby-plugin-elasticlunr-search/pull/3).

You can now also resolve the gatsby store with ``getNodesByType`` and ``getNodes``
You can now also resolve the gatsby store with `getNodesByType` and `getNodes`
so the full signature of node resolving is this:

```
(node, getNode, getNodesByType, getNodes)
```

Documentation of all node helpers:

- [getNode](https://www.gatsbyjs.org/docs/node-api-helpers/#getNode)
- [getNodesByType](https://www.gatsbyjs.org/docs/node-api-helpers/#getNodesByType)
- [getNodes](https://www.gatsbyjs.org/docs/node-api-helpers/#getNodes)




6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@gatsby-contrib/gatsby-plugin-elasticlunr-search",
"version": "2.4.1",
"version": "2.4.2",
"description": "Search for gatsby; implemented via elasticlunr.",
"main": "n/a",
"main": "gatsby-node.js",
"scripts": {
"build": "babel src --out-dir .",
"build:watch": "babel src --watch --out-dir .",
Expand Down Expand Up @@ -55,4 +55,4 @@
"engines": {
"node": ">=10.13.0"
}
}
}
23 changes: 19 additions & 4 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const createOrGetIndex = async (
node,
cache,
getNode,
getNodesByType,
getNodesByType,
getNodes,
server,
{ fields, resolvers }
Expand All @@ -67,7 +67,12 @@ const createOrGetIndex = async (
...Object.keys(fieldResolvers).reduce((prev, key) => {
return {
...prev,
[key]: fieldResolvers[key](pageNode, getNode, getNodesByType, getNodes),
[key]: fieldResolvers[key](
pageNode,
getNode,
getNodesByType,
getNodes
),
}
}, {}),
}
Expand Down Expand Up @@ -109,7 +114,9 @@ exports.onCreateNode = ({ node, actions, getNode }, { resolvers, filter }) => {
return
}

if (filter && !filter(node, getNode)) { return }
if (filter && !filter(node, getNode)) {
return
}

const { createNode } = actions
const searchIndex = getNode(SEARCH_INDEX_ID) || createEmptySearchIndexNode()
Expand All @@ -129,7 +136,15 @@ exports.setFieldsOnGraphQLNodeType = (
index: {
type: SearchIndex,
resolve: (node, _opts, _3, server) =>
createOrGetIndex(node, cache, getNode, getNodesByType, getNodes, server, pluginOptions),
createOrGetIndex(
node,
cache,
getNode,
getNodesByType,
getNodes,
server,
pluginOptions
),
},
}
}

0 comments on commit 68d3fa2

Please sign in to comment.