Skip to content

Commit

Permalink
feat(core): added support for schema interfaces, and update typescrip…
Browse files Browse the repository at this point in the history
…t generator
  • Loading branch information
dotansimha committed Dec 14, 2016
1 parent f432ecd commit 22d3ad0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 49 deletions.
6 changes: 5 additions & 1 deletion dev-test/githunt/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ export interface User {
html_url: string;
}

export interface Comment {
export interface Comment extends TestInterface {
id: number;
postedBy: User;
createdAt: number;
content: string;
repoName: string;
}

export interface TestInterface {
content: string;
}

export interface Vote {
vote_value: number;
}
Expand Down
50 changes: 21 additions & 29 deletions generators/typescript-multiple-files/document.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,30 @@
{{/each ~}}

export namespace {{ name }} {
{{#if hasVariables}}
export interface Variables {
{{#each variables}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}}{{#unless isRequired}} | null{{/unless}};
{{/each}}
}
{{#if hasVariables}}
export type Variables = {
{{#each variables}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}}{{#unless isRequired}} | null{{/unless}};
{{/each}}
}

{{/if ~}}
{{/if ~}}

{{#each innerTypes}}
{{#if usingFragments}}
export type {{ name }} = {{#each fragmentsUsed ~}}{{this}}.Fragment & {{/each }}{
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}};
{{/each}}
}
{{else}}
export interface {{name}} {
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}};
{{/each}}
}
{{/if}}
{{#each innerTypes}}
export type {{ name }} = {
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}};
{{/each}}
} {{#each fragmentsUsed}}& {{this}}.Fragment {{/each }}{{#if hasInlineFragments}}& ({{#each inlineFragments}}{{ typeName }}{{/each}} | {}) {{/if}}

{{/each ~}}
{{/each ~}}

{{#each resultFields}}
export interface {{ name }}Result {
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}};
{{/each}}
}
export type Result = {
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}};
{{/each}}
}

{{/each ~}}
}
{{/each ~}}
}
8 changes: 4 additions & 4 deletions generators/typescript-multiple-files/model.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
{{/each ~}}

{{#if isObject ~}}
export interface {{ name }} {
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}}{{#unless isRequired}} | null{{/unless}};
{{/each ~}}
export interface {{ name }}{{#if hasImplementedInterfaces}} extends {{#each implementedInterfaces}}{{this}}{{/each}}{{/if}} {
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}}{{#unless isRequired}} | null{{/unless}};
{{/each ~}}
}

{{/if ~}}
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript-single-file/template.handlebars
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{#each models ~}}
{{#if isObject ~}}
export interface {{ name }} {
export interface {{ name }}{{#if hasImplementedInterfaces}} extends {{#each implementedInterfaces}}{{this}}{{/each}}{{/if}} {
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}}{{#unless isRequired}} | null{{/unless}};
{{/each ~}}
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ export interface Model {
name?: string;
description?: string;
fields?: Field[];
isFragment?: boolean;
isObject?: boolean;
isInterface?: boolean;
isFragment?: boolean;
isEnum?: boolean;
isUnion?: boolean;
usingFragments?: boolean;
enumValues?: EnumValue[];
fragmentsUsed?: string[];
inlineFragments?: InlineFragment[];
hasInlineFragments?: boolean;
imports?: string[];
implementedInterfaces?: string[];
hasImplementedInterfaces?: boolean;
}

export interface CodegenDocument {
Expand Down
25 changes: 14 additions & 11 deletions src/model-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ export const handleType = (primitivesMap: any, typeName: string, type: GraphQLTy
imports: [],
name: typeName,
fields: [],
isFragment: false,
isEnum: false,
isObject: false,
isInterface: false,
isUnion: false
isObject: false
};

if (!shouldSkip(typeName)) {
Expand All @@ -65,10 +62,17 @@ export const handleType = (primitivesMap: any, typeName: string, type: GraphQLTy
};
});
}
else if (type instanceof GraphQLObjectType || type instanceof GraphQLInputObjectType) {
else if (type instanceof GraphQLObjectType || type instanceof GraphQLInputObjectType || type instanceof GraphQLInterfaceType) {
currentType.isObject = true;
const fields = type.getFields();

if (type instanceof GraphQLObjectType) {
currentType.implementedInterfaces = type.getInterfaces().map<string>(interf => {
return interf.name;
});
currentType.hasImplementedInterfaces = currentType.implementedInterfaces.length > 0;
}

currentType.fields = Object
.keys(fields)
.map((fieldName: string) => fields[fieldName])
Expand All @@ -87,13 +91,12 @@ export const handleType = (primitivesMap: any, typeName: string, type: GraphQLTy
};
});
}
else if (type instanceof GraphQLInterfaceType) {
currentType.isInterface = true;
// TODO: implemented
}
else if (type instanceof GraphQLUnionType) {
currentType.isUnion = true;
// TODO: implemented
// TODO: Handle union
//
// type.getTypes().map(type => {
// return type.name
// });
}
else if (type instanceof GraphQLList || type instanceof GraphQLNonNull) {
return handleType(primitivesMap, typeName, getNamedType(type));
Expand Down

0 comments on commit 22d3ad0

Please sign in to comment.