belongsTo static method

ModelFieldDefinition belongsTo({
  1. required QueryField<Object?> key,
  2. bool isRequired = true,
  3. required String ofModelName,
  4. QueryField<Object?>? 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<Object?> key,
  bool isRequired = true,
  required String ofModelName,
  QueryField<Object?>? 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,
      // Allow support for old schemas
      // ignore: deprecated_member_use_from_same_package
      targetName: targetName,
      targetNames: targetNames,
      associatedName: associatedName,
      associatedType: associatedType,
    ),
  );
}