belongsTo static method

ModelFieldDefinition belongsTo({
  1. required QueryField key,
  2. bool isRequired = true,
  3. required String ofModelName,
  4. QueryField? associatedKey,
  5. @Deprecated('Please use the latest version of Amplify CLI to regenerate models') String? targetName,
  6. List<String>? targetNames,
})

Implementation

static ModelFieldDefinition belongsTo(
    {required QueryField key,
    bool isRequired = true,
    required String ofModelName,
    QueryField? associatedKey,
    @Deprecated('Please use the latest version of Amplify CLI to regenerate models')
        String? targetName,
    List<String>? targetNames}) {
  // Extra code needed due to lack of nullability support
  String? associatedName;
  String? associatedType;

  associatedName = associatedKey?.fieldName;
  associatedType = associatedKey?.fieldType?.ofModelName ?? ofModelName;

  return field(
      key: key,
      isRequired: isRequired,
      ofType:
          ModelFieldType(ModelFieldTypeEnum.model, ofModelName: ofModelName),
      association: ModelAssociation(
          associationType: ModelAssociationEnum.BelongsTo,
          targetName: targetName,
          targetNames: targetNames,
          associatedName: associatedName,
          associatedType: associatedType));
}