Skip to content

Commit

Permalink
Read heritage in input token
Browse files Browse the repository at this point in the history
  • Loading branch information
andrelmlins committed Sep 8, 2020
1 parent 8978f45 commit c3013a3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
3 changes: 2 additions & 1 deletion example/src/controllers/AnotherController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Query, Mutation } from 'recife';

import CommentModel from '../models/CommentModel';
import { CommentUpdate } from '../inputs/CommentInput';

interface CommentDelete {
postId: string;
Expand All @@ -14,7 +15,7 @@ class AnotherController {
}

@Mutation()
updateComment(input: { text: String }): Array<CommentModel> {
updateComment(input: CommentUpdate): Array<CommentModel> {
return [new CommentModel(), new CommentModel()];
}
}
Expand Down
5 changes: 4 additions & 1 deletion example/src/inputs/CommentInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export interface CommentDelete {
commentId?: String;
}

export class CommentUpdate {
export class CommentForm {
postId!: String;
}

export class CommentUpdate extends CommentForm {
text!: String;
commentId?: String;
params?: {
Expand Down
20 changes: 18 additions & 2 deletions src/compiler/token/Input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ts from 'typescript';
import Field from './Field';
import FieldTypeEnum from '../enum/FieldTypeEnum';
import { getReference } from '../../helpers/referenceHelper';

class Input {
public name: string;
Expand All @@ -23,13 +24,28 @@ class Input {
if (ts.isTypeLiteralNode(node)) {
this.compileObject(node);
} else if (ts.isTypeAliasDeclaration(node)) {
this.name = node.name.getText(this.sourceFile);
if (!fieldName) {
this.name = node.name.getText(this.sourceFile);
}
this.compileTypeLiteral(node);
} else if (ts.isClassDeclaration(node) || ts.isInterfaceDeclaration(node)) {
if (node.name) {
this.name = node.name.getText(this.sourceFile);
if (!fieldName) {
this.name = node.name.getText(this.sourceFile);
}
this.compileClassAndInterface(node);
}

if (node.heritageClauses) {
node.heritageClauses.forEach((heritage: ts.HeritageClause) => {
const nodeHeritage = getReference(heritage.types[0].expression.getText(sourceFile), sourceFile!);

if (nodeHeritage) {
const inputHeritage = new Input(nodeHeritage, '', sourceFile, this.name);
this.fields = this.fields.concat(inputHeritage.fields);
}
});
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/graph-compiler/snapshot/input-with-heritage/UserInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class FilterUserBase {
postId: String;
}

export default class FilterUser extends FilterUserBase {
name: String;
}
11 changes: 11 additions & 0 deletions test/graph-compiler/snapshot/input-with-heritage/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Query } from '../../../../src';
import FilterUser from './UserInput';

class InputController {
@Query()
getUser(input: FilterUser): string {
return '';
}
}

export default InputController;
24 changes: 24 additions & 0 deletions test/graph-compiler/snapshot/input-with-heritage/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use-strict';

module.exports = {
graphs: [
{
nameController: 'InputController',
options: {},
return: { type: 'String', isArray: false, isRequired: true },
name: 'getUser',
isExportDefaultController: true,
params: { name: 'input', isRequired: true, type: 'FilterUser' },
type: 'Query'
}
],
inputs: [
{
fields: [
{ visible: true, parentName: 'FilterUser', name: 'name', isRequired: true, isArray: false, type: 'String' },
{ visible: true, parentName: 'FilterUser', name: 'postId', isRequired: true, isArray: false, type: 'String' }
],
name: 'FilterUser'
}
]
};

0 comments on commit c3013a3

Please sign in to comment.