···
1
+
import { getVisitFn, visitInParallel, BREAK } from 'graphql/language/visitor';
3
+
export { getVisitFn, visitInParallel, BREAK };
5
+
export function visit(node, visitor) {
7
+
const ancestors = [];
9
+
function callback(node, key, parent, isLeaving) {
10
+
const visitFn = getVisitFn(visitor, node.kind, isLeaving)
12
+
const result = visitFn.call(visitor, node, key, parent, path, ancestors)
13
+
if (result === BREAK) throw BREAK
18
+
function traverse(node, key, parent) {
21
+
result = callback(node, key, parent, false);
22
+
if (result === false) {
24
+
} else if (result && typeof result.kind === 'string') {
28
+
ancestors.push(node);
31
+
let hasEdited = false;
32
+
for (const nodeKey in node) {
33
+
const value = node[nodeKey];
37
+
if (Array.isArray(value)) {
38
+
newValue = value.slice();
39
+
for (let index = 0; index < value.length; index++) {
40
+
if (value[index] != null && typeof value[index].kind === 'string') {
42
+
result = traverse(value[index], index, node);
44
+
if (result !== undefined) {
45
+
newValue[index] = result;
51
+
} else if (value != null && typeof value.kind === 'string') {
52
+
result = traverse(value, nodeKey, node);
53
+
if (result !== undefined) {
60
+
copy[nodeKey] = hasEdited ? newValue : value;
65
+
if (hasEdited) node = copy;
66
+
return callback(node, key, parent, true);
70
+
const result = traverse(node);
71
+
return result !== undefined ? result : node;
73
+
if (error !== BREAK) throw error;