Skip to content

Commit

Permalink
Fix tracking nodes with filtering/running sift (#4024)
Browse files Browse the repository at this point in the history
* Track Node that contains inline objects/arrays instead of its parent. This doesn't break finding topmost ancestor node as we already trying to get walk along parent hierarchy.

Rename tracking functions and jsdocs to provide some basic context for these functions

* Fix tracking nodes with filtering/running sift. Before we filter nodes we create copies of nodes with appended fields added by plugins (by setFieldsOnGraphQLNodeType) if they are filtered on. These copies weren't tracked.
  • Loading branch information
pieh authored and KyleAMathews committed Feb 14, 2018
1 parent e6c5212 commit c8d3c63
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { joinPath } = require(`../utils/path`)
const {
getNode,
hasNodeChanged,
trackSubObjectsToRootNodeId,
trackInlineObjectsInRootNode,
} = require(`./index`)
const { store } = require(`./index`)
import * as joiSchemas from "../joi-schemas/joi"
Expand Down Expand Up @@ -484,7 +484,7 @@ actions.createNode = (node: any, plugin?: Plugin, traceId?: string) => {
)
}

trackSubObjectsToRootNodeId(node)
trackInlineObjectsInRootNode(node)

const oldNode = getNode(node.id)

Expand Down
36 changes: 28 additions & 8 deletions packages/gatsby/src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,45 @@ const emitter = mitt()
// Reducers
const reducers = require(`./reducers`)

// Parent node tracking
// Root node tracking

/**
* Map containing links between inline objects or arrays
* and Node that contains them
* @type {Object.<(Object|Array),string>}
*/
const rootNodeMap = new WeakMap()
const addParentToSubObjects = (data, parentId) => {

/**
* Add link between passed data and Node. This function shouldn't be used
* directly. Use higher level `trackInlineObjectsInRootNode`
* @see trackInlineObjectsInRootNode
* @param {(Object|Array)} data Inline object or array
* @param {string} nodeId Id of node that contains data passed in first parameter
*/
const addRootNodeToInlineObject = (data, nodeId) => {
if (_.isPlainObject(data) || _.isArray(data)) {
_.each(data, o => addParentToSubObjects(o, parentId))
rootNodeMap.set(data, parentId)
_.each(data, o => addRootNodeToInlineObject(o, nodeId))
rootNodeMap.set(data, nodeId)
}
}

const trackSubObjectsToRootNodeId = node => {
/**
* Adds link between inline objects/arrays contained in Node object
* and that Node object.
* @param {Node} node Root Node
*/
const trackInlineObjectsInRootNode = node => {
_.each(node, (v, k) => {
// Ignore the node internal object.
if (k === `internal`) {
return
}
addParentToSubObjects(v, node.parent)
addRootNodeToInlineObject(v, node.id)
})
return node
}
exports.trackSubObjectsToRootNodeId = trackSubObjectsToRootNodeId
exports.trackInlineObjectsInRootNode = trackInlineObjectsInRootNode

// Read from cache the old node data.
let initialState = {}
Expand All @@ -40,7 +60,7 @@ try {
)

_.each(initialState.nodes, node => {
trackSubObjectsToRootNodeId(node)
trackInlineObjectsInRootNode(node)
})
} catch (e) {
// ignore errors.
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/schema/run-sift.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { connectionFromArray } = require(`graphql-skip-limit`)
const { createPageDependency } = require(`../redux/actions/add-page-dependency`)
const prepareRegex = require(`./prepare-regex`)
const Promise = require(`bluebird`)
const { trackInlineObjectsInRootNode } = require(`../redux`)

function awaitSiftField(fields, node, k) {
const field = fields[k]
Expand Down Expand Up @@ -109,6 +110,7 @@ module.exports = ({
return Promise.all(
nodes.map(node => resolveRecursive(node, fieldsToSift, type.getFields()))
).then(myNodes => {
myNodes = myNodes.map(trackInlineObjectsInRootNode)
if (!connection) {
const index = _.isEmpty(siftArgs)
? 0
Expand Down

0 comments on commit c8d3c63

Please sign in to comment.