Skip to content

Commit 6d7fbde

Browse files
committed
✨ added fromInterface
1 parent 846c52a commit 6d7fbde

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/Models/ParserTree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface ParserField {
1414
interfaces: string[];
1515
directives: ParserField[];
1616
description?: string;
17+
fromInterface?: string[];
1718
}
1819

1920
export interface ParserTree {

src/Parser/index.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export class Parser {
3333
if (isTypeSystemDefinitionNode(d) || isTypeSystemExtensionNode(d)) {
3434
const args = TypeResolver.resolveFieldsFromDefinition(d);
3535
if ('name' in d) {
36+
const interfaces = 'interfaces' in d && d.interfaces ? d.interfaces.map((i) => i.name.value) : [];
37+
const directives = 'directives' in d && d.directives ? TypeResolver.iterateDirectives(d.directives) : [];
38+
3639
return {
3740
name: d.name.value,
3841
type:
@@ -49,8 +52,8 @@ export class Parser {
4952
},
5053

5154
...('description' in d && d.description?.value ? { description: d.description.value } : {}),
52-
interfaces: 'interfaces' in d && d.interfaces ? d.interfaces.map((i) => i.name.value) : [],
53-
directives: 'directives' in d && d.directives ? TypeResolver.iterateDirectives(d.directives) : [],
55+
interfaces,
56+
directives,
5457
args,
5558
id: generateNodeId(d.name.value, d.kind as AllTypes, args),
5659
};
@@ -119,8 +122,9 @@ export class Parser {
119122
const nodeTree: ParserTree = {
120123
nodes: [...comments, ...nodes],
121124
};
125+
const allInterfaceNodes = nodeTree.nodes.filter((n) => n.data.type === TypeDefinition.InterfaceTypeDefinition);
122126
nodeTree.nodes.forEach((n) => {
123-
if (n.data?.type === TypeDefinition.ObjectTypeDefinition) {
127+
if (n.data.type === TypeDefinition.ObjectTypeDefinition) {
124128
if (operations.Query ? operations.Query === n.name : n.name === 'Query') {
125129
n.type.operations = [OperationType.query];
126130
}
@@ -131,6 +135,30 @@ export class Parser {
131135
n.type.operations = [OperationType.subscription];
132136
}
133137
}
138+
if (
139+
n.data.type === TypeDefinition.ObjectTypeDefinition ||
140+
n.data.type === TypeDefinition.InterfaceTypeDefinition
141+
) {
142+
if (n.interfaces) {
143+
const myInterfaces = allInterfaceNodes
144+
.filter((interfaceNode) => n.interfaces.includes(interfaceNode.name))
145+
.map((n) => ({
146+
name: n.name,
147+
argNames: n.args.map((a) => a.name),
148+
}));
149+
n.args = n.args.map((a) => {
150+
const interfaceNames = myInterfaces
151+
.filter((myInterface) => myInterface.argNames.includes(a.name))
152+
.map((i) => i.name);
153+
if (interfaceNames.length)
154+
return {
155+
...a,
156+
fromInterface: interfaceNames,
157+
};
158+
return a;
159+
});
160+
}
161+
}
134162
});
135163
return nodeTree;
136164
};

src/__tests__/Parser/Interface.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('Interfaces works as expected', () => {
3636
type: Options.name,
3737
},
3838
},
39-
39+
fromInterface: ['HasName'],
4040
data: {
4141
type: TypeSystemDefinition.FieldDefinition,
4242
},
@@ -102,14 +102,13 @@ describe('Interfaces works as expected', () => {
102102
args: [
103103
createParserField({
104104
name: 'name',
105-
106105
type: {
107106
fieldType: {
108107
name: ScalarTypes.String,
109108
type: Options.name,
110109
},
111110
},
112-
111+
fromInterface: ['HasName'],
113112
data: {
114113
type: TypeSystemDefinition.FieldDefinition,
115114
},

0 commit comments

Comments
 (0)