Skip to content

Commit b51a4b3

Browse files
committed
✨ Added fromLibrary flag
1 parent e4654ad commit b51a4b3

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-js-tree",
3-
"version": "0.3.7",
3+
"version": "0.3.8",
44
"private": false,
55
"license": "MIT",
66
"description": "GraphQL Parser providing simplier structure",

src/Models/ParserTree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface ParserField {
1515
directives: ParserField[];
1616
description?: string;
1717
fromInterface?: string[];
18+
fromLibrary?: boolean;
1819
value?: {
1920
type: Value;
2021
value?: string;

src/TreeOperations/merge.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { Parser } from '@/Parser';
33
import { isExtensionNode } from '@/TreeOperations/shared';
44
import { TreeToGraphQL } from '@/TreeToGraphQL';
55

6+
const addFromLibrary = (n: ParserField): ParserField => ({ ...n, fromLibrary: true });
7+
68
const mergeNode = (n1: ParserField, n2: ParserField) => {
79
const mergedNode = {
810
...n1,
9-
args: [...n1.args, ...n2.args],
10-
directives: [...n1.directives, ...n2.directives],
11+
args: [...n1.args, ...n2.args.map(addFromLibrary)],
12+
directives: [...n1.directives, ...n2.directives.map(addFromLibrary)],
1113
interfaces: [...n1.interfaces, ...n2.interfaces],
1214
} as ParserField;
1315
//dedupe

src/__tests__/TreeOperations/merge.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { mergeSDLs } from '@/TreeOperations/merge';
1+
import { Parser } from '@/Parser';
2+
import { mergeSDLs, mergeTrees } from '@/TreeOperations/merge';
23
import { expectTrimmedEqual } from '@/__tests__/TestUtils';
34

45
// const mergingErrorSchema = `
@@ -81,6 +82,37 @@ describe('Merging GraphQL Schemas', () => {
8182
}`,
8283
);
8384
});
85+
it('Tree test - Should merge interfaces and implementation of both nodes matching library fields.', () => {
86+
const baseSchema = `
87+
type Person implements Node{
88+
firstName: String
89+
health: String
90+
_id: String
91+
}
92+
interface Node {
93+
_id: String
94+
}
95+
`;
96+
97+
const mergingSchema = `
98+
type Person implements Dateable{
99+
lastName: String
100+
createdAt: String
101+
}
102+
interface Dateable {
103+
createdAt: String
104+
}
105+
`;
106+
const baseTree = Parser.parse(baseSchema);
107+
const libraryTree = Parser.parse(mergingSchema);
108+
const mergedTree = mergeTrees(baseTree, libraryTree);
109+
if (mergedTree.__typename === 'error') throw new Error('Invalid parse');
110+
const PersonNode = mergedTree.nodes.find((n) => n.name === 'Person');
111+
const lastNameField = PersonNode?.args.find((a) => a.name === 'lastName');
112+
const createdAtField = PersonNode?.args.find((a) => a.name === 'createdAt');
113+
expect(lastNameField?.fromLibrary).toBeTruthy();
114+
expect(createdAtField?.fromLibrary).toBeTruthy();
115+
});
84116
it('Should merge interfaces and implementation of both nodes', () => {
85117
const baseSchema = `
86118
type Person implements Node{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy