I am new to AWS Amplify and GraphQL API. I created data models using Amplify Studio. Here is the schema.graphql:
type Language @model @auth(rules: [{allow: public}]) {
id: ID!
name: String
fluency: String
rpm: Float
translatorID: ID @index(name: "byTranslator")
}
enum TranslatorTime {
FULLTIME
FREETIME
PARTTIME
}
type Translator @model @auth(rules: [{allow: public}]) {
id: ID!
userId: String
time: TranslatorTime
Languages: [Language] @hasMany(indexName: "byTranslator", fields: ["id"])
}
I am using graphql api on flutter app to create items in translator model, here is the code i tried:
Future<void> addTranslator(Translator translator) async {
try {
final request = ModelMutations.create(translator);
final response = await Amplify.API.mutate(request: request).response;
final createdTranslator = response.data;
if (createdTranslator == null) {
safePrint('errors: ${response.errors}');
return;
}
safePrint('Mutation result: $createdTranslator');
} on ApiException catch (e) {
safePrint('Mutation failed: $e');
}
}
But the problem is when i create translator item, the list of languages is not saved in the database, since the translator has one-to-many relation with language, its not working, in both tables translator and language. I am creating list of language and passing to the language model. The mutation result is:
Mutation result: Translator {id=156a3fd4-ae09-4579-977c-0ca4bb205aca, userId=7d8d034e-d13c-4f53-87c4-4f4bb9adfe03, time=PARTTIME, createdAt=2023-02-06T10:05:52.329000000Z, updatedAt=2023-02-06T10:05:52.329000000Z}
it does not have Languages. Tried with translatorId and without it.