Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use typescript required properties on interfaces to inform required in GQL #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts2gql",
"version": "1.0.1",
"version": "2.0.1",
"description": "Converts a TypeScript type hierarchy into GraphQL's IDL.",
"homepage": "https://github.com/convoyinc/ts2gql",
"bugs": "https://github.com/convoyinc/ts2gql/issues",
Expand Down
1 change: 1 addition & 0 deletions src/Collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export default class Collector {
type: 'property',
name: node.name.getText(),
signature: this._walkNode(node.type),
hasQuestionToken: !!node.questionToken,
};
}

Expand Down
9 changes: 7 additions & 2 deletions src/Emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default class Emitter {
type: 'property',
name: '__placeholder',
signature: {type: 'boolean'},
hasQuestionToken: false,
});
}

Expand All @@ -103,7 +104,7 @@ export default class Emitter {
const returnType = this._emitExpression(member.returns);
return `${this._name(member.name)}${parameters}: ${returnType}`;
} else if (member.type === 'property') {
return `${this._name(member.name)}: ${this._emitExpression(member.signature)}`;
return `${this._name(member.name)}: ${this._emitExpression(member.signature)}${this._emitRequiredToken(member.hasQuestionToken)}`;
} else {
throw new Error(`Can't serialize ${member.type} as a property of an interface`);
}
Expand Down Expand Up @@ -151,7 +152,7 @@ export default class Emitter {
if (member.type !== 'property') {
throw new Error(`Expected members of literal object to be properties; got ${member.type}`);
}
return `${this._name(member.name)}: ${this._emitExpression(member.signature)}`;
return `${this._name(member.name)}: ${this._emitExpression(member.signature)}${this._emitRequiredToken(member.hasQuestionToken)}`;
})
.join(', ');
} else {
Expand All @@ -160,6 +161,10 @@ export default class Emitter {
}
}

_emitRequiredToken(hasQuestionToken:boolean) {
return !hasQuestionToken ? '!' : '';
}

// Utility

_name = (name:types.SymbolName):string => {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface PropertyNode extends ComplexNode {
type:'property';
name:string;
signature:Node;
hasQuestionToken:boolean;
}

export interface AliasNode extends ComplexNode {
Expand Down