-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgraphql_to_dart_base.dart
More file actions
73 lines (69 loc) · 2.38 KB
/
Copy pathgraphql_to_dart_base.dart
File metadata and controls
73 lines (69 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import 'dart:async';
import 'dart:io';
import 'package:graphql_to_dart/src/builders/type_builder.dart';
import 'package:graphql_to_dart/src/constants/files.dart';
import 'package:graphql_to_dart/src/constants/type_converters.dart';
import 'package:graphql_to_dart/src/introspection_api_client/client.dart';
import 'package:graphql_to_dart/src/models/config.dart';
import 'package:graphql_to_dart/src/models/graphql_types.dart';
import 'package:graphql_to_dart/src/parsers/config_parser.dart';
class GraphQlToDart {
final String yamlFilePath;
GraphQlToDart(this.yamlFilePath);
static const List<String> ignoreFields = [
"rootquerytype",
"rootsubscriptiontype",
"rootmutationtype",
"mutation",
"query",
"subscription"
];
init() async {
Config config = await ConfigParser.parse(yamlFilePath);
ValidationResult result = await config.validate();
if (result.hasError) {
throw result.errorMessage;
}
LocalGraphQLClient localGraphQLClient = LocalGraphQLClient();
localGraphQLClient.init(config);
final schema = await localGraphQLClient.fetchTypes();
TypeConverters converters = TypeConverters();
converters.overrideTypes(config.typeOverride);
await Future.forEach(schema.types, (Types type) async {
if (type.fields != null &&
type.inputFields == null &&
!type.name.startsWith("__") &&
!ignoreFields.contains(type.name?.toLowerCase())) {
print("Creating model from: ${type.name}");
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();
return;
}
Future runFlutterFormat() async {
Process.runSync(
"flutter",
["format", FileConstants().modelsDirectory.path],
runInShell: true,
);
print("Formatted Generated Files");
}
}