getModelSchemaByModelName function
Gets the modelSchema from provider that matches the name and validates its fields.
Implementation
ModelSchema getModelSchemaByModelName(
String modelName,
GraphQLRequestOperation? operation,
) {
// ignore: invalid_use_of_protected_member
final provider = Amplify.API.defaultPlugin.modelProvider;
if (provider == null) {
throw const ApiOperationException(
'No modelProvider found',
recoverySuggestion:
'Pass in a modelProvider instance while instantiating APIPlugin',
);
}
// In web, the modelName runtime type conversion will add "$" to returned string.
// If ends with "$" on web, strip last character.
// TODO(ragingsquirrel3): fix underlying issue with modelName
if (zIsWeb && modelName.endsWith(r'$')) {
modelName = modelName.substring(0, modelName.length - 1);
}
final schema =
(provider.modelSchemas + provider.customTypeSchemas).firstWhere(
(elem) => elem.name == modelName,
orElse: () => throw ApiOperationException(
'No schema found for the ModelType provided: $modelName',
recoverySuggestion: 'Pass in a valid modelProvider instance while '
'instantiating APIPlugin or provide a valid ModelType',
),
);
if (schema.fields == null) {
throw const ApiOperationException(
'Schema found does not have a fields property',
recoverySuggestion: 'Pass in a valid modelProvider instance while '
'instantiating APIPlugin',
);
}
if (operation == GraphQLRequestOperation.list && schema.pluralName == null) {
throw const ApiOperationException(
'No schema name found',
recoverySuggestion: 'Pass in a valid modelProvider instance while '
'instantiating APIPlugin or provide a valid ModelType',
);
}
return schema;
}