MkGraphQLBuilder constructor

MkGraphQLBuilder([
  1. BuilderOptions? options
])

Implementation

factory MkGraphQLBuilder([BuilderOptions? options]) {
  // One recursive listing of `lib/`, reused for both this file discovery
  // loop and (via `autoDetectedSchemaPath` below) MkGraphQLConfig's own
  // schema-file auto-detection — previously two separate full-tree
  // `listSync(recursive: true)` walks per builder construction.
  final libDir = Directory('lib');
  final gqlFiles = libDir.existsSync()
      ? libDir
          .listSync(recursive: true)
          .whereType<File>()
          .where(
              (f) => f.path.endsWith('.graphql') || f.path.endsWith('.gql'))
          .toList()
      : const <File>[];

  String? autoDetectedSchemaPath;
  for (final file in gqlFiles) {
    final name = p.basename(file.path);
    if (name == 'schema.graphql' || name == 'schema.gql') {
      autoDetectedSchemaPath = file.path;
      break;
    }
  }

  final config = MkGraphQLConfig.fromOptions(
    options ?? BuilderOptions.empty,
    autoDetectedSchemaPath: autoDetectedSchemaPath,
  );
  final extensions = <String, List<String>>{};
  final parsedCache = <String, _CachedParse>{};
  final schemaPath = config.schemaPath;
  final sharedDir = config.sharedDir;
  for (final file in gqlFiles) {
    try {
      final content = file.readAsStringSync();
      final parsed = ParsedGraphQLDocument.parse(content);
      parsedCache[file.path] = _CachedParse(content, parsed);
      final outputRels = GeneratorOrchestrator.computeOutputPaths(
        content: content,
        inputPath: file.path,
        schemaPath: schemaPath,
        sharedDir: sharedDir,
        preParsed: parsed,
      );
      final outputs = outputRels
          .map((k) => p.normalize(p.join(p.dirname(file.path), k)))
          .toList();
      extensions[file.path] = outputs;
    } catch (e, st) {
      log.warning(
        'mk_graphql_generator: failed to compute build extensions for '
        '${file.path} — this file will be skipped by codegen until its '
        'error is fixed.',
        e,
        st,
      );
    }
  }

  if (extensions.isEmpty) {
    extensions['.graphql'] = ['.gql.dart'];
    extensions['.gql'] = ['.gql.dart'];
  }

  return MkGraphQLBuilder._(config, extensions, parsedCache);
}