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 {
  List<FragmentDefinitionNode> fragmentsCommon = [];

  final fragmentsGlob = options.fragmentsGlob;
  if (fragmentsGlob != null) {
    final commonFragments = (await readGraphQlFiles(buildStep, fragmentsGlob))
        .map((e) => e.definitions.whereType<FragmentDefinitionNode>())
        .expand((e) => e)
        .toList();

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

    fragmentsCommon.addAll(commonFragments);
  }

  for (final schemaMap in options.schemaMapping) {
    List<FragmentDefinitionNode> schemaCommonFragments = [
      ...fragmentsCommon,
    ];
    final schemaFragmentsGlob = schemaMap.fragmentsGlob;
    if (schemaFragmentsGlob != null) {
      final schemaFragments =
          (await readGraphQlFiles(buildStep, schemaFragmentsGlob))
              .map((e) => e.definitions.whereType<FragmentDefinitionNode>())
              .expand((e) => e)
              .toList();

      if (schemaFragments.isEmpty) {
        throw MissingFilesException(schemaFragmentsGlob);
      }

      schemaCommonFragments.addAll(schemaFragments);
    }

    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 (queriesGlob == null) {
      throw MissingBuildConfigurationException('schema_map => queries_glob');
    } else if (Glob(queriesGlob).matches(schema)) {
      throw QueryGlobsSchemaException();
    } else if (Glob(queriesGlob).matches(output)) {
      throw QueryGlobsOutputException();
    }

    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(
        (doc) {
          final transformed =
              transform(doc, [AppendTypename(schemaMap.typeNameField)]);

          // transform makes definitions growable: false so just recreate it again
          // as far as we need to add some elements there lately
          return DocumentNode(
            definitions: List.from(transformed.definitions),
            span: transformed.span,
          );
        },
      ).toList();

      schemaCommonFragments = schemaCommonFragments
          .map((fragments) => transform(
                fragments,
                [AppendTypename(schemaMap.typeNameField)],
              ))
          .toList();
    }

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

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

    final buffer = StringBuffer();

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

    writeLibraryDefinitionToBuffer(
      buffer,
      options.ignoreForFile,
      libDefinition,
    );

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

    if (!output.endsWith('.graphql.dart')) {
      final forwarderOutputFileId =
          AssetId(buildStep.inputId.package, output);
      await buildStep.writeAsString(
          forwarderOutputFileId, writeLibraryForwarder(libDefinition));
    }
  }
}