Skip to content
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
99 changes: 76 additions & 23 deletions lib/src/builders/type_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,40 @@ 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();
}

_addImports() {
StringBuffer importBuffer = StringBuffer();
localFields.unique<String>((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"/"));
}
}
});
Expand Down Expand Up @@ -83,23 +92,34 @@ 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;");
} else if (field.type == "DateTime") {
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();
Expand All @@ -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++) {
Expand Down Expand Up @@ -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());
Expand All @@ -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;";
Expand Down
15 changes: 15 additions & 0 deletions lib/src/graphql_to_dart_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions lib/src/introspection_api_client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down
23 changes: 23 additions & 0 deletions lib/src/introspection_api_client/queries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions lib/src/models/graphql_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Types {
String kind;
String name;
Type ofType;
var inputFields;
List<Fields> inputFields;
List<Fields> enumValues;
Types({this.fields, this.kind, this.name, this.ofType});

Types.fromJson(Map<String, dynamic> json) {
Expand All @@ -36,7 +37,19 @@ class Types {
fields.add(Fields.fromJson(v));
});
}
inputFields = json['inputFields'];
if (json['inputFields'] != null) {
inputFields = List<Fields>();
json['inputFields'].forEach((v) {
inputFields.add(Fields.fromJson(v));
});
}
if (json['enumValues'] != null) {
enumValues = List<Fields>();
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;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ environment:

dependencies:
yaml: ^2.2.1
graphql: ^3.0.1
graphql: ^4.0.0-beta.2
recase: ^3.0.0