Skip to content

Commit

Permalink
Merge pull request #4 from graphql-editor/fix-on-node-field-add-delete
Browse files Browse the repository at this point in the history
Fix on node field add delete
  • Loading branch information
aexol authored Jul 17, 2024
2 parents 1c4d70a + d715c6d commit 37433e7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-js-tree",
"version": "3.0.0",
"version": "3.0.1",
"private": false,
"license": "MIT",
"description": "GraphQL Parser providing simplier structure",
Expand Down
20 changes: 10 additions & 10 deletions src/TreeOperations/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
return;
}
};
const deleteFieldFromNode = (n: ParserField, i: number) => {
const deleteFieldFromNode = (n: ParserField, i: number, parentNode?: string) => {
if (n.data.type === TypeDefinition.InterfaceTypeDefinition) {
const argName = n.args[i].name;
tree.nodes
Expand All @@ -64,7 +64,7 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
}
n.args.splice(i, 1);
regenerateId(n);
mutateParentIfField(n);
mutateParentIfField(n, parentNode);
};

const updateFieldOnNode = (node: ParserField, i: number, updatedField: ParserField, parentNode?: string) => {
Expand All @@ -82,13 +82,13 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
mutateParentIfField(node, parentNode);
};

const addFieldToNode = (node: ParserField, f: ParserField) => {
const addFieldToNode = (node: ParserField, f: ParserField, parentNode?: string) => {
node.args?.push({ ...f });
if (node.data.type === TypeDefinition.InterfaceTypeDefinition) {
updateInterfaceNodeAddField(tree.nodes, node);
}
regenerateId(node);
mutateParentIfField(node);
mutateParentIfField(node, parentNode);
};
const renameRootNode = (node: ParserField, newName: string) => {
const isError = allNodes.map((n) => n.name).includes(newName);
Expand Down Expand Up @@ -118,20 +118,20 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
node.name = newName;
regenerateId(node);
};
const removeNode = (node: ParserField) => {
const removeNode = (node: ParserField, parentNode?: string) => {
if (node.data.type === TypeSystemDefinition.FieldDefinition) {
const parent = allNodes.find((parentNode) => parentNode.args.includes(node));
if (parent) {
const index = parent.args.indexOf(node);
deleteFieldFromNode(parent, index);
deleteFieldFromNode(parent, index, parentNode);
}
return;
}
if (node.data.type === TypeSystemDefinition.UnionMemberDefinition) {
const parent = allNodes.find((parentNode) => parentNode.args.includes(node));
if (parent) {
const index = parent.args.indexOf(node);
deleteFieldFromNode(parent, index);
deleteFieldFromNode(parent, index, parentNode);
}
return;
}
Expand All @@ -142,13 +142,13 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
recursivelyDeleteDirectiveArgument(allNodes, parent.name, node);
}
const index = parent.args.indexOf(node);
deleteFieldFromNode(parent, index);
deleteFieldFromNode(parent, index, parentNode);
} else {
const parent = allNodes.find((p) => p.args.some((a) => a.args.includes(node)));
const field = parent?.args.find((a) => a.args.includes(node));
if (field) {
const fieldIndex = field.args.findIndex((f) => f === node);
deleteFieldFromNode(field, fieldIndex);
deleteFieldFromNode(field, fieldIndex, parentNode);
}
}
return;
Expand All @@ -157,7 +157,7 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
const parent = allNodes.find((parentNode) => parentNode.args.includes(node));
if (parent) {
const index = parent.args.indexOf(node);
deleteFieldFromNode(parent, index);
deleteFieldFromNode(parent, index, parentNode);
}
return;
}
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/TreeOperations/tree.addField.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ describe('Tree Operations tests - adding fields', () => {

expect(treeMock.nodes[0].args[2].args).toContainEqual(limitNode);
});
test('Add field with args to node that exists in other type', () => {
const treeMock = createMock();
const oldField = treeMock.nodes[2].args[0].args[0];
const newField = {
...oldField,
name: 'secondName',
};

mutate(treeMock, treeMock.nodes).addFieldToNode(treeMock.nodes[2].args[0], newField, treeMock.nodes[2].id);
expect(treeMock.nodes[2].args[0].args).toContainEqual(newField);
expect(treeMock.nodes[0].args[3].args).not.toContainEqual(newField);
});
});
8 changes: 8 additions & 0 deletions src/__tests__/TreeOperations/tree.remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ describe('Tree Operations - node removal tests', () => {
expect(treeMock.nodes).not.toContainEqual(oldExtendNode);
expect(treeMock.nodes).not.toContainEqual(oldExtendNode2);
});
test('Delete field with args that exists in other type', () => {
const treeMock = createMock();
const oldField = treeMock.nodes[2].args[0].args[0];

mutate(treeMock, treeMock.nodes).removeNode(treeMock.nodes[2].args[0].args[0], treeMock.nodes[2].id);
expect(treeMock.nodes[2].args[0].args).not.toContainEqual(oldField);
expect(treeMock.nodes[0].args[3].args).toContainEqual(oldField);
});
});

0 comments on commit 37433e7

Please sign in to comment.