addAllModelApis method

void addAllModelApis(
  1. CodeBuilder code, {
  2. ClassSnippetGenerator? extraGenerator,
  3. ClassSnippetGenerator? inputGenerator,
})

Implementation

void addAllModelApis(
  CodeBuilder code, {
  ClassSnippetGenerator? extraGenerator,
  ClassSnippetGenerator? inputGenerator,
}) {
  final fragmentNames = <String>[];

  declaredObjectTypes.forEach((def) {
    // If there are union types, add them now
    final unionInterfaces = this
        .unionTypes
        .values
        .where((union) => union.types.any((t) {
              return t.name.value == def.name.value;
            }))
        .map((e) => e.name.value)
        .toSet();
    code += classDefinition(
      this,
      '${def.name.value}Data'.toNameNode(),
      isAbstract: true,
      fromJson: true,
      toJson: true,
      toMap: true,
      isInput: false,
      isData: true,
      directives: def.directives,
      isJoinRecord: def.directives.hasDirective('joinRecord'),
      extraMixins: [],
      interfaces: [
        ...unionInterfaces,
        ...def.interfaces.map(
          (e) => e.toDartType(withNullability: false),
        )
      ],
      fields: def.fields.map(
        (d) => FieldDefinition.ofField(this, d, entityName: def.name.value),
      ),
      extraGenerator: extraGenerator,
    );
  });
  log.info('Writing Unions');
  unionTypes.values.forEach((union) {
    final unionInterface = union.directives
        .getDirectiveValue('union', 'interface')
        .stringValueOrNull;
    late List<FieldDefinition> fields;
    if (unionInterface != null) {
      final unionInterfaceDef = interfaceTypes[unionInterface]!;
      fields = unionInterfaceDef.fields.map((field) {
        return FieldDefinition.ofField(this, field,
            entityName: union.name.value);
      }).toList();
    } else {
      fields = [];
    }
  });

  log.info('Writing Interfaces');
  this.declaredInterfaceTypes.forEach((def) {
    if (!def.directives.hasDirective('unionInterface')) {
      code += classDefinition(
        this,
        def.name,
        toJson: false,
        toMap: false,
        fromJson: false,
        isInput: false,
        isJoinRecord: false,
        directives: def.directives,
        fields: def.fields.map((d) =>
            FieldDefinition.ofField(this, d, entityName: def.name.value)),
        isAbstract: true,
      );
    }
  });

  log.info('Writing typedefs');
  this.typedefs.forEach((name, definition) {
    code += 'typedef $name = $definition;';
  });

  log.info('Writing Input Ref Aliases');
  final handled = <String>{};
  this
      .declaredInputTypes
      .expand((element) => element.fields)
      .forEach((field) {
    var refAlias =
        field.directives.getDirectiveValue('ref', 'alias').stringValueOrNull;
    final fieldType = field.type.toDartType(withNullability: false);
    if (refAlias != null && !handled.contains(fieldType)) {
      handled.add(fieldType);
      code += "typedef $fieldType = ${refAlias};";
    }

    var refListAlias = field.directives
        .getDirectiveValue('reflist', 'alias')
        .stringValueOrNull;
    var targetType = field.directives
        .getDirectiveValue('reflist', 'target')
        .stringValueOrNull;
    if (refListAlias != null &&
        targetType != null &&
        !handled.contains(targetType)) {
      handled.add(targetType);
      code += "typedef $targetType = ${refListAlias};";
    }
  });

  log.info('Writing Enum');
  this.declaredEnumTypes.forEach((def) {
    code += [
      'class ${def.name.value} extends GraphEnum<String> {',
      for (var field in def.values)
        '  static const ${field.name.value} = ${def.name.value}("${field.name.value}");',
      '  static const values = [${def.values.map((v) => v.name.value).join(',')}];',
      '  const ${def.name.value}(String value): super(value);',
      '  factory ${def.name.value}.fromJson(json) {',
      '    switch(json!.toString().toLowerCase()) {',
      for (var field in def.values)
        '      case "${field.name.value.toLowerCase()}": return ${def.name.value}.${field.name.value};',
      '      default: throw "Enum not recognized \${json}";',
      '    }',
      '  }',
      '}',
    ];
  });
}