initFromAnnotation static method

Future<SunnyGraphQLModel> initFromAnnotation(
  1. Element element,
  2. ConstantReader annotation
)

Implementation

static Future<SunnyGraphQLModel> initFromAnnotation(
    Element element, ConstantReader annotation) async {
  assert(element is ClassElement, "@graphQL must only be applied to class");
  final cls = element as ClassElement;
  assert(cls.isAbstract, "@graphQL must be applied to abstract classes");
  var includesValue = annotation.read('includes');
  var excludesValue = annotation.read('excludes');
  var mixinMapValue = annotation.read('mixins');
  var typeMap = annotation.read('typeMap').isMap
      ? annotation.read('typeMap').mapValue.map((key, value) =>
          MapEntry(key!.toStringValue()!, value!.toStringValue()!))
      : <String, String>{};

  var mixinMap = mixinMapValue.isMap
      ? mixinMapValue.mapValue.map((key, value) =>
          MapEntry(key!.toStringValue()!, value!.toStringValue()!))
      : <String, String>{};

  var includesSet = includesValue.isList
      ? includesValue.listValue.map((d) => d.toStringValue()).toSet()
      : <String>{};
  var excludeSet = excludesValue.isList
      ? excludesValue.listValue
          .map((d) => RegExp(d.toStringValue()!))
          .toList()
      : <RegExp>[];
  final concreteName = cls.name.substring(1);

  final sourceUris = [annotation.read('uri'), annotation.read('fragmentUri')]
      .map((ref) => ref.isString ? ref.stringValue : null)
      .whereType<String>()
      .toList();
  var doc = (await Future.wait(sourceUris.map((uri) async {
        try {
          final read = uri.startsWith("http")
              ? (await Client().get(Uri.parse(uri))).body
              : File(uri).readAsStringSync();
          final doc = lang.parseString(read);
          return doc;
        } catch (e) {
          print(e);
          return null;
        }
      })))
          .reduce((value, element) => DocumentNode(definitions: [
                if (value != null)
                  for (final def in value.definitions) def,
                if (element != null)
                  for (final def in element.definitions) def,
              ])) ??
      DocumentNode();

  var typeNameMappers = <RegExp, String>{};
  var fieldNameMappers = <RegExp, String>{};
  if (!annotation.read('typeNameMappers').isNull) {
    typeNameMappers = annotation.read('typeNameMappers').mapValue.map(
        (key, value) =>
            MapEntry(RegExp(key!.toStringValue()!), value!.toStringValue()!));
  }
  if (!annotation.read('fieldNameMappers').isNull) {
    fieldNameMappers = annotation.read('fieldNameMappers').mapValue.map(
        (key, value) =>
            MapEntry(RegExp(key!.toStringValue()!), value!.toStringValue()!));
  }

  final result = SunnyGraphQLModel(
    className: concreteName,
    mixinMap: mixinMap,
    typeMap: typeMap,
    includes: includesSet
        .whereType<String>()
        .map((pattern) => RegExp(pattern))
        .toList(),
    excludes: excludeSet
        .whereType<String>()
        .map((pattern) => RegExp(pattern))
        .toList(),
    doc: doc,
    fieldNameMap: fieldNameMappers,
    typeNameMap: typeNameMappers,
  );

  doc.definitions
      .forEach((def) => result.addDefinition(def, forceInclude: false));
  return result;
}