Skip to content

Commit 49f5a46

Browse files
committed
new version fix id duplicate
1 parent 266df68 commit 49f5a46

File tree

6 files changed

+76
-31
lines changed

6 files changed

+76
-31
lines changed

.eslintrc.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module.exports = {
22
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
33
extends: [
44
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
5-
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
65
'plugin:prettier/recommended', // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
76
],
87
parserOptions: {

package.json

+16-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-js-tree",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"private": false,
55
"license": "MIT",
66
"description": "GraphQL Parser providing simplier structure",
@@ -21,37 +21,24 @@
2121
"url": "https://github.com/graphql-editor/graphql-js-tree.git"
2222
},
2323
"devDependencies": {
24-
"@commitlint/cli": "^8.3.5",
25-
"@commitlint/config-conventional": "^8.3.4",
2624
"@types/graphql": "^14.5.0",
27-
"@types/jest": "^25.1.4",
28-
"@types/node": "^13.9.0",
29-
"@typescript-eslint/eslint-plugin": "^4.15.0",
30-
"@typescript-eslint/parser": "^4.15.0",
31-
"cz-conventional-changelog": "^3.1.0",
32-
"eslint": "^7.19.0",
33-
"eslint-config-prettier": "^7.2.0",
34-
"eslint-plugin-prettier": "^3.3.1",
35-
"husky": "^4.2.3",
36-
"jest": "^29.5.0",
37-
"prettier": "^2.0.2",
38-
"ts-jest": "^29.1.0",
39-
"ts-node": "^9.0.0",
40-
"ts-patch": "^3.0.0",
41-
"typescript": "^5.1.3",
25+
"@types/jest": "^29.5.11",
26+
"@types/node": "^20.10.5",
27+
"@typescript-eslint/eslint-plugin": "^6.16.0",
28+
"@typescript-eslint/parser": "^6.16.0",
29+
"eslint": "^8.56.0",
30+
"eslint-config-prettier": "^9.1.0",
31+
"eslint-plugin-prettier": "^5.1.2",
32+
"husky": "^8.0.3",
33+
"jest": "^29.7.0",
34+
"prettier": "^3.1.1",
35+
"ts-jest": "^29.1.1",
36+
"ts-node": "^10.9.2",
37+
"ts-patch": "^3.1.1",
38+
"typescript": "^5.3.3",
4239
"typescript-transform-paths": "^3.4.6"
4340
},
4441
"dependencies": {
4542
"graphql": "15.4.0"
46-
},
47-
"config": {
48-
"commitizen": {
49-
"path": "./node_modules/cz-conventional-changelog"
50-
}
51-
},
52-
"husky": {
53-
"hooks": {
54-
"pre-commit": "npm run lint"
55-
}
5643
}
57-
}
44+
}

src/TreeOperations/merge.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import { generateNodeId } from '@/shared';
77
const addFromLibrary = (n: ParserField): ParserField => ({ ...n, fromLibrary: true });
88

99
const mergeNode = (n1: ParserField, n2: ParserField) => {
10+
const args = [...n1.args, ...n2.args.map(addFromLibrary)];
1011
const mergedNode = {
1112
...n1,
12-
args: [...n1.args, ...n2.args.map(addFromLibrary)],
13+
id: generateNodeId(n1.name, n1.data.type, args),
14+
args,
1315
directives: [...n1.directives, ...n2.directives.map(addFromLibrary)],
1416
interfaces: [...n1.interfaces, ...n2.interfaces],
1517
} as ParserField;

src/TreeOperations/orphans.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ParserField, TypeSystemDefinition } from '@/Models';
2+
3+
export const removeOrphans = (possibleOrphans: string[], nodes: ParserField[]) => {
4+
const usedNodes = nodes.flatMap((n) => {
5+
return [...n.interfaces, ...n.args.flatMap((a) => [...a.name, ...a.args.map((ia) => ia.name)])];
6+
});
7+
return nodes.filter((n) => {
8+
if (!possibleOrphans.includes(n.name)) {
9+
return true;
10+
}
11+
if (n.data.type === TypeSystemDefinition.DirectiveDefinition) {
12+
return true;
13+
}
14+
return usedNodes.includes(n.name);
15+
});
16+
};

src/__tests__/GqlParser/GqlParserTreeToGql.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Query {
2020
type User implements Nameable{
2121
name: String
2222
age: Int
23+
lastName: String
2324
friend: User
2425
}
2526
type Person implements Nameable{
@@ -80,6 +81,11 @@ const userNode = createRootField({
8081
createPlainField({ name: 'friend', type: 'User' }),
8182
],
8283
});
84+
const nameableNode = createRootField({
85+
name: 'Nameable',
86+
type: TypeDefinition.InterfaceTypeDefinition,
87+
args: [createPlainField({ name: 'name', type: 'String' })],
88+
});
8389

8490
describe('Test generation of gql strings from the GqlParserTree', () => {
8591
it('Creates gql node from gql and schema', () => {
@@ -327,11 +333,13 @@ describe('Test generation of gql strings from the GqlParserTree', () => {
327333
it('works with inline fragments', () => {
328334
const mockQueryInline = `query MyQuery {
329335
namings{
336+
name
330337
... on Person {
331338
index
332339
}
333340
... on User{
334341
age
342+
lastName
335343
}
336344
}
337345
}`;
@@ -344,6 +352,10 @@ describe('Test generation of gql strings from the GqlParserTree', () => {
344352
name: 'namings',
345353
node: queryNode.args[2],
346354
children: [
355+
{
356+
node: nameableNode.args[0],
357+
name: 'name',
358+
},
347359
{
348360
inlineFragment: true,
349361
node: personNode,
@@ -364,6 +376,10 @@ describe('Test generation of gql strings from the GqlParserTree', () => {
364376
name: 'age',
365377
node: userNode.args[1],
366378
},
379+
{
380+
name: 'lastName',
381+
node: userNode.args[2],
382+
},
367383
],
368384
},
369385
],

src/__tests__/TreeOperations/merge.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ import { expectTrimmedEqual } from '@/__tests__/TestUtils';
99
// `;
1010

1111
describe('Merging GraphQL Schemas', () => {
12+
it('Should merge scalars', () => {
13+
const baseSchema = `
14+
scalar URL
15+
scalar JSON
16+
`;
17+
18+
const mergingSchema = `
19+
scalar URL
20+
scalar DATE
21+
`;
22+
const t1 = mergeSDLs(baseSchema, mergingSchema);
23+
if (t1.__typename === 'error') throw new Error('Invalid parse');
24+
expectTrimmedEqual(
25+
t1.sdl,
26+
`
27+
scalar JSON
28+
scalar URL
29+
scalar DATE`,
30+
);
31+
});
1232
it('Should merge fields of both nodes', () => {
1333
const baseSchema = `
1434
type Person{
@@ -136,6 +156,11 @@ describe('Merging GraphQL Schemas', () => {
136156
`;
137157
const t1 = mergeSDLs(baseSchema, mergingSchema);
138158
if (t1.__typename === 'error') throw new Error('Invalid parse');
159+
expect(
160+
t1.nodes.every((n, i) => {
161+
return i === t1.nodes.findIndex((t1n) => t1n.id === n.id);
162+
}),
163+
).toBeTruthy();
139164
expectTrimmedEqual(
140165
t1.sdl,
141166
`

0 commit comments

Comments
 (0)