diff --git a/lib/src/builders/type_builder.dart b/lib/src/builders/type_builder.dart index daa0bcb..7892933 100644 --- a/lib/src/builders/type_builder.dart +++ b/lib/src/builders/type_builder.dart @@ -19,15 +19,26 @@ class TypeBuilder { TypeBuilder(this.type, this.config); Future build() async { - _addFields(); - _addConstructor(); - _addFromJson(); - _addToJson(); - String current = stringBuffer.toString(); - stringBuffer.clear(); - current = _wrapWith(current, "class ${type.name}{", "}"); - stringBuffer.write(current.toString()); - _addImports(); + if (type.fields != null) { + _addFields(); + } + if (type.inputFields != null) { + _addInputFields(); + } + if (type.kind == 'ENUM') { + _addEnumValues(); + + } else { + _addConstructor(); + _addFromJson(); + _addToJson(); + String current = stringBuffer.toString(); + stringBuffer.clear(); + current = _wrapWith(current, "class ${type.name}{", "}"); + stringBuffer.write(current.toString()); + _addImports(); + } + await _saveToFile(); } @@ -35,15 +46,13 @@ class TypeBuilder { StringBuffer importBuffer = StringBuffer(); localFields.unique((field) => field.type).forEach((field) { if (field.object == true) { - if(config.dynamicImportPath){ - importBuffer.writeln( - "import 'package:${config.packageName}/${config.modelsDirectoryPath.replaceAll(r"lib/", "")}/${pascalToSnake(field.type)}.dart';" - .replaceAll(r"//", r"/")); - } - else{ + if (config.dynamicImportPath) { importBuffer.writeln( - "import '${pascalToSnake(field.type)}.dart';" - .replaceAll(r"//", r"/")); + "import 'package:${config.packageName}/${config.modelsDirectoryPath.replaceAll(r"lib/", "")}/${pascalToSnake(field.type)}.dart';" + .replaceAll(r"//", r"/")); + } else { + importBuffer.writeln("import '${pascalToSnake(field.type)}.dart';" + .replaceAll(r"//", r"/")); } } }); @@ -83,15 +92,21 @@ class TypeBuilder { stringBuffer .write(_wrapWith(toJsonBuilder.toString(), "Map toJson(){", "}")); } - + _addFromJson() { StringBuffer fromJsonBuilder = StringBuffer(); localFields.forEach((field) { if (field.list == true) { fromJsonBuilder.write(""" ${field.name} = json['${field.name}']!=null ? -${field.object == true ? "List.generate(json['${field.name}'].length, (index)=> ${field.type}.fromJson(json['${field.name}'][index]))" : field.type == "DateTime" ? "List.generate(json['${field.name}'].length, (index)=> DateTime.parse(json['${field.name}'][index]))" : "json['${field.name}']"}: null; +${field.object == true ? + "List.generate(json['${field.name}'].length, (index)=> ${field.type}.fromJson(json['${field.name}'][index]))" + : field.type == "DateTime" ? + "List.generate(json['${field.name}'].length, (index)=> DateTime.parse(json['${field.name}'][index]))" + : "json['${field.name}'].map<${field.type}>((o)=>o.to${field.type}()).toList()"}: null; """); + } else if (field.isEnum){ + // fromJsonBuilder.writeln("${field.name} = json['${field.name}'];"); } else if (field.object == true) { fromJsonBuilder.writeln( "${field.name} = json['${field.name}']!=null ? ${field.type}.fromJson(json['${field.name}']) : null;"); @@ -99,7 +114,12 @@ ${field.object == true ? "List.generate(json['${field.name}'].length, (index)=> fromJsonBuilder.writeln( "${field.name} = json['${field.name}']!=null ? DateTime.parse(json['${field.name}']) : null;"); } else { - fromJsonBuilder.writeln("${field.name} = json['${field.name}'];"); + if(field.type=='double'){ + fromJsonBuilder.writeln("${field.name} = json['${field.name}']?.toDouble();"); + + }else{ + fromJsonBuilder.writeln("${field.name} = json['${field.name}'];"); + } } }); stringBuffer.writeln(); @@ -124,6 +144,29 @@ ${field.object == true ? "List.generate(json['${field.name}'].length, (index)=> }); } + _addInputFields() { + type.inputFields.forEach((field) { + _typeOrdering(field.type, field.name); + }); + } + + _addEnumValues() { + // stringBuffer.writeln("import 'package:flutter/foundation.dart';"); + stringBuffer + .writeln('enum ${type.name}{\n${type.enumValues.map((e) => e.name).join(',\n')}\n}'); +// stringBuffer.writeln(''' +// extension ${type.name}Index on ${type.name} { +// // Overload the [] getter to get the name of the fruit. +// operator[](String key) => (name){ +// switch(name) { +// ${type.enumValues.map((e) => "case \'${e.name}\': return ${type.name}.${e.name};" ).join('\n')} +// default: throw RangeError("enum ${type.name} contains no value '\$name'"); +// } +// }(key); +// } +// '''); + } + _addConstructor() { StringBuffer constructorBuffer = StringBuffer(); for (int i = 0; i < localFields.length; i++) { @@ -156,9 +199,17 @@ ${field.object == true ? "List.generate(json['${field.name}'].length, (index)=> type: TypeConverters().nonObjectTypes[type.name.toLowerCase()], object: false); localFields.add(localField); + } else if (type.kind == 'ENUM') { + localField = LocalField( + name: fieldName, + list: list, + type: TypeConverters().nonObjectTypes['string'], + object: false, + /*isEnum: true*/); + localFields.add(localField); } else { - localField = - LocalField(name: fieldName, list: list, type: type.name, object: true); + localField = LocalField( + name: fieldName, list: list, type: type.name, object: true); localFields.add(localField); } stringBuffer.writeln(localField.toDeclarationStatement()); @@ -179,8 +230,10 @@ class LocalField { final bool list; final String type; final bool object; + final bool isEnum; - LocalField({this.name, this.list, this.type, this.object}); + LocalField( + {this.name, this.list, this.type, this.object, this.isEnum = false}); String toDeclarationStatement() { return "${list ? "List<" : ""}${type ?? "var"}${list ? ">" : ""} $name;"; diff --git a/lib/src/graphql_to_dart_base.dart b/lib/src/graphql_to_dart_base.dart index 5a2882e..c56a69d 100644 --- a/lib/src/graphql_to_dart_base.dart +++ b/lib/src/graphql_to_dart_base.dart @@ -41,6 +41,21 @@ class GraphQlToDart { TypeBuilder builder = TypeBuilder(type, config); await builder.build(); } + if(type.kind=='INPUT_OBJECT'&& + type.fields==null&& + type.inputFields!=null){ + print("Creating input model from: ${type.name}"); + TypeBuilder builder = TypeBuilder(type, config); + await builder.build(); + } + if(type.kind=='ENUM'&& + type.fields==null&& + !type.name.startsWith("__") && + type.inputFields==null){ + print("Creating enum model from: ${type.name}"); + TypeBuilder builder = TypeBuilder(type,config); + await builder.build(); + } }); print("Formatting Generated Files"); await runFlutterFormat(); diff --git a/lib/src/introspection_api_client/client.dart b/lib/src/introspection_api_client/client.dart index 779e112..0b9ce0c 100644 --- a/lib/src/introspection_api_client/client.dart +++ b/lib/src/introspection_api_client/client.dart @@ -8,10 +8,10 @@ class LocalGraphQLClient { init(Config config) { final HttpLink _httpLink = HttpLink( - uri: config.graphQLEndpoint, + config.graphQLEndpoint, ); client = GraphQLClient( - cache: InMemoryCache(), + cache: GraphQLCache(store:InMemoryStore()), link: _httpLink, ); } diff --git a/lib/src/introspection_api_client/queries.dart b/lib/src/introspection_api_client/queries.dart index d89a7c3..b07051d 100644 --- a/lib/src/introspection_api_client/queries.dart +++ b/lib/src/introspection_api_client/queries.dart @@ -17,6 +17,29 @@ class Queries { } } } + enumValues{ + name + } + inputFields{ + name + description + type{ + name + kind + ofType{ + name + kind + ofType{ + name + kind + ofType{ + name + kind + } + } + } + } + } fields{ name description diff --git a/lib/src/models/graphql_types.dart b/lib/src/models/graphql_types.dart index 62ff276..8a303a1 100644 --- a/lib/src/models/graphql_types.dart +++ b/lib/src/models/graphql_types.dart @@ -26,7 +26,8 @@ class Types { String kind; String name; Type ofType; - var inputFields; + List inputFields; + List enumValues; Types({this.fields, this.kind, this.name, this.ofType}); Types.fromJson(Map json) { @@ -36,7 +37,19 @@ class Types { fields.add(Fields.fromJson(v)); }); } - inputFields = json['inputFields']; + if (json['inputFields'] != null) { + inputFields = List(); + json['inputFields'].forEach((v) { + inputFields.add(Fields.fromJson(v)); + }); + } + if (json['enumValues'] != null) { + enumValues = List(); + json['enumValues'].forEach((v) { + print(v); + enumValues.add(Fields.fromJson(v)); + }); + } kind = json['kind']; name = json['name']; ofType = json['ofType'] != null ? Type.fromJson(json['ofType']) : null; diff --git a/pubspec.yaml b/pubspec.yaml index 1a2c822..4b3aa3c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,6 +7,6 @@ environment: dependencies: yaml: ^2.2.1 - graphql: ^3.0.1 + graphql: ^4.0.0-beta.2 recase: ^3.0.0