build method

  1. @override
Future<void> build(
  1. BuildStep buildStep
)

Generates the outputs for a given BuildStep.

Implementation

@override
Future<void> build(BuildStep buildStep) async {
  // for (final basePath in basePaths) {
  final allClasses = <ClassElement>[];
  final allResolvers = <Type, List<Element>>{
    Query: [],
    Mutation: [],
    Subscription: [],
  };
  await for (final input in buildStep.findAssets(Glob('$basePath/**.dart'))) {
    final LibraryReader reader;
    try {
      final library = await buildStep.resolver.libraryFor(input);
      reader = LibraryReader(library);
    } on NonLibraryAssetException catch (_) {
      continue;
    }
    final classResolversMethods = reader.allElements
        .whereType<ClassElement>()
        .where((element) => const TypeChecker.fromRuntime(ClassResolver)
            .hasAnnotationOf(element))
        .expand((e) => e.methods);

    for (final e in [Query, Mutation, Subscription]) {
      final typeChecker = TypeChecker.fromRuntime(e);
      allResolvers[e]!.addAll(
        reader.allElements.where((w) => typeChecker.hasAnnotationOfExact(w)),
      );
      allResolvers[e]!.addAll(classResolversMethods
          .where((e) => typeChecker.hasAnnotationOfExact(e)));
    }
    allClasses.addAll(
      reader.classes.where(
        (element) => const TypeChecker.fromRuntime(BaseGraphQLTypeDecorator)
            .hasAnnotationOf(element),
      ),
    );
  }

  try {
    final _serializers = allClasses
        .where(
          (element) {
            return element.typeParameters.isEmpty &&
                generateSerializer(element);
          },
        )
        .map((e) {
          final typeName =
              e.thisType.getDisplayString(withNullability: false);
          return '${ReCase(typeName).camelCase}$serializerSuffix,';
        })
        .toSet()
        .join();
    final _genericSerializersCtx = allClasses
        .where(
          (element) =>
              element.typeParameters.isNotEmpty &&
              generateSerializer(element),
        )
        .map((e) {
          final typeName = e.displayName;
          return '${ReCase(typeName).camelCase}SerdeCtx,';
        })
        .toSet()
        .join();

    Iterable<String> _resolverStr(Type type) {
      return allResolvers[type]!
          .map((e) => '${e.name}$graphQLFieldSuffix,')
          .toSet();
    }

    final queries = _resolverStr(Query);
    final mutations = _resolverStr(Mutation);
    final subscriptions = _resolverStr(Subscription);
    final allElements =
        allResolvers.values.expand((el) => el).followedBy(allClasses);

    String out = '''
// ignore: depend_on_referenced_packages
import 'package:leto_schema/leto_schema.dart';
${allElements.map((e) => "import '${cleanImport(basePath, e.source!.uri)}';").toSet().join()}

GraphQLSchema recreateGraphQLApiSchema() {
HotReloadableDefinition.incrementCounter();
_graphqlApiSchema = null;
return graphqlApiSchema;
}

GraphQLSchema? _graphqlApiSchema;
GraphQLSchema get graphqlApiSchema => _graphqlApiSchema ??= GraphQLSchema(
serdeCtx: SerdeCtx()..addAll([$_serializers])..children.addAll([$_genericSerializersCtx]),
queryType: objectType(
  'Query',
  fields: [${queries.join()}],
),
${mutations.isEmpty ? '' : '''
mutationType: objectType(
  'Mutation',
  fields: [${mutations.join()}],
),
'''}
${subscriptions.isEmpty ? '' : '''
subscriptionType: objectType(
  'Subscription',
  fields: [${subscriptions.join()}],
),
'''}
);

''';
    try {
      out = DartFormatter().format(out);
    } catch (_) {}

    await buildStep.writeAsString(_allFileOutput(basePath, buildStep), out);
  } catch (e, s) {
    print('$e $s');
  }
  // }
}