@@ -24,16 +24,18 @@ String _addGraphQLExtensionToPathIfNeeded(String path) {
24
24
}
25
25
26
26
List <String > _builderOptionsToExpectedOutputs (BuilderOptions builderOptions) {
27
- final schemaMaps =
27
+ final schemaMapping =
28
28
GeneratorOptions .fromJson (builderOptions.config).schemaMapping;
29
29
30
- if (schemaMaps.any ((s) => s.output == null )) {
31
- throw Exception ('''One or more SchemaMap configurations miss an output!
32
- Please check your build.yaml file.
33
- ''' );
30
+ if (schemaMapping.isEmpty) {
31
+ throw MissingBuildConfigurationException ('schema_mapping' );
34
32
}
35
33
36
- return schemaMaps
34
+ if (schemaMapping.any ((s) => s.output == null )) {
35
+ throw MissingBuildConfigurationException ('schema_mapping => output' );
36
+ }
37
+
38
+ return schemaMapping
37
39
.map ((s) {
38
40
final outputWithoutLib = s.output! .replaceAll (RegExp (r'^lib/' ), '' );
39
41
@@ -70,49 +72,72 @@ class GraphQLQueryBuilder implements Builder {
70
72
r'$lib$' : expectedOutputs,
71
73
};
72
74
75
+ /// read asset files
76
+ Future <List <DocumentNode >> readGraphQlFiles (
77
+ BuildStep buildStep,
78
+ String schema,
79
+ ) async {
80
+ final schemaAssetStream = buildStep.findAssets (Glob (schema));
81
+
82
+ return await schemaAssetStream
83
+ .asyncMap (
84
+ (asset) async => parseString (
85
+ await buildStep.readAsString (asset),
86
+ url: asset.path,
87
+ ),
88
+ )
89
+ .toList ();
90
+ }
91
+
73
92
@override
74
93
Future <void > build (BuildStep buildStep) async {
75
- if (options.fragmentsGlob != null ) {
76
- final fragmentStream = buildStep.findAssets (Glob (options.fragmentsGlob! ));
77
- final fDocs = await fragmentStream
78
- .asyncMap (
79
- (asset) async => parseString (
80
- await buildStep.readAsString (asset),
81
- url: asset.path,
82
- ),
83
- )
84
- .toList ();
85
- fDocs.forEach (
86
- (fDoc) => fragmentsCommon.addAll (
87
- fDoc.definitions.whereType <FragmentDefinitionNode >().toList ()),
94
+ final fragmentsGlob = options.fragmentsGlob;
95
+ if (fragmentsGlob != null ) {
96
+ fragmentsCommon.addAll (
97
+ (await readGraphQlFiles (buildStep, fragmentsGlob))
98
+ .map ((e) => e.definitions.whereType <FragmentDefinitionNode >())
99
+ .expand ((e) => e)
100
+ .toList (),
88
101
);
102
+
103
+ if (fragmentsCommon.isEmpty) {
104
+ throw MissingFilesException (fragmentsGlob);
105
+ }
89
106
}
90
107
91
108
for (final schemaMap in options.schemaMapping) {
92
- final buffer = StringBuffer ();
93
- final outputFileId = AssetId (buildStep.inputId.package,
94
- _addGraphQLExtensionToPathIfNeeded (schemaMap.output! ));
109
+ final queriesGlob = schemaMap.queriesGlob;
110
+ final schema = schemaMap.schema;
111
+ final output = schemaMap.output;
112
+
113
+ if (schema == null ) {
114
+ throw MissingBuildConfigurationException ('schema_map => schema' );
115
+ }
116
+
117
+ if (output == null ) {
118
+ throw MissingBuildConfigurationException ('schema_map => output' );
119
+ }
95
120
96
121
// Loop through all files in glob
97
- if (schemaMap.queriesGlob == null ) {
98
- throw Exception ('''No queries were considered on this generation!
99
- Make sure that `queries_glob` your build.yaml file include GraphQL queries files.
100
- ''' );
101
- } else if (Glob (schemaMap.queriesGlob! ).matches (schemaMap.schema! )) {
122
+ if (queriesGlob == null ) {
123
+ throw MissingBuildConfigurationException ('schema_map => queries_glob' );
124
+ } else if (Glob (queriesGlob).matches (schema)) {
102
125
throw QueryGlobsSchemaException ();
103
- } else if (Glob (schemaMap. queriesGlob! ).matches (schemaMap. output! )) {
126
+ } else if (Glob (queriesGlob).matches (output)) {
104
127
throw QueryGlobsOutputException ();
105
128
}
106
129
107
- final assetStream = buildStep.findAssets (Glob (schemaMap.queriesGlob! ));
108
- var gqlDocs = await assetStream
109
- .asyncMap (
110
- (asset) async => parseString (
111
- await buildStep.readAsString (asset),
112
- url: asset.path,
113
- ),
114
- )
115
- .toList ();
130
+ final gqlSchema = await readGraphQlFiles (buildStep, schema);
131
+
132
+ if (gqlSchema.isEmpty) {
133
+ throw MissingFilesException (schema);
134
+ }
135
+
136
+ var gqlDocs = await readGraphQlFiles (buildStep, queriesGlob);
137
+
138
+ if (gqlDocs.isEmpty) {
139
+ throw MissingFilesException (queriesGlob);
140
+ }
116
141
117
142
if (schemaMap.appendTypeName) {
118
143
gqlDocs = gqlDocs.map (
@@ -137,38 +162,26 @@ Make sure that `queries_glob` your build.yaml file include GraphQL queries files
137
162
.toList ();
138
163
}
139
164
140
- final schemaAssetStream = buildStep.findAssets (Glob (schemaMap.schema! ));
141
-
142
- DocumentNode gqlSchema;
143
-
144
- try {
145
- gqlSchema = await schemaAssetStream
146
- .asyncMap (
147
- (asset) async => parseString (
148
- await buildStep.readAsString (asset),
149
- url: asset.path,
150
- ),
151
- )
152
- .first;
153
- } catch (e) {
154
- throw Exception (
155
- '''Schema `${schemaMap .schema }` was not found or doesn't have a proper format!
156
- Make sure the file exists and you've typed it correctly on build.yaml.
157
- $e
158
- ''' );
159
- }
160
-
161
165
final libDefinition = generateLibrary (
162
- _addGraphQLExtensionToPathIfNeeded (schemaMap. output! ),
166
+ _addGraphQLExtensionToPathIfNeeded (output),
163
167
gqlDocs,
164
168
options,
165
169
schemaMap,
166
170
fragmentsCommon,
167
- gqlSchema,
171
+ gqlSchema.first ,
168
172
);
173
+
169
174
if (onBuild != null ) {
170
175
onBuild !(libDefinition);
171
176
}
177
+
178
+ final buffer = StringBuffer ();
179
+
180
+ final outputFileId = AssetId (
181
+ buildStep.inputId.package,
182
+ _addGraphQLExtensionToPathIfNeeded (output),
183
+ );
184
+
172
185
writeLibraryDefinitionToBuffer (
173
186
buffer,
174
187
options.ignoreForFile,
177
190
178
191
await buildStep.writeAsString (outputFileId, buffer.toString ());
179
192
180
- if (! schemaMap. output! .endsWith ('.graphql.dart' )) {
193
+ if (! output.endsWith ('.graphql.dart' )) {
181
194
final forwarderOutputFileId =
182
- AssetId (buildStep.inputId.package, schemaMap. output! );
195
+ AssetId (buildStep.inputId.package, output);
183
196
await buildStep.writeAsString (
184
197
forwarderOutputFileId, writeLibraryForwarder (libDefinition));
185
198
}
0 commit comments