Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(gatsby): Page dependency resolver (#9732)
Anywhere in Gatsby where a graphql resolver returns a node, we need to declare a dependency from the node to the page `context.path`. This code is littered throughout `schema`. Eventually, it would be great to figure out an alternative way to declare these dependencies, since causing side effects in query resolvers isn't ideal, but in the mean time, this PR cleans up the code a bit by introducing a resolver middleware called `pageDependencyResolver`. It executes a given resolver and then creates a page dependency. Another alternative would be to use [graphql-middleware](https://github.com/prisma/graphql-middleware) to run this on all field resolvers. Perhaps another day.
- Loading branch information
Showing
10 changed files
with
99 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 4 additions & 10 deletions
14
packages/gatsby/src/schema/__tests__/build-node-connections-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/gatsby/src/schema/__tests__/page-dependency-resolver.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const pageDependencyResolver = require(`../page-dependency-resolver`) | ||
|
||
describe(`page-dependency-resolver`, () => { | ||
it(`should handle nulls in results`, async () => { | ||
const innerResolver = () => [null] | ||
const resolver = pageDependencyResolver(innerResolver) | ||
const result = await resolver({}, {}) | ||
expect(result).toEqual([null]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const _ = require(`lodash`) | ||
const createPageDependency = require(`../redux/actions/add-page-dependency`) | ||
|
||
/** | ||
* A Graphql resolver middleware that runs `resolver` and creates a | ||
* page dependency with the returned node. | ||
* | ||
* @param resolver A graphql resolver. A function that take arguments | ||
* (node, args, context, info) and return a node | ||
* @returns A new graphql resolver | ||
*/ | ||
function pageDependencyResolver(resolver) { | ||
return async (node, args, context = {}, info = {}) => { | ||
const { path } = context | ||
const result = await resolver(node, args, context, info) | ||
|
||
// Call createPageDependency on each result | ||
if (path) { | ||
const asArray = _.isArray(result) ? result : [result] | ||
for (const node of asArray) { | ||
if (node) { | ||
// using module.exports here so it can be mocked | ||
createPageDependency({ | ||
path, | ||
nodeId: node.id, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
// Finally return the found node | ||
return result | ||
} | ||
} | ||
|
||
module.exports = pageDependencyResolver |
Oops, something went wrong.