fromSchemaFile function

Future<String> fromSchemaFile(
  1. BuildStep buildStep
)

Implementation

Future<String> fromSchemaFile(BuildStep buildStep) async {
  final List<String> values = [];
  await for (final input in buildStep.findAssets(Glob('lib/**.graphql'))) {
    final schemaStr = await buildStep.readAsString(input);
    final schema = buildSchema(schemaStr);

    final queryType = schema.queryType;

    for (final t in schema.allTypes) {
      t.whenNamed(
        enum_: (t) {
          values.add('''
enum ${t.name} {
  ${t.values.map((e) => '${e.name},').join('\n  ')}
}''');
        },
        scalar: (t) {},
        object: (t) {},
        input: (t) {
          values.add('''
@JsonSerializable()
class ${t.name} {
  ${t.fields.map((e) => 'final ${graphQLToDartType(e.type)} ${e.name};').join('\n  ')}
  const ${t.name}({
    ${t.fields.map((e) => '${e.type.isNonNullable ? 'required ' : ''}this.${e.name},').join('\n  ')}
  });

  factory ${t.name}.fromJson(Map<String, Object?> json) => _\$${t.name}FromJson(json);
  Map<String, Object?> toJson() => _\$${t.name}ToJson(this);
}''');
        },
        union: (t) {
          values.add('''
@freezed
class ${t.name} {
  ${t.possibleTypes.map((variant) {
            return '''
const factory ${t.name}.${variant.name}({
  ${variant.fields.map((e) => '${e.type.isNonNullable ? 'required ' : ''} ${graphQLToDartType(e.type)} ${e.name},').join('\n  ')}
}) = _${variant.name};
''';
          }).join('\n')}

}''');
        },
      );
    }

    if (queryType != null) {
      values.add('abstract class Query {');
      for (final field in queryType.fields) {
        values.add(
          '${graphQLToDartType(field.type)} ${field.name}(${_inputs(field.inputs)});',
        );
      }
      values.add('}');
    }
  }
  return values.join('\n\n');
}