Skip to content

Commit

Permalink
Merge pull request #3 from graphql-editor/fix-field-on-node-update
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
aexol authored May 7, 2024
2 parents 63ccf8a + 253fae1 commit a898af6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/TreeOperations/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ import {
} from '@/TreeOperations/shared';

export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
const mutateParentIfField = (node: ParserField) => {
const mutateParentIfField = (node: ParserField, parentNode?: string) => {
if (node.data.type === TypeSystemDefinition.FieldDefinition) {
const parentNode = allNodes.find((an) => an.args.some((a) => a.id === node.id));
if (!parentNode) throw new Error('Invalid field definition');
const fieldIndex = parentNode.args.findIndex((a) => a.id == node.id);
updateFieldOnNode(parentNode, fieldIndex, node);
const findParentNode = parentNode
? allNodes.find((n) => n.id === parentNode)
: allNodes.find((an) => an.args.some((a) => a.id === node.id));
if (!findParentNode) throw new Error('Invalid field definition');
const fieldIndex = findParentNode.args.findIndex((a) => a.id == node.id);
updateFieldOnNode(findParentNode, fieldIndex, node);
return;
}
};
Expand All @@ -65,7 +67,7 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
mutateParentIfField(n);
};

const updateFieldOnNode = (node: ParserField, i: number, updatedField: ParserField) => {
const updateFieldOnNode = (node: ParserField, i: number, updatedField: ParserField, parentNode?: string) => {
regenerateId(updatedField);
if (node.data.type === TypeSystemDefinition.DirectiveDefinition) {
const oldField: ParserField = JSON.parse(JSON.stringify(node.args[i]));
Expand All @@ -77,7 +79,7 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
}
node.args[i] = updatedField;
regenerateId(node);
mutateParentIfField(node);
mutateParentIfField(node, parentNode);
};

const addFieldToNode = (node: ParserField, f: ParserField) => {
Expand Down
22 changes: 19 additions & 3 deletions src/__tests__/TreeOperations/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ const mainMock: ParserTree = {
name: 'friends',
type: ScalarTypes.String,
}),
createPlainField({
name: 'testUpdate',
type: ScalarTypes.Boolean,
args: [
createPlainInputValue({
name: 'firstName',
type: ScalarTypes.String,
}),
],
}),
],
}),
createRootField({
Expand Down Expand Up @@ -105,9 +115,15 @@ const mainMock: ParserTree = {
name: 'AnInput',
type: TypeDefinition.InputObjectTypeDefinition,
args: [
createPlainInputValue({
name: 'firstName',
type: ScalarTypes.String,
createPlainField({
name: 'testUpdate',
type: ScalarTypes.Boolean,
args: [
createPlainInputValue({
name: 'firstName',
type: ScalarTypes.String,
}),
],
}),
],
}),
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/TreeOperations/tree.rename.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ describe('Tree Operations tests - rename operations', () => {
fromInterface: [treeMock.nodes[0].name],
});
});
test('Update field on input definition inside node args', () => {
const treeMock = createMock();
const oldField = treeMock.nodes[2].args[0].args[0];
const updatedField = {
...oldField,
name: 'secondName',
};

mutate(treeMock, treeMock.nodes).updateFieldOnNode(
treeMock.nodes[2].args[0],
0,
updatedField,
treeMock.nodes[2].id,
);
expect(treeMock.nodes[2].args[0].args).toContainEqual(updatedField);
expect(treeMock.nodes[0].args[3].args).not.toContainEqual(updatedField);
});
test('Update field name on directive definition', () => {
const treeMock = createMock();
const updatedField = createParserField({
Expand Down

0 comments on commit a898af6

Please sign in to comment.