Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit 9570da5

Browse files
authored
Merge pull request #300 from comigor/feature/config-file-values-handling
config file values handling
2 parents 4cffbf2 + 1a5c8c6 commit 9570da5

16 files changed

+277
-105
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 7.0.0-beta.7
4+
5+
- config file error handling
6+
37
## 7.0.0-beta.6
48

59
- packages update

analysis_options.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ analyzer:
77
implicit-casts: false
88
exclude:
99
- example/**/*.dart
10-
- test/**/*.dart
1110
linter:
1211
rules:
1312
- public_member_api_docs

lib/builder.dart

+75-62
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ String _addGraphQLExtensionToPathIfNeeded(String path) {
2424
}
2525

2626
List<String> _builderOptionsToExpectedOutputs(BuilderOptions builderOptions) {
27-
final schemaMaps =
27+
final schemaMapping =
2828
GeneratorOptions.fromJson(builderOptions.config).schemaMapping;
2929

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');
3432
}
3533

36-
return schemaMaps
34+
if (schemaMapping.any((s) => s.output == null)) {
35+
throw MissingBuildConfigurationException('schema_mapping => output');
36+
}
37+
38+
return schemaMapping
3739
.map((s) {
3840
final outputWithoutLib = s.output!.replaceAll(RegExp(r'^lib/'), '');
3941

@@ -70,49 +72,72 @@ class GraphQLQueryBuilder implements Builder {
7072
r'$lib$': expectedOutputs,
7173
};
7274

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+
7392
@override
7493
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(),
88101
);
102+
103+
if (fragmentsCommon.isEmpty) {
104+
throw MissingFilesException(fragmentsGlob);
105+
}
89106
}
90107

91108
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+
}
95120

96121
// 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)) {
102125
throw QueryGlobsSchemaException();
103-
} else if (Glob(schemaMap.queriesGlob!).matches(schemaMap.output!)) {
126+
} else if (Glob(queriesGlob).matches(output)) {
104127
throw QueryGlobsOutputException();
105128
}
106129

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+
}
116141

117142
if (schemaMap.appendTypeName) {
118143
gqlDocs = gqlDocs.map(
@@ -137,38 +162,26 @@ Make sure that `queries_glob` your build.yaml file include GraphQL queries files
137162
.toList();
138163
}
139164

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-
161165
final libDefinition = generateLibrary(
162-
_addGraphQLExtensionToPathIfNeeded(schemaMap.output!),
166+
_addGraphQLExtensionToPathIfNeeded(output),
163167
gqlDocs,
164168
options,
165169
schemaMap,
166170
fragmentsCommon,
167-
gqlSchema,
171+
gqlSchema.first,
168172
);
173+
169174
if (onBuild != null) {
170175
onBuild!(libDefinition);
171176
}
177+
178+
final buffer = StringBuffer();
179+
180+
final outputFileId = AssetId(
181+
buildStep.inputId.package,
182+
_addGraphQLExtensionToPathIfNeeded(output),
183+
);
184+
172185
writeLibraryDefinitionToBuffer(
173186
buffer,
174187
options.ignoreForFile,
@@ -177,9 +190,9 @@ $e
177190

178191
await buildStep.writeAsString(outputFileId, buffer.toString());
179192

180-
if (!schemaMap.output!.endsWith('.graphql.dart')) {
193+
if (!output.endsWith('.graphql.dart')) {
181194
final forwarderOutputFileId =
182-
AssetId(buildStep.inputId.package, schemaMap.output!);
195+
AssetId(buildStep.inputId.package, output);
183196
await buildStep.writeAsString(
184197
forwarderOutputFileId, writeLibraryForwarder(libDefinition));
185198
}

lib/generator/errors.dart

+27
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@ Change `schema` or `output` location and try again.
6565
''';
6666
}
6767

68+
/// Define an exception thrown when Artemis does not find asset files
69+
class MissingFilesException implements Exception {
70+
/// glob pattern which was used
71+
final String globPattern;
72+
73+
/// Define an exception thrown when Artemis does not find asset files
74+
MissingFilesException(this.globPattern);
75+
76+
@override
77+
String toString() {
78+
return 'Missing files for $globPattern';
79+
}
80+
}
81+
82+
/// Define an exception thrown when Artemis does not find required config params
83+
class MissingBuildConfigurationException implements Exception {
84+
/// missing config option name
85+
final String name;
86+
87+
/// Define an exception thrown when Artemis does not find required config params
88+
MissingBuildConfigurationException(this.name);
89+
90+
@override
91+
String toString() =>
92+
'Missing `$name` configuration option. Cehck `build.yaml` configuration';
93+
}
94+
6895
/// Define an exception thrown when Artemis find a scalar on schema but it's
6996
/// not configured on `build.yaml`.
7097
class MissingScalarConfigurationException implements Exception {

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: artemis
2-
version: 7.0.0-beta.6
2+
version: 7.0.0-beta.7
33

44
description: Build dart types from GraphQL schemas and queries (using Introspection Query).
55
homepage: https://github.com/comigor/artemis

test/generator/helpers_test.dart

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'package:artemis/generator/data/data.dart';
2-
import 'package:gql/language.dart';
31
import 'package:test/test.dart';
42
import 'package:artemis/generator/helpers.dart';
53

test/generator/print_helpers_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ enum SomeEnum {
696696
test('Should not add ignore_for_file when ignoreForFile is null', () {
697697
final buffer = StringBuffer();
698698
final definition = LibraryDefinition(basename: r'test_query.graphql');
699-
final List<String> ignoreForFile = [];
699+
final ignoreForFile = <String>[];
700700

701701
writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);
702702

test/helpers.dart

-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ Future testNaming({
6464
required String namingScheme,
6565
bool shouldFail = false,
6666
}) {
67-
Logger.root.level = Level.ALL;
68-
6967
final anotherBuilder = graphQLQueryBuilder(BuilderOptions({
7068
'generate_helpers': false,
7169
'schema_mapping': [

test/query_generator/append_type_name_test.dart

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import 'package:artemis/builder.dart';
2-
import 'package:artemis/generator/data/enum_value_definition.dart';
31
import 'package:artemis/generator/data/data.dart';
4-
import 'package:build/build.dart';
5-
import 'package:build_test/build_test.dart';
62
import 'package:test/test.dart';
73

84
import '../helpers.dart';

test/query_generator/ast_schema/multiple_schema_mappint_test.dart

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import 'package:build/build.dart';
33
import 'package:artemis/generator/data/data.dart';
44
import 'package:artemis/generator/data/enum_value_definition.dart';
55
import 'package:build_test/build_test.dart';
6-
import 'package:logging/logging.dart';
76
import 'package:test/test.dart';
8-
import 'package:collection/collection.dart';
97

108
void main() {
119
group('Multiple schema mapping', () {
@@ -30,7 +28,7 @@ void main() {
3028
],
3129
}));
3230

33-
int count = 0;
31+
var count = 0;
3432
anotherBuilder.onBuild = expectAsync1((definition) {
3533
log.fine(definition);
3634
if (count == 0) {

test/query_generator/deprecated/deprecated_field_test.dart

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:artemis/generator/data/data.dart';
2-
import 'package:gql/language.dart';
32
import 'package:test/test.dart';
43

54
import '../../helpers.dart';

0 commit comments

Comments
 (0)