Skip to content

Commit

Permalink
feat(generator): fixes for type handling and added support for lists …
Browse files Browse the repository at this point in the history
…and nullable types
  • Loading branch information
dotansimha committed Dec 14, 2016
1 parent f69f375 commit 8c4c498
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 45 deletions.
108 changes: 65 additions & 43 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import {
Source,
GraphQLType,
TypeMap,
GraphQLFieldDefinitionMap,
GraphQLEnumType,
GraphQLEnumValueDefinition,
GraphQLObjectType,
GraphQLFieldDefinition
GraphQLFieldDefinition,
GraphQLInterfaceType,
GraphQLInputObjectType,
GraphQLUnionType,
GraphQLList,
GraphQLNonNull,
getNamedType
} from 'graphql';
import {Codegen, Model, EnumValue, Field} from './interfaces';
import unregisterDecorator = Handlebars.unregisterDecorator;
Expand Down Expand Up @@ -47,52 +52,69 @@ const getTypeName = (type: GraphQLType) => {
}
};

export const prepareCodegen = (schema: GraphQLSchema, documents: Source[]): Codegen => {
let models: ModelsObject = {};
let typesMap: TypeMap = schema.getTypeMap();
const handleType = (typeName: string, type: GraphQLType) => {
let currentType: Model = {
name: typeName,
fields: [],
isFragment: false,
isEnum: false,
isObject: false,
isInterface: false,
isUnion: false
};

Object.keys(typesMap).forEach(typeName => {
const type: GraphQLType = typesMap[typeName];
let currentType: Model = {
name: typeName,
fields: [],
isFragment: false,
isEnum: false,
isInterface: false,
isInnerType: false
};
if (!shouldSkip(typeName)) {
if (type instanceof GraphQLEnumType) {
currentType.isEnum = true;
currentType.enumValues = type.getValues().map<EnumValue>((enumItem: GraphQLEnumValueDefinition) => {
return <EnumValue>{
name: enumItem.name,
description: enumItem.description,
value: enumItem.value
}
});
}
else if (type instanceof GraphQLObjectType || type instanceof GraphQLInputObjectType) {
currentType.isObject = true;
const fields = type.getFields();

if (!shouldSkip(typeName)) {
if (type instanceof GraphQLEnumType) {
currentType.isEnum = true;
currentType.enumValues = type.getValues().map<EnumValue>((enumItem: GraphQLEnumValueDefinition) => {
return <EnumValue>{
name: enumItem.name,
description: enumItem.description,
value: enumItem.value
}
currentType.fields = Object
.keys(fields)
.map((fieldName: string) => fields[fieldName])
.map<Field>((field: GraphQLFieldDefinition) => {
return {
name: field.name,
type: getTypeName(field.type),
isArray: isArray(field.type),
isRequired: isRequired(field.type)
};
});
}
else if (type instanceof GraphQLObjectType) {
currentType.isInterface = true;
const fields: GraphQLFieldDefinitionMap = type.getFields();

currentType.fields = Object
.keys(fields)
.map<GraphQLFieldDefinition>((fieldName: string) => fields[fieldName])
.map<Field>((field: GraphQLFieldDefinition) => {
return {
name: field.name,
type: getTypeName(field.type),
isArray: isArray(field.type),
isRequired: isRequired(field.type),
isNullable: false
};
});
}
}
else if (type instanceof GraphQLInterfaceType) {
currentType.isInterface = true;
// TODO: implemented
}
else if (type instanceof GraphQLUnionType) {
currentType.isUnion = true;
// TODO: implemented
}
else if (type instanceof GraphQLList || type instanceof GraphQLNonNull) {
return handleType(typeName, getNamedType(type));
}

models[typeName] = currentType;
return currentType;
}
else {
return null;
}
};

export const prepareCodegen = (schema: GraphQLSchema, documents: Source[]): Codegen => {
let models: ModelsObject = {};
let typesMap: TypeMap = schema.getTypeMap();

Object.keys(typesMap).forEach(typeName => {
models[typeName] = handleType(typeName, typesMap[typeName]);
});

return <Codegen>{
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export interface Field {
type: string;
isArray: boolean;
isRequired: boolean;
isNullable: boolean;
}

export interface EnumValue {
Expand All @@ -17,9 +16,10 @@ export interface Model {
parent?: string;
fields: Field[];
isFragment: boolean;
isObject: boolean;
isInterface: boolean;
isInnerType: boolean;
isEnum: boolean;
isUnion: boolean;
enumValues?: EnumValue[];
}

Expand Down

0 comments on commit 8c4c498

Please sign in to comment.