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

Commit

Permalink
Merge pull request #300 from comigor/feature/config-file-values-handling
Browse files Browse the repository at this point in the history
config file values handling
  • Loading branch information
vasilich6107 authored Apr 29, 2021
2 parents 4cffbf2 + 1a5c8c6 commit 9570da5
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 105 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 7.0.0-beta.7

- config file error handling

## 7.0.0-beta.6

- packages update
Expand Down
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ analyzer:
implicit-casts: false
exclude:
- example/**/*.dart
- test/**/*.dart
linter:
rules:
- public_member_api_docs
Expand Down
137 changes: 75 additions & 62 deletions lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ String _addGraphQLExtensionToPathIfNeeded(String path) {
}

List<String> _builderOptionsToExpectedOutputs(BuilderOptions builderOptions) {
final schemaMaps =
final schemaMapping =
GeneratorOptions.fromJson(builderOptions.config).schemaMapping;

if (schemaMaps.any((s) => s.output == null)) {
throw Exception('''One or more SchemaMap configurations miss an output!
Please check your build.yaml file.
''');
if (schemaMapping.isEmpty) {
throw MissingBuildConfigurationException('schema_mapping');
}

return schemaMaps
if (schemaMapping.any((s) => s.output == null)) {
throw MissingBuildConfigurationException('schema_mapping => output');
}

return schemaMapping
.map((s) {
final outputWithoutLib = s.output!.replaceAll(RegExp(r'^lib/'), '');

Expand Down Expand Up @@ -70,49 +72,72 @@ class GraphQLQueryBuilder implements Builder {
r'$lib$': expectedOutputs,
};

/// read asset files
Future<List<DocumentNode>> readGraphQlFiles(
BuildStep buildStep,
String schema,
) async {
final schemaAssetStream = buildStep.findAssets(Glob(schema));

return await schemaAssetStream
.asyncMap(
(asset) async => parseString(
await buildStep.readAsString(asset),
url: asset.path,
),
)
.toList();
}

@override
Future<void> build(BuildStep buildStep) async {
if (options.fragmentsGlob != null) {
final fragmentStream = buildStep.findAssets(Glob(options.fragmentsGlob!));
final fDocs = await fragmentStream
.asyncMap(
(asset) async => parseString(
await buildStep.readAsString(asset),
url: asset.path,
),
)
.toList();
fDocs.forEach(
(fDoc) => fragmentsCommon.addAll(
fDoc.definitions.whereType<FragmentDefinitionNode>().toList()),
final fragmentsGlob = options.fragmentsGlob;
if (fragmentsGlob != null) {
fragmentsCommon.addAll(
(await readGraphQlFiles(buildStep, fragmentsGlob))
.map((e) => e.definitions.whereType<FragmentDefinitionNode>())
.expand((e) => e)
.toList(),
);

if (fragmentsCommon.isEmpty) {
throw MissingFilesException(fragmentsGlob);
}
}

for (final schemaMap in options.schemaMapping) {
final buffer = StringBuffer();
final outputFileId = AssetId(buildStep.inputId.package,
_addGraphQLExtensionToPathIfNeeded(schemaMap.output!));
final queriesGlob = schemaMap.queriesGlob;
final schema = schemaMap.schema;
final output = schemaMap.output;

if (schema == null) {
throw MissingBuildConfigurationException('schema_map => schema');
}

if (output == null) {
throw MissingBuildConfigurationException('schema_map => output');
}

// Loop through all files in glob
if (schemaMap.queriesGlob == null) {
throw Exception('''No queries were considered on this generation!
Make sure that `queries_glob` your build.yaml file include GraphQL queries files.
''');
} else if (Glob(schemaMap.queriesGlob!).matches(schemaMap.schema!)) {
if (queriesGlob == null) {
throw MissingBuildConfigurationException('schema_map => queries_glob');
} else if (Glob(queriesGlob).matches(schema)) {
throw QueryGlobsSchemaException();
} else if (Glob(schemaMap.queriesGlob!).matches(schemaMap.output!)) {
} else if (Glob(queriesGlob).matches(output)) {
throw QueryGlobsOutputException();
}

final assetStream = buildStep.findAssets(Glob(schemaMap.queriesGlob!));
var gqlDocs = await assetStream
.asyncMap(
(asset) async => parseString(
await buildStep.readAsString(asset),
url: asset.path,
),
)
.toList();
final gqlSchema = await readGraphQlFiles(buildStep, schema);

if (gqlSchema.isEmpty) {
throw MissingFilesException(schema);
}

var gqlDocs = await readGraphQlFiles(buildStep, queriesGlob);

if (gqlDocs.isEmpty) {
throw MissingFilesException(queriesGlob);
}

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

final schemaAssetStream = buildStep.findAssets(Glob(schemaMap.schema!));

DocumentNode gqlSchema;

try {
gqlSchema = await schemaAssetStream
.asyncMap(
(asset) async => parseString(
await buildStep.readAsString(asset),
url: asset.path,
),
)
.first;
} catch (e) {
throw Exception(
'''Schema `${schemaMap.schema}` was not found or doesn't have a proper format!
Make sure the file exists and you've typed it correctly on build.yaml.
$e
''');
}

final libDefinition = generateLibrary(
_addGraphQLExtensionToPathIfNeeded(schemaMap.output!),
_addGraphQLExtensionToPathIfNeeded(output),
gqlDocs,
options,
schemaMap,
fragmentsCommon,
gqlSchema,
gqlSchema.first,
);

if (onBuild != null) {
onBuild!(libDefinition);
}

final buffer = StringBuffer();

final outputFileId = AssetId(
buildStep.inputId.package,
_addGraphQLExtensionToPathIfNeeded(output),
);

writeLibraryDefinitionToBuffer(
buffer,
options.ignoreForFile,
Expand All @@ -177,9 +190,9 @@ $e

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

if (!schemaMap.output!.endsWith('.graphql.dart')) {
if (!output.endsWith('.graphql.dart')) {
final forwarderOutputFileId =
AssetId(buildStep.inputId.package, schemaMap.output!);
AssetId(buildStep.inputId.package, output);
await buildStep.writeAsString(
forwarderOutputFileId, writeLibraryForwarder(libDefinition));
}
Expand Down
27 changes: 27 additions & 0 deletions lib/generator/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ Change `schema` or `output` location and try again.
''';
}

/// Define an exception thrown when Artemis does not find asset files
class MissingFilesException implements Exception {
/// glob pattern which was used
final String globPattern;

/// Define an exception thrown when Artemis does not find asset files
MissingFilesException(this.globPattern);

@override
String toString() {
return 'Missing files for $globPattern';
}
}

/// Define an exception thrown when Artemis does not find required config params
class MissingBuildConfigurationException implements Exception {
/// missing config option name
final String name;

/// Define an exception thrown when Artemis does not find required config params
MissingBuildConfigurationException(this.name);

@override
String toString() =>
'Missing `$name` configuration option. Cehck `build.yaml` configuration';
}

/// Define an exception thrown when Artemis find a scalar on schema but it's
/// not configured on `build.yaml`.
class MissingScalarConfigurationException implements Exception {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: artemis
version: 7.0.0-beta.6
version: 7.0.0-beta.7

description: Build dart types from GraphQL schemas and queries (using Introspection Query).
homepage: https://github.com/comigor/artemis
Expand Down
2 changes: 0 additions & 2 deletions test/generator/helpers_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'package:artemis/generator/data/data.dart';
import 'package:gql/language.dart';
import 'package:test/test.dart';
import 'package:artemis/generator/helpers.dart';

Expand Down
2 changes: 1 addition & 1 deletion test/generator/print_helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ enum SomeEnum {
test('Should not add ignore_for_file when ignoreForFile is null', () {
final buffer = StringBuffer();
final definition = LibraryDefinition(basename: r'test_query.graphql');
final List<String> ignoreForFile = [];
final ignoreForFile = <String>[];

writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

Expand Down
2 changes: 0 additions & 2 deletions test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ Future testNaming({
required String namingScheme,
bool shouldFail = false,
}) {
Logger.root.level = Level.ALL;

final anotherBuilder = graphQLQueryBuilder(BuilderOptions({
'generate_helpers': false,
'schema_mapping': [
Expand Down
4 changes: 0 additions & 4 deletions test/query_generator/append_type_name_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import 'package:artemis/builder.dart';
import 'package:artemis/generator/data/enum_value_definition.dart';
import 'package:artemis/generator/data/data.dart';
import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
import 'package:test/test.dart';

import '../helpers.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import 'package:build/build.dart';
import 'package:artemis/generator/data/data.dart';
import 'package:artemis/generator/data/enum_value_definition.dart';
import 'package:build_test/build_test.dart';
import 'package:logging/logging.dart';
import 'package:test/test.dart';
import 'package:collection/collection.dart';

void main() {
group('Multiple schema mapping', () {
Expand All @@ -30,7 +28,7 @@ void main() {
],
}));

int count = 0;
var count = 0;
anotherBuilder.onBuild = expectAsync1((definition) {
log.fine(definition);
if (count == 0) {
Expand Down
1 change: 0 additions & 1 deletion test/query_generator/deprecated/deprecated_field_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:artemis/generator/data/data.dart';
import 'package:gql/language.dart';
import 'package:test/test.dart';

import '../../helpers.dart';
Expand Down
Loading

0 comments on commit 9570da5

Please sign in to comment.