Skip to content

Commit

Permalink
✨ Provide feature to clean unexisting directives
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Sep 29, 2023
1 parent ed41120 commit 266df68
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-js-tree",
"version": "1.0.5",
"version": "1.0.6",
"private": false,
"license": "MIT",
"description": "GraphQL Parser providing simplier structure",
Expand Down Expand Up @@ -54,4 +54,4 @@
"pre-commit": "npm run lint"
}
}
}
}
18 changes: 17 additions & 1 deletion src/TreeOperations/directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Options, ParserField } from '@/Models';
import { Options, ParserField, TypeSystemDefinition } from '@/Models';
import { checkValueType } from '@/TreeOperations/tree';

export const recursivelyDeleteDirectiveNodes = (nodes: ParserField[], directiveName: string) => {
Expand All @@ -9,6 +9,22 @@ export const recursivelyDeleteDirectiveNodes = (nodes: ParserField[], directiveN
recursivelyDeleteDirectiveNodes(n.args, directiveName);
});
};
export const recursivelyDeleteAllDirectiveNodes = (nodes: ParserField[]) => {
const indexesToRemove: number[] = [];
nodes.forEach((n, i) => {
if (n.data.type === TypeSystemDefinition.DirectiveDefinition) {
indexesToRemove.push(i);
}
if (n.directives) {
n.directives = [];
}
recursivelyDeleteAllDirectiveNodes(n.args);
});
indexesToRemove.reverse();
indexesToRemove.forEach((itr) => {
nodes.splice(itr, 1);
});
};

export const recursivelyRenameDirectiveNodes = (
nodes: ParserField[],
Expand Down
5 changes: 5 additions & 0 deletions src/TreeOperations/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@/Models';
import { getTypeName } from '@/shared';
import {
recursivelyDeleteAllDirectiveNodes,
recursivelyDeleteDirectiveArgument,
recursivelyDeleteDirectiveNodes,
recursivelyRenameDirectiveNodes,
Expand Down Expand Up @@ -234,6 +235,9 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
type: checkValueType(node, allNodes),
};
};
const removeAllDirectives = () => {
recursivelyDeleteAllDirectiveNodes(allNodes);
};
return {
updateFieldOnNode,
addFieldToNode,
Expand All @@ -242,6 +246,7 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
implementInterface,
deImplementInterface,
setValueNode,
removeAllDirectives,
};
};

Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/Parser/ExoticDirective.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createPlainDirectiveImplementation, createRootField } from '@/shared';
import { ParserTree, TypeDefinition } from '../../Models';
import { Parser } from '../../Parser';

// TODO: Add schema directive test
// TODO: Add directive with arguments test

describe('Exotic Directive tests on parser', () => {
test(`Test non existing directives`, () => {
const schema = `
type Person @model
`;
const tree = Parser.parse(schema);
const treeMock: ParserTree = {
nodes: [
createRootField({
name: 'Person',
type: TypeDefinition.ObjectTypeDefinition,
directives: [
createPlainDirectiveImplementation({
name: 'model',
}),
],
}),
],
};
expect(tree.nodes).toEqual(expect.arrayContaining(treeMock.nodes));
});
});
12 changes: 12 additions & 0 deletions src/__tests__/TreeOperations/tree.cleanDirectives.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { mutate } from '@/TreeOperations/tree';
import { createMock } from '@/__tests__/TreeOperations/mocks';

describe('Tree Operations - directive cleaning tests', () => {
test('Delete directive node from tree using cleanall', () => {
const treeMock = createMock();
const directiveNode = JSON.parse(JSON.stringify(treeMock.nodes[5]));
mutate(treeMock, treeMock.nodes).removeAllDirectives();
expect(treeMock.nodes).not.toContainEqual(directiveNode);
expect(treeMock.nodes[0].directives).toHaveLength(0);
});
});

0 comments on commit 266df68

Please sign in to comment.