Mirror: The small sibling of the graphql package, slimmed down for client-side libraries.

handle inserted nodes with 'enter' correctly (#7)

Changed files
+43 -2
alias
language
+42 -1
alias/language/__tests__/visitor.test.js
···
// See: https://github.com/graphql/graphql-js/blob/976d64b/src/language/__tests__/visitor-test.ts
-
import { Kind, parse } from 'graphql';
+
import { Kind, parse, print } from 'graphql';
import { visit, visitInParallel, BREAK } from '../visitor';
const kitchenSinkQuery = String.raw`
···
['enter', 'Name', 'c'],
['leave', 'SelectionSet', undefined],
]);
+
});
+
+
it('handles deep immutable edits correctly when using "enter"', () => {
+
const formatNode = (node) => {
+
if (
+
node.selectionSet &&
+
!node.selectionSet.selections.some(
+
(node) =>
+
node.kind === Kind.FIELD &&
+
node.name.value === '__typename' &&
+
!node.alias
+
)
+
) {
+
return {
+
...node,
+
selectionSet: {
+
...node.selectionSet,
+
selections: [
+
...node.selectionSet.selections,
+
{
+
kind: Kind.FIELD,
+
name: {
+
kind: Kind.NAME,
+
value: '__typename',
+
},
+
},
+
],
+
},
+
};
+
}
+
};
+
const ast = parse('{ players { nodes { id } } }');
+
const expected = parse(
+
'{ players { nodes { id __typename } __typename } }'
+
);
+
const visited = visit(ast, {
+
Field: formatNode,
+
InlineFragment: formatNode,
+
});
+
+
expect(print(visited)).toEqual(print(expected));
});
it('visits kitchen sink', () => {
+1 -1
alias/language/visitor.mjs
···
if (resultLeave !== undefined) {
return resultLeave;
} else if (resultEnter !== undefined) {
-
return resultEnter;
+
return hasEdited ? copy : resultEnter;
} else {
return hasEdited ? copy : node;
}